Prepare loading state
This commit is contained in:
parent
1aba33fb91
commit
26142084f6
@ -10,6 +10,7 @@ use seed::input;
|
|||||||
use seed::label;
|
use seed::label;
|
||||||
use seed::{div, log, prelude::*, App, Url, C};
|
use seed::{div, log, prelude::*, App, Url, C};
|
||||||
use shared::apirequests::users::LoginUser;
|
use shared::apirequests::users::LoginUser;
|
||||||
|
use shared::datatypes::Loadable;
|
||||||
use shared::datatypes::User;
|
use shared::datatypes::User;
|
||||||
|
|
||||||
use crate::i18n::{I18n, Lang};
|
use crate::i18n::{I18n, Lang};
|
||||||
@ -31,7 +32,7 @@ fn init(url: Url, orders: &mut impl Orders<Msg>) -> Model {
|
|||||||
base_url: Url::new().add_path_part("app"),
|
base_url: Url::new().add_path_part("app"),
|
||||||
page: Page::init(url, orders, lang.clone()),
|
page: Page::init(url, orders, lang.clone()),
|
||||||
i18n: lang,
|
i18n: lang,
|
||||||
user: None,
|
user: Loadable::Data(None),
|
||||||
login_form: LoginForm::default(),
|
login_form: LoginForm::default(),
|
||||||
login_data: LoginUser::default(),
|
login_data: LoginUser::default(),
|
||||||
}
|
}
|
||||||
@ -47,7 +48,7 @@ struct Model {
|
|||||||
base_url: Url,
|
base_url: Url,
|
||||||
page: Page,
|
page: Page,
|
||||||
i18n: i18n::I18n,
|
i18n: i18n::I18n,
|
||||||
user: Option<User>,
|
user: Loadable<User>,
|
||||||
login_form: LoginForm,
|
login_form: LoginForm,
|
||||||
login_data: LoginUser,
|
login_data: LoginUser,
|
||||||
}
|
}
|
||||||
@ -139,8 +140,8 @@ fn update(msg: Msg, model: &mut Model, orders: &mut impl Orders<Msg>) {
|
|||||||
Msg::UserReceived(user)
|
Msg::UserReceived(user)
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
Msg::UserReceived(user) => model.user = Some(user),
|
Msg::UserReceived(user) => model.user = Loadable::Data(Some(user)),
|
||||||
Msg::NotAuthenticated => {if model.user.is_some() {model.user = None; logout(orders)}},
|
Msg::NotAuthenticated => {if model.user.is_some() {model.user = Loadable::Data(None); logout(orders)}},
|
||||||
Msg::Login => {login_user(model, orders)}
|
Msg::Login => {login_user(model, orders)}
|
||||||
Msg::UsernameChanged(s) => model.login_data.username = s,
|
Msg::UsernameChanged(s) => model.login_data.username = s,
|
||||||
Msg::PasswordChanged(s) => model.login_data.password = s,
|
Msg::PasswordChanged(s) => model.login_data.password = s,
|
||||||
@ -238,7 +239,7 @@ impl<'a> Urls<'a> {
|
|||||||
fn view(model: &Model) -> Node<Msg> {
|
fn view(model: &Model) -> Node<Msg> {
|
||||||
div![
|
div![
|
||||||
C!["page"],
|
C!["page"],
|
||||||
if let Some(user) = &model.user {
|
if let Some(ref user) = *model.user {
|
||||||
div![
|
div![
|
||||||
navigation::navigation(&model.i18n, &model.base_url, user),
|
navigation::navigation(&model.i18n, &model.base_url, user),
|
||||||
view_content(&model.page, &model.base_url)
|
view_content(&model.page, &model.base_url)
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
use std::ops::Deref;
|
||||||
|
|
||||||
use serde::{Deserialize, Serialize, Serializer};
|
use serde::{Deserialize, Serialize, Serializer};
|
||||||
/// A generic list returntype containing the User and a Vec containing e.g. Links or Users
|
/// A generic list returntype containing the User and a Vec containing e.g. Links or Users
|
||||||
#[derive(Clone, Deserialize, Serialize)]
|
#[derive(Clone, Deserialize, Serialize)]
|
||||||
@ -86,3 +88,20 @@ impl std::fmt::Display for Secret {
|
|||||||
f.write_str("*****SECRET*****")
|
f.write_str("*****SECRET*****")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
|
pub enum Loadable<T> {
|
||||||
|
Data(Option<T>),
|
||||||
|
Loading,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> Deref for Loadable<T>{
|
||||||
|
type Target = Option<T>;
|
||||||
|
|
||||||
|
fn deref(&self) -> &Self::Target {
|
||||||
|
match self {
|
||||||
|
Loadable::Data(t) => t,
|
||||||
|
Loadable::Loading => &None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user