From dc7f8eeff8013829d798976dd663943308a6fe31 Mon Sep 17 00:00:00 2001 From: antifallobst Date: Thu, 28 Sep 2023 19:49:28 +0200 Subject: [PATCH] refactor(topbar): refactored TopBar to be a function_component and implemented a second topbar for users that are logged in --- src/state.rs | 2 +- src/topbar/guest.rs | 81 +++++++++++++++++++++++++++++++++++++++++++++ src/topbar/mod.rs | 71 ++++++++------------------------------- src/topbar/user.rs | 19 +++++++++++ 4 files changed, 114 insertions(+), 59 deletions(-) create mode 100644 src/topbar/guest.rs create mode 100644 src/topbar/user.rs diff --git a/src/state.rs b/src/state.rs index 9e7d460..23a00c0 100644 --- a/src/state.rs +++ b/src/state.rs @@ -1,6 +1,6 @@ #[derive(Clone, PartialEq)] pub struct State { - auth_token: Option, + pub auth_token: Option, } impl Default for State { diff --git a/src/topbar/guest.rs b/src/topbar/guest.rs new file mode 100644 index 0000000..b668f4c --- /dev/null +++ b/src/topbar/guest.rs @@ -0,0 +1,81 @@ +use crate::topbar::Tab; +use std::rc::Rc; +use yew::prelude::*; + +pub enum Action { + ToggleSignIn, + ToggleSignUp, +} + +pub struct State { + sign_in: bool, + sign_up: bool, +} + +impl Default for State { + fn default() -> Self { + Self { + sign_in: false, + sign_up: false, + } + } +} + +impl Reducible for State { + type Action = Action; + + fn reduce(self: Rc, action: Self::Action) -> Rc { + let mut state = Self::default(); + + match action { + Action::ToggleSignIn => { + state.sign_up = false; + state.sign_in = !self.sign_in; + } + Action::ToggleSignUp => { + state.sign_in = false; + state.sign_up = !self.sign_up; + } + } + Rc::new(state) + } +} + +#[function_component] +pub fn TopBar() -> Html { + let state = use_reducer(State::default); + + let sign_in_onclick = { + let state = state.clone(); + Callback::from(move |_| state.dispatch(Action::ToggleSignIn)) + }; + + let sign_up_onclick = { + let state = state.clone(); + Callback::from(move |_| state.dispatch(Action::ToggleSignUp)) + }; + + html! { + + } +} diff --git a/src/topbar/mod.rs b/src/topbar/mod.rs index e701129..da7457f 100644 --- a/src/topbar/mod.rs +++ b/src/topbar/mod.rs @@ -1,9 +1,9 @@ -use yew::prelude::*; +mod guest; +mod user; -pub enum Msg { - ToggleSignIn, - ToggleSignUp, -} +use crate::state::State; +use std::rc::Rc; +use yew::prelude::*; #[derive(Properties, PartialEq)] pub struct TabProps { @@ -25,60 +25,15 @@ fn Tab(props: &TabProps) -> Html { } } -pub struct TopBar { - sign_in: bool, - sign_up: bool, -} +#[function_component] +pub fn TopBar() -> Html { + let ctx = use_context::>().expect("No context found!"); -impl Component for TopBar { - type Message = Msg; - type Properties = (); - - fn create(_ctx: &Context) -> Self { - Self { - sign_in: false, - sign_up: false, - } - } - - fn update(&mut self, _ctx: &Context, msg: Self::Message) -> bool { - match msg { - Msg::ToggleSignIn => { - self.sign_up = false; - self.sign_in = !self.sign_in; - true - } - Msg::ToggleSignUp => { - self.sign_in = false; - self.sign_up = !self.sign_up; - true - } - } - } - - fn view(&self, ctx: &Context) -> Html { - html! { - + html! { + if ctx.auth_token.is_some() { + + } else { + } } } diff --git a/src/topbar/user.rs b/src/topbar/user.rs new file mode 100644 index 0000000..eb23241 --- /dev/null +++ b/src/topbar/user.rs @@ -0,0 +1,19 @@ +use crate::topbar::Tab; +use yew::prelude::*; + +#[function_component] +pub fn TopBar() -> Html { + html! { + + } +}