use dioxus::prelude::{dioxus_elements::FileEngine, *}; use std::sync::Arc; #[derive(Debug)] struct UploadedFile { name: String, contents: String, } #[derive(PartialEq)] enum Steps { Upload, Verify, Done, } #[component] pub fn Migration() -> Element { let step = use_signal(|| Steps::Upload); rsx! { div { class: "flex flex-col items-center justify-center h-full py-10", ul { class: "steps pb-36", li { class: "step step-primary", "Uploaden" }, li { class: "step", class: if *step.read() == Steps::Verify { "step-primary" }, "Controleren" }, li { class: "step", "Klaar" }, }, match *step.read() { Steps::Upload => rsx! { Upload { step: step } }, Steps::Verify => rsx! { Verify {} }, Steps::Done => rsx! { Verify {} }, } } } } #[component] fn Upload(step: Signal) -> Element { let mut file_uploaded = use_signal(|| None); let mut loading = use_signal(|| false); let read_files = move |file_engine: Arc| async move { let files = file_engine.files(); for file_name in &files { if let Some(contents) = file_engine.read_file_to_string(file_name).await { file_uploaded.set(Some(UploadedFile { name: file_name.clone(), contents, })); } } }; let upload_files = move |evt: FormEvent| async move { if let Some(file_engine) = evt.files() { read_files(file_engine).await; } }; let sumbit = move |_evt: FormEvent| async move { match &*file_uploaded.read() { Some(file) => { loading.set(true); if let Ok(_response) = upload_members_list(file.contents.clone()).await { tracing::info!("Done"); step.set(Steps::Verify); } loading.set(false); } None => tracing::info!("File doesn't exists"), } }; rsx! { form { class: "flex flex-col items-center w-full h-full max-w-md mx-auto px-2", onsubmit: sumbit, h2 { class: "text-xl", "Selecteer het ledenbestand" }, input { r#type: "file", class: "file-input file-input-bordered w-full mt-16", accept: ".csv", multiple: false, autocomplete: "off", onchange: upload_files, } button { class: "btn btn-primary btn-wide mt-5", disabled: file_uploaded.read().is_none() || loading(), if loading() { span { class: "loading loading-spinner" } } "Uploaden" } "{file_uploaded.read().is_none()}" } } } #[component] fn Verify() -> Element { rsx! { div { class: "flex flex-col items-center w-full h-full max-w-md mx-auto px-2", h2 { class: "text-xl", "Controleer de actie" }, } } } #[server] async fn upload_members_list(input: String) -> Result { use crate::util::model::member::Member; tracing::info!("Getting members..."); let result = Member::migrate_proposal(input).await; tracing::info!("{:?}", result); Ok("Whoo".to_string()) } #[server] async fn check_members_list(correct: bool, id: u32) -> Result<(), ServerFnError> { tracing::info!("boe"); Ok(()) }