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
}
}

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::NotAuthenticated => {if model.user.is_some() {model.user = Loadable::Data(None); logout(orders)}},
Msg::Login => {login_user(model, orders)}
Msg::NotAuthenticated => {
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::PasswordChanged(s) => model.login_data.password = s,
}
}
fn logout(orders: &mut impl Orders<Msg>) {
orders.perform_cmd(async {let request = Request::new("/admin/logout/");
unwrap_or_return!(fetch(request).await, Msg::GetLoggedUser);
Msg::NotAuthenticated});
orders.perform_cmd(async {
let request = Request::new("/admin/logout/");
unwrap_or_return!(fetch(request).await, Msg::GetLoggedUser);
Msg::NotAuthenticated
});
}
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)
]
} 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};
/// Setup the page
pub fn init(mut url: Url, orders: &mut impl Orders<Msg>, i18n: I18n) -> Model {
// fetch the links to fill the list.
@ -31,25 +30,25 @@ pub fn init(mut url: Url, orders: &mut impl Orders<Msg>, i18n: I18n) -> Model {
};
Model {
links: Vec::new(), // will contain the links to display
i18n, // to translate
links: Vec::new(), // will contain the links to display
i18n, // to translate
formconfig: LinkRequestForm::default(), // when requesting links the form is stored here
inputs: EnumMap::default(), // the input fields for the searches
edit_link, // if set this will trigger a link edit dialog
last_message: None, // if a message to the user is recieved
question: None, // some operations should be confirmed
inputs: EnumMap::default(), // the input fields for the searches
edit_link, // if set this will trigger a link edit dialog
last_message: None, // if a message to the user is recieved
question: None, // some operations should be confirmed
}
}
#[derive(Debug)]
pub struct Model {
links: Vec<FullLink>, // will contain the links to display
i18n: I18n, // to translate
links: Vec<FullLink>, // will contain the links to display
i18n: I18n, // to translate
formconfig: LinkRequestForm, // when requesting links the form is stored here
inputs: EnumMap<LinkOverviewColumns, FilterInput>, // the input fields for the searches
edit_link: Option<RefCell<LinkDelta>>, // if set this will trigger a link edit dialog
last_message: Option<Status>, // if a message to the user is recieved
question: Option<EditMsg>, // some operations should be confirmed
question: Option<EditMsg>, // some operations should be confirmed
}
#[derive(Default, Debug, Clone)]
@ -59,9 +58,9 @@ struct FilterInput {
#[derive(Clone)]
pub enum Msg {
Query(QueryMsg), // Messages related to querying links
Edit(EditMsg), // Messages related to editing links
ClearAll, // Clear all messages
Query(QueryMsg), // Messages related to querying links
Edit(EditMsg), // Messages related to editing links
ClearAll, // Clear all messages
SetMessage(String), // Set a message to the user
}
@ -104,9 +103,7 @@ pub fn update(msg: Msg, model: &mut Model, orders: &mut impl Orders<Msg>) {
match msg {
Msg::Query(msg) => process_query_messages(msg, model, orders),
Msg::Edit(msg) => process_edit_messages(msg, model, orders),
Msg::ClearAll => {
clear_all(model)
}
Msg::ClearAll => clear_all(model),
Msg::SetMessage(msg) => {
clear_all(model);
model.last_message = Some(Status::Error(Message { message: msg }));
@ -400,7 +397,6 @@ pub fn view(model: &Model) -> Node<Msg> {
} else {
section![]
},
// display the list of links
table![
// Add the headlines
@ -410,7 +406,6 @@ pub fn view(model: &Model) -> Node<Msg> {
// Add all the content lines
model.links.iter().map(view_link)
],
// A fetch button - this should not be needed and will be removed in future.
button![
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
fn close_button() -> Node<Msg> {
div![

View File

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

View File

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

View File

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

View File

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

View File

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