Formatting...

This commit is contained in:
Dietrich 2021-05-28 17:22:27 +02:00 committed by Franz Dietrich
parent 26142084f6
commit 50da81889e
8 changed files with 44 additions and 49 deletions

View File

@ -108,4 +108,3 @@ impl Lang {
bundle bundle
} }
} }

View File

@ -141,18 +141,24 @@ fn update(msg: Msg, model: &mut Model, orders: &mut impl Orders<Msg>) {
}); });
} }
Msg::UserReceived(user) => model.user = Loadable::Data(Some(user)), Msg::UserReceived(user) => model.user = Loadable::Data(Some(user)),
Msg::NotAuthenticated => {if model.user.is_some() {model.user = Loadable::Data(None); logout(orders)}}, Msg::NotAuthenticated => {
Msg::Login => {login_user(model, orders)} if model.user.is_some() {
model.user = Loadable::Data(None);
logout(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,
} }
} }
fn logout(orders: &mut impl Orders<Msg>) { fn logout(orders: &mut impl Orders<Msg>) {
orders.perform_cmd(async {let request = Request::new("/admin/logout/"); orders.perform_cmd(async {
let request = Request::new("/admin/logout/");
unwrap_or_return!(fetch(request).await, Msg::GetLoggedUser); unwrap_or_return!(fetch(request).await, Msg::GetLoggedUser);
Msg::NotAuthenticated}); Msg::NotAuthenticated
});
} }
fn login_user(model: &mut Model, orders: &mut impl Orders<Msg>) { fn login_user(model: &mut Model, orders: &mut impl Orders<Msg>) {
@ -245,7 +251,7 @@ fn view(model: &Model) -> Node<Msg> {
view_content(&model.page, &model.base_url) view_content(&model.page, &model.base_url)
] ]
} else { } else {
view_login(&model.i18n, &model) view_login(&model.i18n, model)
} }
] ]
} }

View File

