2023-05-21 16:45:12 +00:00
|
|
|
#![allow(non_snake_case)]
|
2023-06-07 20:14:19 +00:00
|
|
|
use js_sys::Array;
|
|
|
|
use wasm_bindgen::{prelude::*, JsCast, JsValue};
|
|
|
|
use web_sys::{Blob, HtmlAnchorElement, HtmlDialogElement, HtmlFormElement, HtmlInputElement, Url};
|
2023-05-22 15:26:08 +00:00
|
|
|
use yew::prelude::*;
|
2023-05-24 15:42:54 +00:00
|
|
|
use yewdux::prelude::*;
|
|
|
|
|
2023-06-01 17:04:03 +00:00
|
|
|
use super::new_plate_dialog::NewPlateDialog;
|
2023-05-21 16:45:12 +00:00
|
|
|
use super::plates::plate_container::PlateContainer;
|
2023-06-01 17:04:03 +00:00
|
|
|
use super::states::{CurrentTransfer, MainState};
|
2023-05-21 16:45:12 +00:00
|
|
|
use super::transfer_menu::TransferMenu;
|
2023-06-01 17:04:03 +00:00
|
|
|
use super::tree::Tree;
|
2023-05-21 16:45:12 +00:00
|
|
|
|
2023-06-05 18:25:47 +00:00
|
|
|
use crate::data::csv::state_to_csv;
|
2023-06-07 20:14:19 +00:00
|
|
|
use crate::data::plate_instances::PlateInstance;
|
2023-05-24 15:42:54 +00:00
|
|
|
|
2023-05-22 15:26:08 +00:00
|
|
|
#[function_component]
|
|
|
|
pub fn MainWindow() -> Html {
|
2023-05-24 15:42:54 +00:00
|
|
|
let (main_state, main_dispatch) = use_store::<MainState>();
|
2023-06-07 21:17:40 +00:00
|
|
|
let (_, ct_dispatch) = use_store::<CurrentTransfer>();
|
2023-05-24 15:42:54 +00:00
|
|
|
|
2023-06-01 17:04:03 +00:00
|
|
|
let source_plate_instance: Option<PlateInstance> = main_state
|
|
|
|
.source_plates
|
|
|
|
.iter()
|
|
|
|
.find(|spi| spi.get_uuid() == main_state.selected_source_plate)
|
2023-05-24 20:10:33 +00:00
|
|
|
.cloned();
|
2023-05-24 22:39:38 +00:00
|
|
|
if let Some(spi) = source_plate_instance.clone() {
|
2023-06-01 17:04:03 +00:00
|
|
|
ct_dispatch.reduce_mut(|state| {
|
|
|
|
state.transfer.transfer_region.source_plate = spi.plate;
|
2023-05-24 22:39:38 +00:00
|
|
|
});
|
|
|
|
}
|
2023-06-01 17:04:03 +00:00
|
|
|
let destination_plate_instance: Option<PlateInstance> = main_state
|
|
|
|
.destination_plates
|
|
|
|
.iter()
|
|
|
|
.find(|dpi| dpi.get_uuid() == main_state.selected_dest_plate)
|
2023-05-24 20:10:33 +00:00
|
|
|
.cloned();
|
2023-05-24 22:39:38 +00:00
|
|
|
if let Some(dpi) = destination_plate_instance.clone() {
|
2023-06-01 17:04:03 +00:00
|
|
|
ct_dispatch.reduce_mut(|state| {
|
|
|
|
state.transfer.transfer_region.dest_plate = dpi.plate;
|
2023-05-24 22:39:38 +00:00
|
|
|
});
|
|
|
|
}
|
2023-05-24 15:42:54 +00:00
|
|
|
|
2023-05-22 22:31:02 +00:00
|
|
|
let new_plate_dialog_is_open = use_state_eq(|| false);
|
|
|
|
let new_plate_dialog_callback = {
|
|
|
|
let new_plate_dialog_is_open = new_plate_dialog_is_open.clone();
|
|
|
|
Callback::from(move |_| {
|
|
|
|
new_plate_dialog_is_open.set(false);
|
|
|
|
})
|
|
|
|
};
|
|
|
|
let open_new_plate_dialog_callback = {
|
|
|
|
let new_plate_dialog_is_open = new_plate_dialog_is_open.clone();
|
|
|
|
Callback::from(move |_| {
|
|
|
|
new_plate_dialog_is_open.set(true);
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
2023-06-05 18:38:29 +00:00
|
|
|
let new_button_callback = {
|
|
|
|
let main_dispatch = main_dispatch.clone();
|
|
|
|
Callback::from(move |_| {
|
|
|
|
let window = web_sys::window().unwrap();
|
2023-06-07 20:14:19 +00:00
|
|
|
let confirm =
|
|
|
|
window.confirm_with_message("This will reset all plates and transfers. Proceed?");
|
2023-06-05 18:38:29 +00:00
|
|
|
if let Ok(confirm) = confirm {
|
|
|
|
if confirm {
|
|
|
|
main_dispatch.set(MainState::default());
|
|
|
|
ct_dispatch.set(CurrentTransfer::default());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
2023-06-05 20:54:15 +00:00
|
|
|
let export_csv_button_callback = {
|
2023-06-05 18:25:47 +00:00
|
|
|
let main_state = main_state.clone();
|
|
|
|
Callback::from(move |_| {
|
2023-06-08 15:57:03 +00:00
|
|
|
if main_state.transfers.is_empty() {
|
2023-06-07 20:14:19 +00:00
|
|
|
web_sys::window()
|
|
|
|
.unwrap()
|
|
|
|
.alert_with_message("No transfers to export.")
|
|
|
|
.unwrap();
|
2023-06-08 15:57:03 +00:00
|
|
|
return;
|
2023-06-05 18:56:01 +00:00
|
|
|
}
|
2023-06-05 21:14:02 +00:00
|
|
|
web_sys::window().unwrap().alert_with_message("CSV export is currently not importable. Export as JSON if you'd like to back up your work!").unwrap();
|
2023-06-05 18:25:47 +00:00
|
|
|
if let Ok(csv) = state_to_csv(&main_state) {
|
2023-06-05 21:14:02 +00:00
|
|
|
save_str(&csv, "transfers.csv");
|
|
|
|
}
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
let export_json_button_callback = {
|
|
|
|
Callback::from(move |_| {
|
|
|
|
if let Ok(json) = serde_json::to_string(&main_state) {
|
|
|
|
save_str(&json, "plate-tool-state.json");
|
|
|
|
} else {
|
2023-06-07 20:14:19 +00:00
|
|
|
web_sys::window()
|
|
|
|
.unwrap()
|
|
|
|
.alert_with_message("Failed to export.")
|
|
|
|
.unwrap();
|
2023-06-05 18:25:47 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
2023-06-05 21:14:02 +00:00
|
|
|
let import_json_button_callback = {
|
|
|
|
Callback::from(move |_| {
|
2023-06-05 22:55:21 +00:00
|
|
|
let window = web_sys::window().unwrap();
|
|
|
|
let document = window.document().unwrap();
|
|
|
|
let body = document.body().unwrap();
|
2023-06-07 20:14:19 +00:00
|
|
|
let modal = document
|
|
|
|
.create_element("dialog")
|
|
|
|
.unwrap()
|
|
|
|
.dyn_into::<HtmlDialogElement>()
|
|
|
|
.unwrap();
|
2023-06-05 22:55:21 +00:00
|
|
|
modal.set_text_content(Some("Import File:"));
|
|
|
|
let onclose_callback = {
|
|
|
|
let modal = modal.clone();
|
|
|
|
Closure::<dyn FnMut(_)>::new(move |_: Event| {
|
|
|
|
modal.remove();
|
|
|
|
})
|
|
|
|
};
|
2023-06-08 15:57:03 +00:00
|
|
|
modal.set_onclose(Some(onclose_callback.as_ref().unchecked_ref()));
|
2023-06-05 22:55:21 +00:00
|
|
|
onclose_callback.forget();
|
|
|
|
|
2023-06-07 20:14:19 +00:00
|
|
|
let form = document
|
|
|
|
.create_element("form")
|
|
|
|
.unwrap()
|
|
|
|
.dyn_into::<HtmlFormElement>()
|
|
|
|
.unwrap();
|
|
|
|
let input = document
|
|
|
|
.create_element("input")
|
|
|
|
.unwrap()
|
|
|
|
.dyn_into::<HtmlInputElement>()
|
|
|
|
.unwrap();
|
2023-06-05 22:55:21 +00:00
|
|
|
input.set_type("file");
|
|
|
|
input.set_accept(".json");
|
|
|
|
form.append_child(&input).unwrap();
|
|
|
|
|
|
|
|
let input_callback = {
|
|
|
|
let main_dispatch = main_dispatch.clone();
|
|
|
|
let modal = modal.clone();
|
|
|
|
Closure::<dyn FnMut(_)>::new(move |e: Event| {
|
2023-06-07 20:14:19 +00:00
|
|
|
if let Some(input) = e.current_target() {
|
|
|
|
let input = input
|
|
|
|
.dyn_into::<HtmlInputElement>()
|
|
|
|
.expect("We know this is an input.");
|
|
|
|
if let Some(files) = input.files() {
|
|
|
|
if let Some(file) = files.get(0) {
|
|
|
|
let fr = web_sys::FileReader::new().unwrap();
|
|
|
|
fr.read_as_text(&file).unwrap();
|
|
|
|
let fr1 = fr.clone(); // Clone to avoid outliving closure
|
|
|
|
let main_dispatch = main_dispatch.clone(); // Clone to satisfy FnMut
|
|
|
|
// trait
|
|
|
|
let modal = modal.clone();
|
2023-06-07 21:17:40 +00:00
|
|
|
let onload = Closure::<dyn FnMut(_)>::new(move |_: Event| {
|
2023-06-07 20:14:19 +00:00
|
|
|
if let Some(value) =
|
|
|
|
&fr1.result().ok().and_then(|v| v.as_string())
|
|
|
|
{
|
|
|
|
let ms = serde_json::from_str::<MainState>(value);
|
|
|
|
match ms {
|
|
|
|
Ok(ms) => main_dispatch.set(ms),
|
|
|
|
Err(e) => log::debug!("{:?}", e),
|
|
|
|
};
|
|
|
|
modal.close();
|
|
|
|
}
|
|
|
|
});
|
2023-06-08 15:57:03 +00:00
|
|
|
fr.set_onload(Some(onload.as_ref().unchecked_ref()));
|
2023-06-07 20:14:19 +00:00
|
|
|
onload.forget(); // Magic (don't touch)
|
|
|
|
}
|
2023-06-05 22:55:21 +00:00
|
|
|
}
|
|
|
|
}
|
2023-06-07 20:14:19 +00:00
|
|
|
})
|
|
|
|
};
|
2023-06-08 15:57:03 +00:00
|
|
|
input.set_onchange(Some(input_callback.as_ref().unchecked_ref()));
|
2023-06-05 22:55:21 +00:00
|
|
|
input_callback.forget(); // Magic straight from the docs, don't touch :(
|
|
|
|
|
|
|
|
modal.append_child(&form).unwrap();
|
|
|
|
body.append_child(&modal).unwrap();
|
|
|
|
modal.show_modal().unwrap();
|
2023-06-05 21:14:02 +00:00
|
|
|
})
|
|
|
|
};
|
|
|
|
|
2023-06-01 17:04:03 +00:00
|
|
|
html! {
|
2023-06-05 18:25:47 +00:00
|
|
|
<>
|
|
|
|
<div class="upper_menu">
|
|
|
|
<div class="dropdown">
|
|
|
|
<button>{"File"}</button>
|
2023-06-05 18:38:29 +00:00
|
|
|
<button onclick={new_button_callback}>{"New"}</button>
|
2023-06-05 20:54:15 +00:00
|
|
|
<div class="dropdown-sub">
|
|
|
|
<button>{"Export"}</button>
|
|
|
|
<div>
|
|
|
|
<button onclick={export_csv_button_callback}>{"Export as CSV"}</button>
|
2023-06-05 21:14:02 +00:00
|
|
|
<button onclick={export_json_button_callback}>{"Export as JSON"}</button>
|
2023-06-05 20:54:15 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
2023-06-05 21:14:02 +00:00
|
|
|
<button onclick={import_json_button_callback}>{"Import"}</button>
|
2023-06-05 18:25:47 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
2023-05-22 15:26:08 +00:00
|
|
|
<div class="main_container">
|
2023-05-22 22:31:02 +00:00
|
|
|
<Tree open_new_plate_callback={open_new_plate_dialog_callback}/>
|
2023-05-22 15:26:08 +00:00
|
|
|
<TransferMenu />
|
2023-05-24 20:10:33 +00:00
|
|
|
<PlateContainer source_dims={source_plate_instance}
|
|
|
|
destination_dims={destination_plate_instance}/>
|
2023-05-22 22:31:02 +00:00
|
|
|
if {*new_plate_dialog_is_open} {
|
|
|
|
<NewPlateDialog close_callback={new_plate_dialog_callback}/>
|
|
|
|
}
|
2023-05-22 15:26:08 +00:00
|
|
|
</div>
|
2023-06-05 18:25:47 +00:00
|
|
|
</>
|
2023-05-22 15:26:08 +00:00
|
|
|
}
|
2023-05-21 16:45:12 +00:00
|
|
|
}
|
2023-06-05 21:14:02 +00:00
|
|
|
|
|
|
|
fn save_str(data: &str, name: &str) {
|
2023-06-07 20:14:19 +00:00
|
|
|
let blob =
|
|
|
|
Blob::new_with_str_sequence(&Array::from_iter(std::iter::once(JsValue::from_str(data))));
|
2023-06-05 21:14:02 +00:00
|
|
|
if let Ok(blob) = blob {
|
|
|
|
let url = Url::create_object_url_with_blob(&blob).expect("We have a blob, why not URL?");
|
|
|
|
// Beneath is the cool hack to download files
|
|
|
|
let window = web_sys::window().unwrap();
|
|
|
|
let document = window.document().unwrap();
|
2023-06-07 20:14:19 +00:00
|
|
|
let anchor = document
|
|
|
|
.create_element("a")
|
|
|
|
.unwrap()
|
|
|
|
.dyn_into::<HtmlAnchorElement>()
|
|
|
|
.unwrap();
|
2023-06-05 21:14:02 +00:00
|
|
|
anchor.set_download(name);
|
|
|
|
anchor.set_href(&url);
|
|
|
|
anchor.click();
|
|
|
|
}
|
|
|
|
}
|