From d39277e18265cb14d576222b8a02e9cbd258e190 Mon Sep 17 00:00:00 2001 From: antifallobst Date: Sat, 7 Oct 2023 10:43:32 +0200 Subject: [PATCH] feat: added loader to signin widget and restructured signin flow --- src/backend/mod.rs | 10 ++----- src/main.rs | 16 +++-------- src/topbar/guest/mod.rs | 30 ++++++------------- src/topbar/guest/signin.rs | 59 +++++++++++++++++++++++++++----------- 4 files changed, 57 insertions(+), 58 deletions(-) diff --git a/src/backend/mod.rs b/src/backend/mod.rs index 5a47bda..19512ca 100644 --- a/src/backend/mod.rs +++ b/src/backend/mod.rs @@ -48,13 +48,7 @@ impl Default for Session { } impl Session { - pub fn login( - &self, - username: String, - password: String, - callback: Callback<(Result, Callback>)>, - status_callback: Callback>, - ) { + pub fn login(&self, username: String, password: String, callback: Callback>) { let url = format!("{}/account/authenticate", &self.base_url); let body = json!({ @@ -90,7 +84,7 @@ impl Session { }; wasm_bindgen_futures::spawn_local( - async move { callback.emit((call.await, status_callback)) }, + async move { callback.emit(call.await) }, ); } diff --git a/src/main.rs b/src/main.rs index 53709cc..3e5378f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -37,7 +37,7 @@ fn switch(routes: Route) -> Html { #[derive(Clone, PartialEq)] pub struct Callbacks { - pub sign_in_callback: Callback<(Result, Callback>)>, + pub sign_in: Callback, } #[function_component] @@ -47,18 +47,10 @@ fn App() -> Html { let cloned_state = state.clone(); let callbacks = use_state(move || Callbacks { - sign_in_callback: Callback::from( - move |response: (Result, Callback>)| { + sign_in: Callback::from( + move |token: String| { let mut new_state = cloned_state.deref().clone(); - - response.1.emit(match response.0 { - Ok(token) => { - new_state.set_token(token); - Ok(()) - } - Err(e) => Err(e), - }); - + new_state.set_token(token); cloned_state.set(new_state); }, ), diff --git a/src/topbar/guest/mod.rs b/src/topbar/guest/mod.rs index 49f4568..d28da67 100644 --- a/src/topbar/guest/mod.rs +++ b/src/topbar/guest/mod.rs @@ -11,15 +11,12 @@ use yew::prelude::*; pub enum Action { ToggleSignIn, ToggleSignUp, - - SignInStatus(String), + CloseAuth, } pub struct State { sign_in: bool, sign_up: bool, - - sign_in_status: AttrValue, } impl Default for State { @@ -27,8 +24,6 @@ impl Default for State { Self { sign_in: false, sign_up: false, - - sign_in_status: AttrValue::default(), } } } @@ -48,10 +43,9 @@ impl Reducible for State { state.sign_in = false; state.sign_up = !self.sign_up; } - Action::SignInStatus(s) => { - state.sign_in = true; + Action::CloseAuth => { + state.sign_in = false; state.sign_up = false; - state.sign_in_status = s.into(); } } Rc::new(state) @@ -71,18 +65,10 @@ pub fn TopBar() -> Html { let state = state.clone(); Callback::from(move |_| state.dispatch(Action::ToggleSignUp)) }; - - let sign_up_close = { + + let close_auth_callback = { let state = state.clone(); - Callback::from(move |_| state.dispatch(Action::ToggleSignUp)) - }; - - let sign_in_status = { - let state = state.clone(); - Callback::from(move |status: Result<()>| match status { - Ok(_) => state.dispatch(Action::ToggleSignIn), - Err(e) => state.dispatch(Action::SignInStatus(e.to_string())), - }) + Callback::from(move |_| state.dispatch(Action::CloseAuth)) }; html! { @@ -100,11 +86,11 @@ pub fn TopBar() -> Html { if state.sign_in { - + } if state.sign_up { - + } } diff --git a/src/topbar/guest/signin.rs b/src/topbar/guest/signin.rs index 214b8f1..1ec64f3 100644 --- a/src/topbar/guest/signin.rs +++ b/src/topbar/guest/signin.rs @@ -8,15 +8,16 @@ use yew::prelude::*; #[derive(PartialEq, Clone, Default, Debug)] struct Data { + pub submitted: bool, + pub status: String, + pub username: String, pub password: String, - pub status: String, } #[derive(Properties, PartialEq)] pub struct Props { - pub status: AttrValue, - pub status_callback: Callback>, + pub close: Callback<()>, } #[function_component] @@ -51,20 +52,40 @@ pub fn SignIn(props: &Props) -> Html { cloned_state.set(data); }); - let status_callback = props.status_callback.clone(); - let onsubmit = Callback::from(move |_| { - let state = state.deref().clone(); + let cloned_state = state.clone(); + let callback = callbacks.sign_in.clone(); + let close_callback = props.close.clone(); + let on_sign_in_api_response = Callback::from(move |response: Result| { + let mut data = cloned_state.deref().clone(); + + match response { + Ok(token) => { + callback.emit(token); + close_callback.emit(()); + } + Err(e) => data.status = e.to_string(), + } + cloned_state.set(data); + }); + + let callback = on_sign_in_api_response.clone(); + let cloned_state = state.clone(); + let onsubmit = Callback::from(move |event: SubmitEvent| { + event.prevent_default(); + + let mut state = cloned_state.deref().clone(); + state.submitted = true; session.login( - state.username, - state.password, - callbacks.sign_in_callback.clone(), - status_callback.clone(), + state.username.clone(), + state.password.clone(), + callback.clone(), ); + cloned_state.set(state); }); html! { -
+


@@ -75,13 +96,19 @@ pub fn SignIn(props: &Props) -> Html {
-
- -
+ if !state.submitted { +
+ +
+ } else { +
+
+
+ }
-

{props.status.clone()}

+

{state.deref().status.clone()}

-
+ } }