documentation updates

This commit is contained in:
2021-08-12 15:48:02 +02:00
committed by dietrich
parent 48a376f3bd
commit bf6bac847b
10 changed files with 70 additions and 16 deletions
+14
View File
@@ -60,12 +60,14 @@ impl Model {
}
}
/// The input fields of the login dialog.
#[derive(Default, Debug)]
struct LoginForm {
username: ElRef<web_sys::HtmlInputElement>,
password: ElRef<web_sys::HtmlInputElement>,
}
/// All information regarding the current location
#[derive(Debug)]
struct Location {
host: String,
@@ -84,6 +86,7 @@ impl Location {
}
}
/// Get the url from the address bar.
#[must_use]
pub fn get_host() -> String {
window()
@@ -92,6 +95,9 @@ pub fn get_host() -> String {
.expect("Failed to extract the host of the url")
}
/// The pages:
/// * Home for listing of links
/// * ListUsers for listing of users
#[derive(Debug)]
enum Page {
Home(pages::list_links::Model),
@@ -137,6 +143,8 @@ impl Page {
// ------ ------
// Update
// ------ ------
/// The messages regarding authentication and settings.
#[derive(Clone)]
pub enum Msg {
UrlChanged(subs::UrlChanged),
@@ -154,6 +162,7 @@ pub enum Msg {
LanguageChanged(Lang),
}
/// react to settings and authentication changes.
fn update(msg: Msg, model: &mut Model, orders: &mut impl Orders<Msg>) {
match msg {
Msg::UrlChanged(url) => {
@@ -222,6 +231,7 @@ fn update(msg: Msg, model: &mut Model, orders: &mut impl Orders<Msg>) {
}
}
/// switch the language
fn change_language(l: Lang, orders: &mut impl Orders<Msg>) {
orders.perform_cmd(async move {
// create request
@@ -241,6 +251,7 @@ fn change_language(l: Lang, orders: &mut impl Orders<Msg>) {
});
}
/// logout on the server
fn logout(orders: &mut impl Orders<Msg>) {
orders.perform_cmd(async {
let request = Request::new("/admin/logout/");
@@ -249,6 +260,7 @@ fn logout(orders: &mut impl Orders<Msg>) {
});
}
/// login using username and password
fn login_user(model: &mut Model, orders: &mut impl Orders<Msg>) {
model.user = Loadable::Loading;
let data = model.login_data.clone();
@@ -272,6 +284,7 @@ fn login_user(model: &mut Model, orders: &mut impl Orders<Msg>) {
});
}
/// to create urls for different subpages
pub struct Urls<'a> {
base_url: std::borrow::Cow<'a, Url>,
}
@@ -356,6 +369,7 @@ fn view_content(page: &Page, url: &Url, user: &User) -> Node<Msg> {
]
}
/// If not logged in render the login form
fn view_login(lang: &I18n, model: &Model) -> Node<Msg> {
let t = move |key: &str| lang.translate(key, None);
-6
View File
@@ -1,6 +0,0 @@
use seed::{div, prelude::*};
#[must_use]
pub fn view<Ms>() -> Node<Ms> {
div!["List Links"]
}
+14 -2
View File
@@ -1,3 +1,4 @@
//! List all the links the own links editable or if an admin is logged in all links editable.
use enum_map::EnumMap;
use fluent::fluent_args;
use image::{DynamicImage, ImageOutputFormat, Luma};
@@ -60,6 +61,7 @@ impl Model {
}
}
/// There can allways be only one dialog.
#[derive(Debug, Clone)]
enum Dialog {
EditLink {
@@ -71,6 +73,7 @@ enum Dialog {
None,
}
/// A qr-code with `new` for creating a blob url and `Drop` for releasing the blob url.
#[derive(Debug, Clone)]
pub struct QrGuard {
svg: String,
@@ -98,17 +101,20 @@ impl QrGuard {
}
impl Drop for QrGuard {
/// release the blob url
fn drop(&mut self) {
web_sys::Url::revoke_object_url(&self.url)
.unwrap_or_else(|_| (log!("Failed to release url!")));
}
}
/// Filter one column of the row.
#[derive(Default, Debug, Clone)]
struct FilterInput {
filter_input: ElRef<web_sys::HtmlInputElement>,
}
/// A message can either edit or query. (or set a dialog)
#[derive(Clone)]
pub enum Msg {
Query(QueryMsg), // Messages related to querying links
@@ -117,6 +123,7 @@ pub enum Msg {
SetMessage(String), // Set a message to the user
}
/// All the messages related to requesting information from the server.
#[derive(Clone)]
pub enum QueryMsg {
Fetch,
@@ -127,7 +134,8 @@ pub enum QueryMsg {
TargetFilterChanged(String),
AuthorFilterChanged(String),
}
/// All the messages on link editing
/// All the messages on storing information on the server.
#[derive(Debug, Clone)]
pub enum EditMsg {
EditSelected(LinkDelta),
@@ -151,7 +159,7 @@ fn clear_all(model: &mut Model) {
model.dialog = Dialog::None;
}
/// React to environment changes
/// Split the update to Query updates and Edit updates.
pub fn update(msg: Msg, model: &mut Model, orders: &mut impl Orders<Msg>) {
match msg {
Msg::Query(msg) => process_query_messages(msg, model, orders),
@@ -706,10 +714,12 @@ fn edit_or_create_link<F: Fn(&str) -> String>(
]
}
/// generate a qr-code for a code
fn generate_qr_from_code(code: &str) -> String {
generate_qr_from_link(&format!("https://{}/{}", get_host(), code))
}
/// generate a svg qr-code for a url
fn generate_qr_from_link(url: &str) -> String {
if let Ok(qr) = QrCode::with_error_correction_level(&url, qrcode::EcLevel::L) {
let svg = qr
@@ -720,6 +730,7 @@ fn generate_qr_from_link(url: &str) -> String {
.build();
svg
} else {
// should never (only on very huge codes) happen.
"".to_string()
}
}
@@ -733,6 +744,7 @@ fn close_button() -> Node<Msg> {
]
}
/// generate a png qr-code for a url
fn generate_qr_png(code: &str) -> Vec<u8> {
let qr = QrCode::with_error_correction_level(
&format!("http://{}/{}", get_host(), code),
+12 -2
View File
@@ -97,6 +97,8 @@ pub enum UserEditMsg {
/*
* update
*/
/// Split the update to Query updates and Edit updates
pub fn update(msg: Msg, model: &mut Model, orders: &mut impl Orders<Msg>) {
match msg {
Msg::Query(msg) => process_query_messages(msg, model, orders),
@@ -107,7 +109,7 @@ pub fn update(msg: Msg, model: &mut Model, orders: &mut impl Orders<Msg>) {
}
}
/// Update all
/// Process all updates related to getting data from the server.
pub fn process_query_messages(msg: UserQueryMsg, model: &mut Model, orders: &mut impl Orders<Msg>) {
match msg {
UserQueryMsg::Fetch => {
@@ -170,6 +172,7 @@ pub fn process_query_messages(msg: UserQueryMsg, model: &mut Model, orders: &mut
}
}
/// Load the list of users from the server.
fn load_users(data: UserRequestForm, orders: &mut impl Orders<Msg>) {
orders.perform_cmd(async {
let data = data;
@@ -200,6 +203,7 @@ fn load_users(data: UserRequestForm, orders: &mut impl Orders<Msg>) {
});
}
/// Process all the messages related to editing users.
pub fn process_user_edit_messages(
msg: UserEditMsg,
model: &mut Model,
@@ -233,7 +237,7 @@ pub fn process_user_edit_messages(
let data = model
.user_edit
.take()
.expect("A user should allways be there on save"); // complicated way to move into the closure
.expect("A user should allways be there on save");
log!("Saving User: ", &data.username);
save_user(data, orders);
}
@@ -251,6 +255,7 @@ pub fn process_user_edit_messages(
}
}
/// Update the role of a user - this toggles between admin and regular.
fn update_privileges(user: UserDelta, orders: &mut impl Orders<Msg>) {
orders.perform_cmd(async {
let data = user;
@@ -281,6 +286,7 @@ fn update_privileges(user: UserDelta, orders: &mut impl Orders<Msg>) {
});
}
/// Save a new user or edit an existing user
fn save_user(user: UserDelta, orders: &mut impl Orders<Msg>) {
orders.perform_cmd(async {
let data = user;
@@ -360,6 +366,7 @@ pub fn view(model: &Model, logged_in_user: &User) -> Node<Msg> {
]
}
/// View the headlines of the table
fn view_user_table_head<F: Fn(&str) -> String>(t: F) -> Node<Msg> {
tr![
th![
@@ -384,6 +391,7 @@ fn view_user_table_head<F: Fn(&str) -> String>(t: F) -> Node<Msg> {
]
}
/// Display the filterboxes below the headlines
fn view_user_table_filter_input<F: Fn(&str) -> String>(model: &Model, t: F) -> Node<Msg> {
tr![
C!["filters"],
@@ -426,6 +434,7 @@ fn view_user_table_filter_input<F: Fn(&str) -> String>(model: &Model, t: F) -> N
]
}
/// Display one user-line of the table
fn view_user<F: Fn(&str) -> String>(l: &User, logged_in_user: &User, t: F) -> Node<Msg> {
let user = UserDelta::from(l.clone());
tr![
@@ -471,6 +480,7 @@ fn view_user<F: Fn(&str) -> String>(l: &User, logged_in_user: &User, t: F) -> No
]
}
/// display the edit and create dialog
fn edit_or_create_user<F: Fn(&str) -> String>(l: UserDelta, t: F) -> Node<Msg> {
let user = l;
let headline: Node<Msg> = match &user.role {
-1
View File
@@ -1,5 +1,4 @@
//! Containing the individual pages for the admin app so far one to list the links and one to list the users.
pub mod home;
pub mod list_links;
pub mod list_users;