use crate::util::model::message::{Message, Target, TargetKind}; use crate::util::model::session::Session; use std::collections::HashMap; use dioxus::prelude::*; mod content; mod targets; #[derive(PartialEq)] enum Steps { Message, Targets, Verify, Done, } #[derive(Props, Clone, PartialEq, Debug)] struct Form { title: Signal, body: Signal, targets: Signal>, } impl Default for Form { fn default() -> Self { Self { title: use_signal(|| String::new()), body: use_signal(|| String::new()), targets: use_signal(|| { HashMap::from([( 0, Target { kind: TargetKind::None, value: String::new(), }, )]) }), } } } #[component] pub fn MessagesCreatePage() -> Element { let mut step = use_signal(|| Steps::Message); let form = Form::default(); let submit_proposal = move |_| async move { let targets = (form.targets)(); if let Ok(amount) = message_proposal( (form.title)(), (form.body)(), targets.into_values().collect(), ) .await { tracing::info!("{}", amount); step.set(Steps::Verify); } else { tracing::info!("Error occured") } }; rsx! { div { class: "w-full max-w-2xl space-y-3 flex flex-col", h1 { class: "text-xl font-bold text-primary", "Nieuw bericht" } ul { class: "steps pb-10 mx-auto", li { class: "step step-primary", "Inhoud" }, li { class: "step", class: if let Steps::Targets | Steps::Done | Steps::Verify = *step.read() { "step-primary" }, "Naar" } li { class: "step", class: if let Steps::Verify | Steps::Done = *step.read() { "step-primary" }, "Controleren" } li { class: "step", class: if let Steps::Done = *step.read() { "step-primary" }, "Klaar" } } div { class: "flex flex-col gap-y-5", div { class: if let Steps::Message = *step.read() { "" } else { "hidden" }, content::Content { step: step, title: form.title, body: form.body, } } div { class: if let Steps::Targets = *step.read() { "" } else { "hidden" }, targets::Targets { step: step, targets: form.targets, onsubmit: submit_proposal, } } } } } } #[server] async fn message_proposal( title: String, body: String, targets: Vec, ) -> Result { let user = Session::fetch_current_user().await?; if !user.admin { return Err(crate::Error::NoPermissions.into()); } let amount = Message::create_proposal(title, body, targets).await?; Ok(amount) }