From 8d447175886773bd5b45fda1b37341398ed0394b Mon Sep 17 00:00:00 2001 From: Valentin Tolmer Date: Wed, 8 Mar 2023 14:44:08 +0100 Subject: [PATCH] app: replace ConsoleService with gloo_console --- Cargo.lock | 27 +++++++++++++++++++++++++ app/Cargo.toml | 1 + app/src/components/app.rs | 12 +++++------ app/src/components/change_password.rs | 8 +++----- app/src/components/create_group.rs | 4 ++-- app/src/components/create_user.rs | 4 ++-- app/src/components/login.rs | 7 ++++--- app/src/components/user_details_form.rs | 10 +-------- app/src/infra/common_component.rs | 6 +++--- 9 files changed, 48 insertions(+), 31 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 29a0199..591cc19 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1534,6 +1534,19 @@ dependencies = [ "gloo-timers", ] +[[package]] +name = "gloo-console" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "82b7ce3c05debe147233596904981848862b068862e9ec3e34be446077190d3f" +dependencies = [ + "gloo-utils", + "js-sys", + "serde", + "wasm-bindgen", + "web-sys", +] + [[package]] name = "gloo-console-timer" version = "0.1.0" @@ -1575,6 +1588,19 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "gloo-utils" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a8e8fc851e9c7b9852508bc6e3f690f452f474417e8545ec9857b7f7377036b5" +dependencies = [ + "js-sys", + "serde", + "serde_json", + "wasm-bindgen", + "web-sys", +] + [[package]] name = "graphql-introspection-query" version = "0.2.0" @@ -2361,6 +2387,7 @@ dependencies = [ "anyhow", "base64 0.13.1", "chrono", + "gloo-console", "graphql_client 0.10.0", "http", "image", diff --git a/app/Cargo.toml b/app/Cargo.toml index 65fa335..c5573f1 100644 --- a/app/Cargo.toml +++ b/app/Cargo.toml @@ -8,6 +8,7 @@ include = ["src/**/*", "queries/**/*", "Cargo.toml", "../schema.graphql"] [dependencies] anyhow = "1" base64 = "0.13" +gloo-console = "0.2.3" graphql_client = "0.10" http = "0.2" jwt = "0.13" diff --git a/app/src/components/app.rs b/app/src/components/app.rs index 69ef406..ed0d387 100644 --- a/app/src/components/app.rs +++ b/app/src/components/app.rs @@ -16,10 +16,8 @@ use crate::{ infra::{api::HostService, cookies::get_cookie}, }; -use yew::{ - prelude::*, - services::{fetch::FetchTask, ConsoleService}, -}; +use gloo_console::error; +use yew::{prelude::*, services::fetch::FetchTask}; use yew_router::{ agent::{RouteAgentDispatcher, RouteRequest}, route::Route, @@ -51,14 +49,14 @@ impl Component for App { link, user_info: get_cookie("user_id") .unwrap_or_else(|e| { - ConsoleService::error(&e.to_string()); + error!(&e.to_string()); None }) .and_then(|u| { get_cookie("is_admin") .map(|so| so.map(|s| (u, s == "true"))) .unwrap_or_else(|e| { - ConsoleService::error(&e.to_string()); + error!(&e.to_string()); None }) }), @@ -105,7 +103,7 @@ impl Component for App { Msg::PasswordResetProbeFinished(Err(err)) => { self.task = None; self.password_reset_enabled = Some(false); - ConsoleService::error(&format!( + error!(&format!( "Could not probe for password reset support: {err:#}" )); } diff --git a/app/src/components/change_password.rs b/app/src/components/change_password.rs index 6b08822..1e65046 100644 --- a/app/src/components/change_password.rs +++ b/app/src/components/change_password.rs @@ -6,9 +6,10 @@ use crate::{ }, }; use anyhow::{anyhow, bail, Context, Result}; +use gloo_console::error; use lldap_auth::*; use validator_derive::Validate; -use yew::{prelude::*, services::ConsoleService}; +use yew::prelude::*; use yew_form::Form; use yew_form_derive::Model; use yew_router::{ @@ -114,10 +115,7 @@ impl CommonComponent for ChangePasswordForm { |e| { // Common error, we want to print a full error to the console but only a // simple one to the user. - ConsoleService::error(&format!( - "Invalid username or password: {}", - e - )); + error!(&format!("Invalid username or password: {}", e)); anyhow!("Invalid username or password") }, )?; diff --git a/app/src/components/create_group.rs b/app/src/components/create_group.rs index 8dcafec..c1b5f05 100644 --- a/app/src/components/create_group.rs +++ b/app/src/components/create_group.rs @@ -3,10 +3,10 @@ use crate::{ infra::common_component::{CommonComponent, CommonComponentParts}, }; use anyhow::{bail, Result}; +use gloo_console::log; use graphql_client::GraphQLQuery; use validator_derive::Validate; use yew::prelude::*; -use yew::services::ConsoleService; use yew_form_derive::Model; use yew_router::{ agent::{RouteAgentDispatcher, RouteRequest}, @@ -60,7 +60,7 @@ impl CommonComponent for CreateGroupForm { Ok(true) } Msg::CreateGroupResponse(response) => { - ConsoleService::log(&format!( + log!(&format!( "Created group '{}'", &response?.create_group.display_name )); diff --git a/app/src/components/create_user.rs b/app/src/components/create_user.rs index ba39b97..7ee0fab 100644 --- a/app/src/components/create_user.rs +++ b/app/src/components/create_user.rs @@ -6,11 +6,11 @@ use crate::{ }, }; use anyhow::{bail, Context, Result}; +use gloo_console::log; use graphql_client::GraphQLQuery; use lldap_auth::{opaque, registration}; use validator_derive::Validate; use yew::prelude::*; -use yew::services::ConsoleService; use yew_form_derive::Model; use yew_router::{ agent::{RouteAgentDispatcher, RouteRequest}, @@ -102,7 +102,7 @@ impl CommonComponent for CreateUserForm { Msg::CreateUserResponse(r) => { match r { Err(e) => return Err(e), - Ok(r) => ConsoleService::log(&format!( + Ok(r) => log!(&format!( "Created user '{}' at '{}'", &r.create_user.id, &r.create_user.creation_date )), diff --git a/app/src/components/login.rs b/app/src/components/login.rs index 84ffc8f..932eaa9 100644 --- a/app/src/components/login.rs +++ b/app/src/components/login.rs @@ -6,9 +6,10 @@ use crate::{ }, }; use anyhow::{anyhow, bail, Context, Result}; +use gloo_console::{debug, error}; use lldap_auth::*; use validator_derive::Validate; -use yew::{prelude::*, services::ConsoleService}; +use yew::prelude::*; use yew_form::Form; use yew_form_derive::Model; @@ -77,7 +78,7 @@ impl CommonComponent for LoginForm { Err(e) => { // Common error, we want to print a full error to the console but only a // simple one to the user. - ConsoleService::error(&format!("Invalid username or password: {}", e)); + error!(&format!("Invalid username or password: {}", e)); self.common.error = Some(anyhow!("Invalid username or password")); self.common.cancel_task(); return Ok(true); @@ -132,7 +133,7 @@ impl Component for LoginForm { app.common .call_backend(HostService::refresh, (), Msg::AuthenticationRefreshResponse) { - ConsoleService::debug(&format!("Could not refresh auth: {}", e)); + debug!(&format!("Could not refresh auth: {}", e)); app.refreshing = false; } app diff --git a/app/src/components/user_details_form.rs b/app/src/components/user_details_form.rs index 0058caf..a320e77 100644 --- a/app/src/components/user_details_form.rs +++ b/app/src/components/user_details_form.rs @@ -8,7 +8,7 @@ use anyhow::{bail, Error, Result}; use graphql_client::GraphQLQuery; use validator_derive::Validate; use wasm_bindgen::JsCast; -use yew::{prelude::*, services::ConsoleService}; +use yew::prelude::*; use yew_form_derive::Model; #[derive(PartialEq, Eq, Clone, Default)] @@ -96,11 +96,8 @@ impl CommonComponent for UserDetailsForm { .expect("Form field avatarInput should be present") .dyn_into::() .expect("Should be an HtmlInputElement"); - ConsoleService::log("Form update"); if let Some(files) = input.files() { - ConsoleService::log("Got file list"); if files.length() > 0 { - ConsoleService::log("Got a file"); let new_avatar = JsFile { file: files.item(0), contents: None, @@ -339,11 +336,9 @@ impl Component for UserDetailsForm { impl UserDetailsForm { fn submit_user_update_form(&mut self) -> Result { - ConsoleService::log("Submit"); if !self.form.validate() { bail!("Invalid inputs"); } - ConsoleService::log("Valid inputs"); if let JsFile { file: Some(_), contents: None, @@ -351,7 +346,6 @@ impl UserDetailsForm { { bail!("Image file hasn't finished loading, try again"); } - ConsoleService::log("File is correctly loaded"); let base_user = &self.common.user; let mut user_input = update_user::UpdateUserInput { id: self.common.user.id.clone(), @@ -379,11 +373,9 @@ impl UserDetailsForm { user_input.avatar = maybe_to_base64(&self.avatar)?; // Nothing changed. if user_input == default_user_input { - ConsoleService::log("No changes"); return Ok(false); } let req = update_user::Variables { user: user_input }; - ConsoleService::log("Querying"); self.common.call_graphql::( req, Msg::UserUpdated, diff --git a/app/src/infra/common_component.rs b/app/src/infra/common_component.rs index bb2c68e..2bb045c 100644 --- a/app/src/infra/common_component.rs +++ b/app/src/infra/common_component.rs @@ -23,13 +23,13 @@ use crate::infra::api::HostService; use anyhow::{Error, Result}; +use gloo_console::{error, log}; use graphql_client::GraphQLQuery; use yew::{ prelude::*, services::{ fetch::FetchTask, reader::{FileData, ReaderService, ReaderTask}, - ConsoleService, }, }; use yewtil::NeqAssign; @@ -100,7 +100,7 @@ impl> CommonComponentParts { com.mut_common().error = None; match com.handle_msg(msg) { Err(e) => { - ConsoleService::error(&e.to_string()); + error!(&e.to_string()); com.mut_common().error = Some(e); com.mut_common().cancel_task(); true @@ -178,7 +178,7 @@ impl> CommonComponentParts { error_message, ) .map_err::<(), _>(|e| { - ConsoleService::log(&e.to_string()); + log!(&e.to_string()); self.error = Some(e); }) .ok()