@ -18,7 +18,6 @@ use shared::{
use crate::{i18n::I18n, unwrap_or_return, unwrap_or_send}; use crate::{i18n::I18n, unwrap_or_return, unwrap_or_send};
/// Setup the page /// Setup the page
pub fn init(mut url: Url, orders: &mut impl Orders<Msg>, i18n: I18n) -> Model { pub fn init(mut url: Url, orders: &mut impl Orders<Msg>, i18n: I18n) -> Model {
// fetch the links to fill the list. // fetch the links to fill the list.
@ -104,9 +103,7 @@ pub fn update(msg: Msg, model: &mut Model, orders: &mut impl Orders<Msg>) {
match msg { match msg {
Msg::Query(msg) => process_query_messages(msg, model, orders), Msg::Query(msg) => process_query_messages(msg, model, orders),
Msg::Edit(msg) => process_edit_messages(msg, model, orders), Msg::Edit(msg) => process_edit_messages(msg, model, orders),
Msg::ClearAll => { Msg::ClearAll => clear_all(model),
clear_all(model)
}
Msg::SetMessage(msg) => { Msg::SetMessage(msg) => {
clear_all(model); clear_all(model);
model.last_message = Some(Status::Error(Message { message: msg })); model.last_message = Some(Status::Error(Message { message: msg }));
@ -400,7 +397,6 @@ pub fn view(model: &Model) -> Node<Msg> {
} else { } else {
section![] section![]
}, },
// display the list of links // display the list of links
table![ table![
// Add the headlines // Add the headlines
@ -410,7 +406,6 @@ pub fn view(model: &Model) -> Node<Msg> {
// Add all the content lines // Add all the content lines
model.links.iter().map(view_link) model.links.iter().map(view_link)
], ],
// A fetch button - this should not be needed and will be removed in future. // A fetch button - this should not be needed and will be removed in future.
button![ button![
ev(Ev::Click, |_| Msg::Query(QueryMsg::Fetch)), ev(Ev::Click, |_| Msg::Query(QueryMsg::Fetch)),
@ -616,7 +611,6 @@ fn edit_or_create_link<F: Fn(&str) -> String>(l: &RefCell<LinkDelta>, t: F) -> N
] ]
} }
/// a close button for dialogs /// a close button for dialogs
fn close_button() -> Node<Msg> { fn close_button() -> Node<Msg> {
div![ div![

View File

@ -263,13 +263,15 @@ fn save_user(user: UserDelta, orders: &mut impl Orders<Msg>) {
Msg::Edit(UserEditMsg::FailedToCreateUser) Msg::Edit(UserEditMsg::FailedToCreateUser)
); );
// check for the status // check for the status
let response = unwrap_or_return!(response let response = unwrap_or_return!(
.check_status(), response.check_status(),
Msg::Edit(UserEditMsg::FailedToCreateUser)); Msg::Edit(UserEditMsg::FailedToCreateUser)
);
// deserialize the response // deserialize the response
let message: Status = unwrap_or_return!(response let message: Status = unwrap_or_return!(
.json() response.json().await,
.await, Msg::Edit(UserEditMsg::FailedToCreateUser)); Msg::Edit(UserEditMsg::FailedToCreateUser)
);
Msg::Edit(UserEditMsg::UserCreated(message)) Msg::Edit(UserEditMsg::UserCreated(message))
}); });
@ -300,7 +302,6 @@ pub fn view(model: &Model) -> Node<Msg> {
} else { } else {
section![] section![]
}, },
// display the table with users // display the table with users
table![ table![
// Column Headlines // Column Headlines

View File

@ -2,7 +2,6 @@ pub mod home;
pub mod list_links; pub mod list_links;
pub mod list_users; pub mod list_users;
/// Unwrap a result and return it's content, or return from the function with another expression. /// Unwrap a result and return it's content, or return from the function with another expression.
#[macro_export] #[macro_export]
macro_rules! unwrap_or_return { macro_rules! unwrap_or_return {
@ -22,7 +21,8 @@ macro_rules! unwrap_or_send {
Some(x) => x, Some(x) => x,
None => { None => {
$orders.send_msg($result); $orders.send_msg($result);
return;}, return;
}
} }
}; };
} }

View File

@ -378,10 +378,7 @@ pub async fn webservice(
"/get_logged_user/", "/get_logged_user/",
web::post().to(views::get_logged_user_json), web::post().to(views::get_logged_user_json),
) )
.route( .route("/login_user/", web::post().to(views::process_login_json)),
"/login_user/",
web::post().to(views::process_login_json),
),
) )
// login to the admin area // login to the admin area
.route("/login/", web::get().to(views::login)) .route("/login/", web::get().to(views::login))

View File

@ -174,9 +174,7 @@ pub async fn get_logged_user_json(
) -> Result<HttpResponse, ServerError> { ) -> Result<HttpResponse, ServerError> {
let user = authenticate(&id, &config).await?; let user = authenticate(&id, &config).await?;
match user { match user {
Role::NotAuthenticated | Role::Disabled => { Role::NotAuthenticated | Role::Disabled => Ok(HttpResponse::Unauthorized().finish()),
Ok(HttpResponse::Unauthorized().finish())
}
Role::Regular { user } | Role::Admin { user } => Ok(HttpResponse::Ok().json2(&user)), Role::Regular { user } | Role::Admin { user } => Ok(HttpResponse::Ok().json2(&user)),
} }
} }

View File

@ -95,13 +95,13 @@ pub enum Loadable<T> {
Loading, Loading,
} }
impl<T> Deref for Loadable<T>{ impl<T> Deref for Loadable<T> {
type Target = Option<T>; type Target = Option<T>;
fn deref(&self) -> &Self::Target { fn deref(&self) -> &Self::Target {
match self { match self {
Loadable::Data(t) => t, Loadable::Data(t) => t,
Loadable::Loading => &None Loadable::Loading => &None,
} }
} }
} }