Skip to content

Commit c5e2c7c

Browse files
committed
appears to be working, more tests needed
1 parent 797fd7b commit c5e2c7c

File tree

2 files changed

+69
-2
lines changed

2 files changed

+69
-2
lines changed

Cargo.toml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,12 @@ yew-custom-components = { version = "0.2.2", features = ["table", "pagination"]
2424
serde_json = "1.0.138"
2525
yew-hooks = "0.3.3"
2626
yew-router = "0.18"
27-
web-sys = { version = "0.3.77", features = ["HtmlInputElement","Document", "Window"] }
27+
web-sys = { version = "0.3.77", features = ["HtmlInputElement","Document", "Window", "Blob", "Url", "HtmlElement"] }
2828
reqwest = { version = "0.12.12", features = ["json"] }
2929
cached = { version = "0.54.0", features = ["async"] }
30+
wasm-bindgen = "0.2"
31+
wasm-bindgen-futures = "0.4"
32+
js-sys = "0.3"
33+
3034
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
31-
tokio = { version = "1.43.0", features = ["full"] }
35+
tokio = { version = "1.43.0", features = ["full"] }

src/sauce/home.rs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ use plotly::layout::{AxisType};
1414
use yew::prelude::*;
1515
use serde::Deserialize;
1616

17+
use wasm_bindgen_futures::spawn_local;
18+
use web_sys::HtmlElement;
19+
use web_sys::{Blob, BlobPropertyBag, Url};
20+
use wasm_bindgen::JsValue;
21+
use js_sys::Array;
22+
use web_sys::wasm_bindgen::JsCast;
23+
24+
1725
#[derive(Debug, Serialize, Deserialize)]
1826
struct ReactionData {
1927
#[serde(rename = "energy")]
@@ -181,6 +189,41 @@ fn convert_string(entry: &Entry) -> String {
181189
output
182190
}
183191

192+
async fn download_xs_cache(selected_indexes: HashSet<usize>) {
193+
let cache = generate_cache(&selected_indexes).await;
194+
195+
// Convert the cache data to JSON
196+
let json_data = serde_json::to_string(&cache).unwrap();
197+
198+
// Create a Blob from the JSON data
199+
let mut blob_options = BlobPropertyBag::new();
200+
blob_options.set_type("application/json");
201+
202+
let blob = Blob::new_with_str_sequence_and_options(
203+
&Array::of1(&JsValue::from_str(&json_data)),
204+
&blob_options,
205+
).unwrap();
206+
207+
// Create a URL for the Blob
208+
let url = Url::create_object_url_with_blob(&blob).unwrap();
209+
210+
// Create a hidden anchor element to trigger the download
211+
let document = web_sys::window().unwrap().document().unwrap();
212+
let a = document.create_element("a").unwrap();
213+
a.set_attribute("href", &url).unwrap();
214+
a.set_attribute("download", "xs_cache.json").unwrap();
215+
a.set_attribute("style", "display: none;").unwrap();
216+
document.body().unwrap().append_child(&a).unwrap();
217+
218+
// Trigger the download
219+
220+
let a: HtmlElement = a.dyn_into::<HtmlElement>().unwrap();
221+
a.click();
222+
223+
// Clean up by revoking the object URL
224+
Url::revoke_object_url(&url).unwrap();
225+
document.body().unwrap().remove_child(&a).unwrap();
226+
}
184227

185228
#[function_component(Home)]
186229
pub fn home() -> Html {
@@ -381,6 +424,17 @@ pub fn home() -> Html {
381424
})
382425
};
383426

427+
428+
let onclick_download = {
429+
let selected_indexes = selected_indexes.clone();
430+
Callback::from(move |_| {
431+
let selected_indexes = selected_indexes.current().clone();
432+
spawn_local(async move {
433+
download_xs_cache(selected_indexes).await;
434+
});
435+
})
436+
};
437+
384438
// let pagination_options = yew_custom_components::pagination::Options::default()
385439
// .show_prev_next(true)
386440
// .show_first_last(true)
@@ -491,6 +545,15 @@ pub fn home() -> Html {
491545
>
492546
{if *is_y_log { "Switch Y to Linear Scale" } else { "Switch Y to Log Scale" }}
493547
</button>
548+
549+
<button
550+
class="btn btn-success mt-2"
551+
onclick={onclick_download}
552+
>
553+
<i class="fas fa-download mr-2"></i>
554+
{" Download Cross Section Data"}
555+
</button>
556+
494557
</div>
495558

496559
<div class="d-flex mb-2">

0 commit comments

Comments
 (0)