Start adding some concepts around UI state

This commit is contained in:
Savanni D'Gerinel 2025-02-18 08:25:40 -05:00
parent ca89455d4d
commit 20b214df10

View File

@ -1,13 +1,54 @@
use std::rc::Rc;
use yew::prelude::*; use yew::prelude::*;
struct AuthInfo {
session_id: Option<String>,
}
impl Default for AuthInfo {
fn default() -> Self {
Self { session_id: None }
}
}
enum AuthAction {
Auth(String),
Unauth,
}
impl Reducible for AuthInfo {
type Action = AuthAction;
fn reduce(self: Rc<Self>, action: Self::Action) -> Rc<Self> {
match action {
AuthAction::Auth(session_id) => Self {
session_id: Some(session_id),
}
.into(),
AuthAction::Unauth => Self { session_id: None }.into(),
}
}
}
#[derive(Properties, PartialEq)]
struct LoginProps {
on_click: Callback<()>,
}
#[function_component] #[function_component]
fn Login() -> Html { fn Login(LoginProps { on_click }: &LoginProps) -> Html {
let on_click = {
let on_click = on_click.clone();
Callback::from(move |_| on_click.emit(()))
};
html! { html! {
<div class="login-form"> <div class="login-form">
<div class="card"> <div class="card">
<h1>{"Welcome to Visions VTT"}</h1> <h1>{"Welcome to Visions VTT"}</h1>
<input type="text" name="username" placeholder="username" /> <input type="text" name="username" placeholder="username" />
<input type="password" name="password" placeholder="password" /> <input type="password" name="password" placeholder="password" />
<button onclick={on_click}>{"Login"}</button>
</div> </div>
</div> </div>
} }
@ -15,8 +56,17 @@ fn Login() -> Html {
#[function_component] #[function_component]
fn App() -> Html { fn App() -> Html {
html! { let auth_info = use_reducer(AuthInfo::default);
<Login />
let on_login = {
let auth_info = auth_info.clone();
Callback::from(move |_| auth_info.dispatch(AuthAction::Auth("abcdefg".into())))
};
if auth_info.session_id.is_some() {
html! { <p>{ "this is just a thing" }</p> }
} else {
html! { <Login on_click={on_login.clone()} /> }
} }
} }