#![allow(non_snake_case)] use uuid::Uuid; use wasm_bindgen::JsCast; use web_sys::{EventTarget, HtmlElement, HtmlDialogElement}; use yew::prelude::*; use yewdux::prelude::*; use crate::components::states::{MainState, CurrentTransfer}; use crate::components::transfer_menu::RegionDisplay; use crate::data::transfer_region::Region; #[derive(PartialEq, Properties)] pub struct TreeProps { pub open_new_plate_callback: Callback<()>, } #[function_component] pub fn Tree(props: &TreeProps) -> Html { let (main_state, main_dispatch) = use_store::(); let (ct_state, ct_dispatch) = use_store::(); let plate_modal_id: UseStateHandle> = use_state(|| {None}); let open_plate_info_callback = { let plate_menu_id =plate_modal_id.clone(); Callback::from(move |e: MouseEvent| { let target: Option = e.target(); let li = target.and_then(|t| t.dyn_into::().ok()); if let Some(li) = li { if let Ok(id) = u128::from_str_radix(li.id().as_str(), 10) { plate_menu_id.set(Some(Uuid::from_u128(id))); } } }) }; let plate_info_close_callback = { let plate_menu_id =plate_modal_id.clone(); Callback::from(move |_| { plate_menu_id.set(None); }) }; let plate_info_delete_callback = { let dispatch = main_dispatch.clone(); let plate_menu_id = plate_modal_id.clone(); Callback::from(move |_| { if let Some(id) = *plate_menu_id { dispatch.reduce_mut(|state| { state.del_plate(id); }); } }) }; let source_plate_select_callback = { let main_dispatch = main_dispatch.clone(); let ct_dispatch = ct_dispatch.clone(); Callback::from(move |e: MouseEvent| { let target: Option = e.target(); let li = target.and_then(|t| t.dyn_into::().ok()); if let Some(li) = li { if let Ok(id) = u128::from_str_radix(li.id().as_str(), 10) { ct_dispatch.reduce_mut(|state| { state.transfer.source_region = Region::default(); }); main_dispatch.reduce_mut(|state| { state.selected_source_plate = Uuid::from_u128(id); }); } } }) }; let destination_plate_select_callback = { let main_dispatch = main_dispatch.clone(); let ct_dispatch = ct_dispatch.clone(); Callback::from(move |e: MouseEvent| { let target: Option = e.target(); let li = target.and_then(|t| t.dyn_into::().ok()); if let Some(li) = li { if let Ok(id) = u128::from_str_radix(li.id().as_str(), 10) { ct_dispatch.reduce_mut(|state| { state.transfer.dest_region = Region::default(); }); main_dispatch.reduce_mut(|state| { state.selected_dest_plate = Uuid::from_u128(id); }); } } }) }; let source_plates = main_state.source_plates.iter() .map(|spi| { html!{
  • {String::from(spi)}
  • } }).collect::(); let dest_plates = main_state.destination_plates.iter() .map(|dpi| { html!{
  • {String::from(dpi)}
  • } }).collect::(); let transfers = main_state.transfers.iter() .map(|transfer| { html!{
  • {transfer.name.clone()}
  • } }) .collect::(); html! {

    {"Source Plates:"}

      {source_plates}

    {"Destination Plates:"}

      {dest_plates}

    {"Transfers:"}

      {transfers}
    if let Some(id) = *plate_modal_id { } // Temporary
    } } #[derive(PartialEq, Properties)] struct PlateInfoModalProps { id: Uuid, dialog_close_callback: Callback<()>, delete_button_callback: Callback<()>, } #[function_component] fn PlateInfoModal(props: &PlateInfoModalProps) -> Html { let (state, dispatch) = use_store::(); let dialog_ref = use_node_ref(); let mut plate = state.source_plates.iter() .find(|spi| {spi.get_uuid() == props.id}); if plate == None { plate = state.destination_plates.iter() .find(|dpi| {dpi.get_uuid() == props.id}); } let plate_name = match plate { Some(plate) => plate.name.clone(), None => "Not Found".to_string() }; let onclose = { let dialog_close_callback = props.dialog_close_callback.clone(); move |_| {dialog_close_callback.emit(())} }; let delete_onclick = { let delete_button_callback = props.delete_button_callback.clone(); let dialog_ref = dialog_ref.clone(); move |_| { delete_button_callback.emit(()); dialog_ref.cast::().unwrap().close(); } }; { let dialog_ref = dialog_ref.clone(); use_effect_with_deps(|dialog_ref| { dialog_ref.cast::().unwrap().show_modal().ok(); }, dialog_ref); } html! {

    {"Plate Info"}

    {"Name: "}

    } }