82 lines
2.4 KiB
Rust
82 lines
2.4 KiB
Rust
use plan_proto::token::Token;
|
|
use yew::prelude::*;
|
|
|
|
use crate::{components::dialog::Dialog, storage::StorageKey};
|
|
|
|
#[function_component]
|
|
pub fn Nav() -> Html {
|
|
let token = Token::load_from_storage().ok();
|
|
let user = token
|
|
.as_ref()
|
|
.map(|token| {
|
|
html! {
|
|
<div class="user">
|
|
<span class="username">{(*token.username).clone()}</span>
|
|
</div>
|
|
}
|
|
})
|
|
.unwrap_or_else(|| {
|
|
html! {
|
|
<>
|
|
<a href="/signup"><button>{"signup"}</button></a>
|
|
<a href="/signin"><button>{"signin"}</button></a>
|
|
</>
|
|
}
|
|
});
|
|
let dialog = use_state(|| false);
|
|
let logged_in_buttons = token.is_some().then(|| {
|
|
let confirm_signout = {
|
|
let dialog = dialog.clone();
|
|
Callback::from(move |_| {
|
|
dialog.set(true);
|
|
})
|
|
};
|
|
|
|
let dialog = dialog.then(|| {
|
|
let cancel_signout = {
|
|
let dialog = dialog.clone();
|
|
Callback::from(move |_| {
|
|
dialog.set(false);
|
|
})
|
|
};
|
|
let callback = Callback::from(move |option: String| match option.as_str() {
|
|
"yes" => {
|
|
Token::delete();
|
|
let _ = gloo::utils::window().location().reload();
|
|
}
|
|
"no" => {
|
|
dialog.set(false);
|
|
}
|
|
_ => {}
|
|
});
|
|
let options: Box<[String]> = Box::new([String::from("yes"), String::from("no")]);
|
|
html! {
|
|
<Dialog
|
|
message={String::from("really sign out?")}
|
|
options={options}
|
|
cancel_callback={Some(cancel_signout)}
|
|
callback={callback}
|
|
/>
|
|
}
|
|
});
|
|
html! {
|
|
<>
|
|
<button onclick={confirm_signout} class="sign-out">{"sign out"}</button>
|
|
<a href="/plans/new"><button>{"new plan"}</button></a>
|
|
{dialog}
|
|
</>
|
|
}
|
|
});
|
|
html! {
|
|
<nav class="user-nav">
|
|
<a href="/">
|
|
<button>
|
|
{"home"}
|
|
</button>
|
|
</a>
|
|
{user}
|
|
{logged_in_buttons}
|
|
</nav>
|
|
}
|
|
}
|