Start adding some concepts around UI state
This commit is contained in:
parent
ca89455d4d
commit
20b214df10
@ -1,13 +1,54 @@
|
||||
use std::rc::Rc;
|
||||
|
||||
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]
|
||||
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! {
|
||||
<div class="login-form">
|
||||
<div class="card">
|
||||
<h1>{"Welcome to Visions VTT"}</h1>
|
||||
<input type="text" name="username" placeholder="username" />
|
||||
<input type="password" name="password" placeholder="password" />
|
||||
<button onclick={on_click}>{"Login"}</button>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
@ -15,8 +56,17 @@ fn Login() -> Html {
|
||||
|
||||
#[function_component]
|
||||
fn App() -> Html {
|
||||
html! {
|
||||
<Login />
|
||||
let auth_info = use_reducer(AuthInfo::default);
|
||||
|
||||
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()} /> }
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user