diff --git a/Cargo.lock b/Cargo.lock index 3395550..72754a0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -560,6 +560,7 @@ dependencies = [ "log", "regex", "serde", + "serde_json", "uuid", "wasm-bindgen", "wasm-bindgen-test", diff --git a/Cargo.toml b/Cargo.toml index b80de30..ad56898 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,6 +19,7 @@ regex = "1" lazy_static = "1.4" uuid = { version = "1.3", features = ["v4", "fast-rng", "macro-diagnostics", "js", "serde"] } serde = { version = "1.0", features = ["derive"] } +serde_json = "1.0" csv = "1.2" [dev-dependencies] diff --git a/src/components/main_window.rs b/src/components/main_window.rs index 5028a29..7bd9766 100644 --- a/src/components/main_window.rs +++ b/src/components/main_window.rs @@ -76,25 +76,31 @@ pub fn MainWindow() -> Html { web_sys::window().unwrap().alert_with_message("No transfers to export.").unwrap(); return () } + 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(); if let Ok(csv) = state_to_csv(&main_state) { - let csv: &str = &csv; - let blob = Blob::new_with_str_sequence( - &Array::from_iter(std::iter::once(JsValue::from_str(csv)))); - 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(); - let anchor = document.create_element("a").unwrap() - .dyn_into::().unwrap(); - anchor.set_download("transfers.csv"); - anchor.set_href(&url); - anchor.click(); - } + save_str(&csv, "transfers.csv"); } }) }; + let export_json_button_callback = { + let main_state = main_state.clone(); + Callback::from(move |_| { + if let Ok(json) = serde_json::to_string(&main_state) { + save_str(&json, "plate-tool-state.json"); + } else { + web_sys::window().unwrap().alert_with_message("Failed to export.").unwrap(); + } + }) + }; + + let import_json_button_callback = { + let main_dispatch = main_dispatch.clone(); + Callback::from(move |_| { + !unimplemented!() + }) + }; + html! { <>
@@ -105,9 +111,10 @@ pub fn MainWindow() -> Html {
- +
+
@@ -122,3 +129,19 @@ pub fn MainWindow() -> Html { } } + +fn save_str(data: &str, name: &str) { + let blob = Blob::new_with_str_sequence( + &Array::from_iter(std::iter::once(JsValue::from_str(data)))); + 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(); + let anchor = document.create_element("a").unwrap() + .dyn_into::().unwrap(); + anchor.set_download(name); + anchor.set_href(&url); + anchor.click(); + } +}