Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix up to PR #10 #12

Merged
merged 3 commits into from
Aug 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ use prettyplease::unparse;
use proc_macro2::TokenStream;
use rinja_derive_standalone::derive_template;
use syn::{parse2, parse_quote};
use web_sys::js_sys::JSON;
use web_sys::wasm_bindgen::prelude::Closure;
use web_sys::wasm_bindgen::JsCast;
use web_sys::wasm_bindgen::{JsCast, JsValue};
use web_sys::{window, HtmlSelectElement, Storage};
use yew::{
function_component, html, use_state, Callback, Event, Html, Properties, SubmitEvent,
Expand Down Expand Up @@ -34,12 +35,13 @@ fn local_storage() -> Option<Storage> {
}
}

const THEME_SOURCE_KEY: &str = "theme";
const STRUCT_SOURCE_KEY: &str = "struct";
const TMPL_SOURCE_KEY: &str = "template";
const THEME_SOURCE_KEY: &str = "play-rinja-theme";
const STRUCT_SOURCE_KEY: &str = "play-rinja-struct";
const TMPL_SOURCE_KEY: &str = "play-rinja-template";

fn get_data_from_local_storage(storage: &Storage, key: &str) -> Option<String> {
storage.get_item(key).ok()?
let text = storage.get_item(key).ok()??;
JSON::parse(&text).ok()?.as_string()
}

#[function_component]
Expand All @@ -50,10 +52,10 @@ pub fn App() -> Html {
let value = local_storage
.as_ref()
.and_then(|ls| get_data_from_local_storage(ls, key));
Rc::from(match value.as_deref().unwrap_or_default() {
"" => default,
value => value,
})
match value.as_deref() {
Some(value) => value.into(),
None => default.into(),
}
};

let theme = local_storage_or(DEFAULT_THEME, THEME_SOURCE_KEY);
Expand Down Expand Up @@ -82,8 +84,12 @@ pub fn App() -> Html {
let state = state.clone();
move |data: String| {
if let Some(storage) = local_storage() {
// Doesn't matter whether or not it succeeded.
let _ = storage.set_item(storage_name, &data);
if let Ok(data) = JSON::stringify(&JsValue::from_str(&data)) {
if let Some(data) = data.as_string() {
// Doesn't matter whether or not it succeeded.
let _ = storage.set_item(storage_name, &data);
}
}
}

let mut new_state = Props::clone(&*state);
Expand Down