Formatting...
This commit is contained in:
parent
26142084f6
commit
50da81889e
@ -108,4 +108,3 @@ impl Lang {
|
|||||||
bundle
|
bundle
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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 {
|
||||||
unwrap_or_return!(fetch(request).await, Msg::GetLoggedUser);
|
let request = Request::new("/admin/logout/");
|
||||||
Msg::NotAuthenticated});
|
unwrap_or_return!(fetch(request).await, Msg::GetLoggedUser);
|
||||||
|
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)
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
@ -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.
|
||||||
@ -29,27 +28,27 @@ pub fn init(mut url: Url, orders: &mut impl Orders<Msg>, i18n: I18n) -> Model {
|
|||||||
Some("create_link") => Some(RefCell::new(LinkDelta::default())),
|
Some("create_link") => Some(RefCell::new(LinkDelta::default())),
|
||||||
None | Some(_) => None,
|
None | Some(_) => None,
|
||||||
};
|
};
|
||||||
|
|
||||||
Model {
|
Model {
|
||||||
links: Vec::new(), // will contain the links to display
|
links: Vec::new(), // will contain the links to display
|
||||||
i18n, // to translate
|
i18n, // to translate
|
||||||
formconfig: LinkRequestForm::default(), // when requesting links the form is stored here
|
formconfig: LinkRequestForm::default(), // when requesting links the form is stored here
|
||||||
inputs: EnumMap::default(), // the input fields for the searches
|
inputs: EnumMap::default(), // the input fields for the searches
|
||||||
edit_link, // if set this will trigger a link edit dialog
|
edit_link, // if set this will trigger a link edit dialog
|
||||||
last_message: None, // if a message to the user is recieved
|
last_message: None, // if a message to the user is recieved
|
||||||
question: None, // some operations should be confirmed
|
question: None, // some operations should be confirmed
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Model {
|
pub struct Model {
|
||||||
links: Vec<FullLink>, // will contain the links to display
|
links: Vec<FullLink>, // will contain the links to display
|
||||||
i18n: I18n, // to translate
|
i18n: I18n, // to translate
|
||||||
formconfig: LinkRequestForm, // when requesting links the form is stored here
|
formconfig: LinkRequestForm, // when requesting links the form is stored here
|
||||||
inputs: EnumMap<LinkOverviewColumns, FilterInput>, // the input fields for the searches
|
inputs: EnumMap<LinkOverviewColumns, FilterInput>, // the input fields for the searches
|
||||||
edit_link: Option<RefCell<LinkDelta>>, // if set this will trigger a link edit dialog
|
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
|
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)]
|
#[derive(Default, Debug, Clone)]
|
||||||
@ -59,9 +58,9 @@ struct FilterInput {
|
|||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub enum Msg {
|
pub enum Msg {
|
||||||
Query(QueryMsg), // Messages related to querying links
|
Query(QueryMsg), // Messages related to querying links
|
||||||
Edit(EditMsg), // Messages related to editing links
|
Edit(EditMsg), // Messages related to editing links
|
||||||
ClearAll, // Clear all messages
|
ClearAll, // Clear all messages
|
||||||
SetMessage(String), // Set a message to the user
|
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 {
|
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![
|
||||||
@ -624,4 +618,4 @@ fn close_button() -> Node<Msg> {
|
|||||||
a!["\u{d7}"],
|
a!["\u{d7}"],
|
||||||
ev(Ev::Click, |_| Msg::ClearAll)
|
ev(Ev::Click, |_| Msg::ClearAll)
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
@ -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
|
||||||
@ -485,4 +486,4 @@ fn close_button() -> Node<Msg> {
|
|||||||
a!["\u{d7}"],
|
a!["\u{d7}"],
|
||||||
ev(Ev::Click, |_| Msg::ClearAll)
|
ev(Ev::Click, |_| Msg::ClearAll)
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
@ -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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -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))
|
||||||
|
@ -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)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user