From 15accc2fcabc20fb0e7f3fea041c47d9e1ef1661 Mon Sep 17 00:00:00 2001 From: Emilia Date: Tue, 30 Jan 2024 20:37:15 -0500 Subject: [PATCH] I think this file was supposed to be in the last one lol --- .../callbacks/new_plate_dialog_callbacks.rs | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 src/components/callbacks/new_plate_dialog_callbacks.rs diff --git a/src/components/callbacks/new_plate_dialog_callbacks.rs b/src/components/callbacks/new_plate_dialog_callbacks.rs new file mode 100644 index 0000000..3aa4609 --- /dev/null +++ b/src/components/callbacks/new_plate_dialog_callbacks.rs @@ -0,0 +1,61 @@ +use yew::prelude::*; +use yewdux::prelude::*; + +use wasm_bindgen::JsCast; +use web_sys::{EventTarget, FormData, HtmlDialogElement, HtmlFormElement}; + +use crate::components::states::MainState; +use crate::data::plate::*; +use crate::data::plate_instances::PlateInstance; + +pub fn new_plate_callback( + dispatch: Dispatch, + close_callback: Callback<()>, +) -> Callback { + Callback::from(move |e: SubmitEvent| { + e.prevent_default(); + close_callback.emit(()); + let target: Option = e.target(); + let form = target.and_then(|t| t.dyn_into::().ok()); + if let Some(form) = form { + if let Ok(form_data) = FormData::new_with_form(&form) { + let name = form_data.get("new_plate_name").as_string().unwrap(); + let format = match form_data.get("plate_format").as_string().unwrap().as_str() { + "6" => PlateFormat::W6, + "12" => PlateFormat::W12, + "24" => PlateFormat::W24, + "48" => PlateFormat::W48, + "96" => PlateFormat::W96, + "384" => PlateFormat::W384, + "1536" => PlateFormat::W1536, + "3456" => PlateFormat::W3456, + _ => unreachable!(), + }; + if let Some(pt_string) = form_data.get("new_plate_type").as_string() { + let plate_type = match pt_string.as_str() { + "src" => PlateType::Source, + "dest" => PlateType::Destination, + _ => PlateType::Source, + }; + dispatch.reduce_mut(|s| { + if plate_type == PlateType::Source { + s.add_source_plate(PlateInstance::new(PlateType::Source, format, name)) + } else { + s.add_dest_plate(PlateInstance::new( + PlateType::Destination, + format, + name, + )) + } + }); + } + } + } + }) +} + +pub fn onclose(close_callback: Callback<()>) -> Callback { + Callback::from(move |_: Event| { + close_callback.emit(()); + }) +}