From f44e8b76596be3a32167797b9820f171eb6da34c Mon Sep 17 00:00:00 2001 From: Valentin Tolmer Date: Wed, 8 Mar 2023 14:27:47 +0100 Subject: [PATCH 1/3] app: wrap template arguments in braces To prepare for the migration to yew 1.19 --- app/src/components/add_group_member.rs | 9 +++--- app/src/components/add_user_to_group.rs | 9 +++--- app/src/components/app.rs | 25 ++++++++------- app/src/components/change_password.rs | 19 +++++------ app/src/components/create_group.rs | 9 +++--- app/src/components/create_user.rs | 33 ++++++++++---------- app/src/components/delete_group.rs | 16 +++++----- app/src/components/delete_user.rs | 16 +++++----- app/src/components/group_details.rs | 18 +++++------ app/src/components/group_table.rs | 10 +++--- app/src/components/login.rs | 15 ++++----- app/src/components/logout.rs | 3 +- app/src/components/remove_user_from_group.rs | 5 +-- app/src/components/reset_password_step1.rs | 13 ++++---- app/src/components/reset_password_step2.rs | 17 +++++----- app/src/components/select.rs | 8 ++--- app/src/components/user_details.rs | 27 ++++++++-------- app/src/components/user_details_form.rs | 25 ++++++++------- app/src/components/user_table.rs | 11 ++++--- 19 files changed, 153 insertions(+), 135 deletions(-) diff --git a/app/src/components/add_group_member.rs b/app/src/components/add_group_member.rs index 61b9938..0ac4e23 100644 --- a/app/src/components/add_group_member.rs +++ b/app/src/components/add_group_member.rs @@ -149,18 +149,19 @@ impl Component for AddGroupMemberComponent { } fn view(&self) -> Html { + let link = &self.common; if let Some(user_list) = &self.user_list { let to_add_user_list = self.get_selectable_user_list(user_list); #[allow(unused_braces)] let make_select_option = |user: User| { html_nested! { - + } }; html! {
- { to_add_user_list .into_iter() @@ -172,8 +173,8 @@ impl Component for AddGroupMemberComponent {
diff --git a/app/src/components/add_user_to_group.rs b/app/src/components/add_user_to_group.rs index 3d7d3f0..1130ffb 100644 --- a/app/src/components/add_user_to_group.rs +++ b/app/src/components/add_user_to_group.rs @@ -162,18 +162,19 @@ impl Component for AddUserToGroupComponent { } fn view(&self) -> Html { + let link = &self.common; if let Some(group_list) = &self.group_list { let to_add_group_list = self.get_selectable_group_list(group_list); #[allow(unused_braces)] let make_select_option = |group: Group| { html_nested! { - + } }; html! {
- { to_add_group_list .into_iter() @@ -185,8 +186,8 @@ impl Component for AddUserToGroupComponent {
diff --git a/app/src/components/app.rs b/app/src/components/app.rs index e20fed7..69ef406 100644 --- a/app/src/components/app.rs +++ b/app/src/components/app.rs @@ -128,7 +128,7 @@ impl Component for App {
- render = Router::render(move |s| Self::dispatch_route(s, &link, is_admin, password_reset_enabled)) + render={Router::render(move |s| Self::dispatch_route(s, &link, is_admin, password_reset_enabled))} />
@@ -198,7 +198,7 @@ impl App { ) -> Html { match switch { AppRoute::Login => html! { - + }, AppRoute::CreateUser => html! { @@ -206,7 +206,7 @@ impl App { AppRoute::Index | AppRoute::ListUsers => html! {
- + {"Create a user"} @@ -218,20 +218,20 @@ impl App { AppRoute::ListGroups => html! {
- + {"Create a group"}
}, AppRoute::GroupDetails(group_id) => html! { - + }, AppRoute::UserDetails(username) => html! { - + }, AppRoute::ChangePassword(username) => html! { - + }, AppRoute::StartResetPassword => match password_reset_enabled { Some(true) => html! { }, @@ -242,7 +242,7 @@ impl App { None => html! {}, }, AppRoute::FinishResetPassword(token) => match password_reset_enabled { - Some(true) => html! { }, + Some(true) => html! { }, Some(false) => { App::dispatch_route(AppRoute::Login, link, is_admin, password_reset_enabled) } @@ -252,6 +252,7 @@ impl App { } fn view_banner(&self) -> Html { + let link = &self.link; html! {
@@ -266,7 +267,7 @@ impl App {
  • + route={AppRoute::ListUsers}> {"Users"} @@ -274,7 +275,7 @@ impl App {
  • + route={AppRoute::ListGroups}> {"Groups"} @@ -312,13 +313,13 @@ impl App {
  • + route={AppRoute::UserDetails(user_id.clone())}> {"View details"}
  • - +
  • diff --git a/app/src/components/change_password.rs b/app/src/components/change_password.rs index dd43369..6b08822 100644 --- a/app/src/components/change_password.rs +++ b/app/src/components/change_password.rs @@ -212,6 +212,7 @@ impl Component for ChangePasswordForm { fn view(&self) -> Html { let is_admin = self.common.is_admin; + let link = &self.common; type Field = yew_form::Field; html! { <> @@ -239,14 +240,14 @@ impl Component for ChangePasswordForm {
    + oninput={link.callback(|_| Msg::FormUpdate)} />
    {&self.form.field_message("old_password")}
    @@ -262,14 +263,14 @@ impl Component for ChangePasswordForm {
    + oninput={link.callback(|_| Msg::FormUpdate)} />
    {&self.form.field_message("password")}
    @@ -284,14 +285,14 @@ impl Component for ChangePasswordForm {
    + oninput={link.callback(|_| Msg::FormUpdate)} />
    {&self.form.field_message("confirm_password")}
    @@ -301,14 +302,14 @@ impl Component for ChangePasswordForm { + route={AppRoute::UserDetails(self.common.username.clone())}> {"Back"} diff --git a/app/src/components/create_group.rs b/app/src/components/create_group.rs index d3e9b45..8dcafec 100644 --- a/app/src/components/create_group.rs +++ b/app/src/components/create_group.rs @@ -97,6 +97,7 @@ impl Component for CreateGroupForm { } fn view(&self) -> Html { + let link = &self.common; type Field = yew_form::Field; html! {
    @@ -113,13 +114,13 @@ impl Component for CreateGroupForm {
    + oninput={link.callback(|_| Msg::Update)} />
    {&self.form.field_message("groupname")}
    @@ -129,8 +130,8 @@ impl Component for CreateGroupForm { diff --git a/app/src/components/create_user.rs b/app/src/components/create_user.rs index b7ece1c..ba39b97 100644 --- a/app/src/components/create_user.rs +++ b/app/src/components/create_user.rs @@ -190,6 +190,7 @@ impl Component for CreateUserForm { } fn view(&self) -> Html { + let link = &self.common; type Field = yew_form::Field; html! {
    @@ -206,13 +207,13 @@ impl Component for CreateUserForm {
    + oninput={link.callback(|_| Msg::Update)} />
    {&self.form.field_message("username")}
    @@ -227,14 +228,14 @@ impl Component for CreateUserForm {
    + oninput={link.callback(|_| Msg::Update)} />
    {&self.form.field_message("email")}
    @@ -247,13 +248,13 @@ impl Component for CreateUserForm {
    + oninput={link.callback(|_| Msg::Update)} />
    {&self.form.field_message("display_name")}
    @@ -266,13 +267,13 @@ impl Component for CreateUserForm {
    + oninput={link.callback(|_| Msg::Update)} />
    {&self.form.field_message("first_name")}
    @@ -285,13 +286,13 @@ impl Component for CreateUserForm {
    + oninput={link.callback(|_| Msg::Update)} />
    {&self.form.field_message("last_name")}
    @@ -304,14 +305,14 @@ impl Component for CreateUserForm {
    + oninput={link.callback(|_| Msg::Update)} />
    {&self.form.field_message("password")}
    @@ -324,14 +325,14 @@ impl Component for CreateUserForm {
    + oninput={link.callback(|_| Msg::Update)} />
    {&self.form.field_message("confirm_password")}
    @@ -340,9 +341,9 @@ impl Component for CreateUserForm {
    diff --git a/app/src/components/delete_group.rs b/app/src/components/delete_group.rs index 881f55f..0934976 100644 --- a/app/src/components/delete_group.rs +++ b/app/src/components/delete_group.rs @@ -109,12 +109,13 @@ impl Component for DeleteGroup { } fn view(&self) -> Html { + let link = &self.common; html! { <> {self.show_modal()} @@ -125,14 +126,15 @@ impl Component for DeleteGroup { impl DeleteGroup { fn show_modal(&self) -> Html { + let link = &self.common; html! {
    } } + fn view_user_menu(&self, ctx: &Context) -> Html { + if let Some((user_id, _)) = &self.user_info { + let link = ctx.link(); + html! { + + } + } else { + html! {} + } + } + fn view_footer(&self) -> Html { html! {
    diff --git a/app/src/components/change_password.rs b/app/src/components/change_password.rs index 1e65046..d6d59d3 100644 --- a/app/src/components/change_password.rs +++ b/app/src/components/change_password.rs @@ -1,21 +1,18 @@ use crate::{ - components::router::{AppRoute, NavButton}, + components::router::{AppRoute, Link}, infra::{ api::HostService, common_component::{CommonComponent, CommonComponentParts}, }, }; -use anyhow::{anyhow, bail, Context, Result}; +use anyhow::{anyhow, bail, Result}; use gloo_console::error; use lldap_auth::*; use validator_derive::Validate; use yew::prelude::*; use yew_form::Form; use yew_form_derive::Model; -use yew_router::{ - agent::{RouteAgentDispatcher, RouteRequest}, - route::Route, -}; +use yew_router::{prelude::History, scope_ext::RouterScopeExt}; #[derive(PartialEq, Eq, Default)] enum OpaqueData { @@ -57,7 +54,6 @@ pub struct ChangePasswordForm { common: CommonComponentParts, form: Form, opaque_data: OpaqueData, - route_dispatcher: RouteAgentDispatcher, } #[derive(Clone, PartialEq, Eq, Properties)] @@ -76,15 +72,20 @@ pub enum Msg { } impl CommonComponent for ChangePasswordForm { - fn handle_msg(&mut self, msg: ::Message) -> Result { + fn handle_msg( + &mut self, + ctx: &Context, + msg: ::Message, + ) -> Result { + use anyhow::Context; match msg { Msg::FormUpdate => Ok(true), Msg::Submit => { if !self.form.validate() { bail!("Check the form for errors"); } - if self.common.is_admin { - self.handle_msg(Msg::SubmitNewPassword) + if ctx.props().is_admin { + self.handle_msg(ctx, Msg::SubmitNewPassword) } else { let old_password = self.form.model().old_password; if old_password.is_empty() { @@ -96,14 +97,14 @@ impl CommonComponent for ChangePasswordForm { .context("Could not initialize login")?; self.opaque_data = OpaqueData::Login(login_start_request.state); let req = login::ClientLoginStartRequest { - username: self.common.username.clone(), + username: ctx.props().username.clone(), login_start_request: login_start_request.message, }; self.common.call_backend( - HostService::login_start, - req, + ctx, + HostService::login_start(req), Msg::AuthenticationStartResponse, - )?; + ); Ok(true) } } @@ -122,7 +123,7 @@ impl CommonComponent for ChangePasswordForm { } _ => panic!("Unexpected data in opaque_data field"), }; - self.handle_msg(Msg::SubmitNewPassword) + self.handle_msg(ctx, Msg::SubmitNewPassword) } Msg::SubmitNewPassword => { let mut rng = rand::rngs::OsRng; @@ -131,15 +132,15 @@ impl CommonComponent for ChangePasswordForm { opaque::client::registration::start_registration(&new_password, &mut rng) .context("Could not initiate password change")?; let req = registration::ClientRegistrationStartRequest { - username: self.common.username.clone(), + username: ctx.props().username.clone(), registration_start_request: registration_start_request.message, }; self.opaque_data = OpaqueData::Registration(registration_start_request.state); self.common.call_backend( - HostService::register_start, - req, + ctx, + HostService::register_start(req), Msg::RegistrationStartResponse, - )?; + ); Ok(true) } Msg::RegistrationStartResponse(res) => { @@ -159,22 +160,20 @@ impl CommonComponent for ChangePasswordForm { registration_upload: registration_finish.message, }; self.common.call_backend( - HostService::register_finish, - req, + ctx, + HostService::register_finish(req), Msg::RegistrationFinishResponse, - ) + ); } _ => panic!("Unexpected data in opaque_data field"), - }?; + }; Ok(false) } Msg::RegistrationFinishResponse(response) => { - self.common.cancel_task(); if response.is_ok() { - self.route_dispatcher - .send(RouteRequest::ChangeRoute(Route::from( - AppRoute::UserDetails(self.common.username.clone()), - ))); + ctx.link().history().unwrap().push(AppRoute::UserDetails { + user_id: ctx.props().username.clone(), + }); } response?; Ok(true) @@ -191,26 +190,21 @@ impl Component for ChangePasswordForm { type Message = Msg; type Properties = Props; - fn create(props: Self::Properties, link: ComponentLink) -> Self { + fn create(_: &Context) -> Self { ChangePasswordForm { - common: CommonComponentParts::::create(props, link), + common: CommonComponentParts::::create(), form: yew_form::Form::::new(FormModel::default()), opaque_data: OpaqueData::None, - route_dispatcher: RouteAgentDispatcher::new(), } } - fn update(&mut self, msg: Self::Message) -> ShouldRender { - CommonComponentParts::::update(self, msg) + fn update(&mut self, ctx: &Context, msg: Self::Message) -> bool { + CommonComponentParts::::update(self, ctx, msg) } - fn change(&mut self, props: Self::Properties) -> ShouldRender { - self.common.change(props) - } - - fn view(&self) -> Html { - let is_admin = self.common.is_admin; - let link = &self.common; + fn view(&self, ctx: &Context) -> Html { + let is_admin = ctx.props().is_admin; + let link = ctx.link(); type Field = yew_form::Field; html! { <> @@ -305,12 +299,12 @@ impl Component for ChangePasswordForm { {"Save changes"} - + to={AppRoute::UserDetails{user_id: ctx.props().username.clone()}}> {"Back"} - +
    diff --git a/app/src/components/create_group.rs b/app/src/components/create_group.rs index c1b5f05..a7e6641 100644 --- a/app/src/components/create_group.rs +++ b/app/src/components/create_group.rs @@ -8,10 +8,7 @@ use graphql_client::GraphQLQuery; use validator_derive::Validate; use yew::prelude::*; use yew_form_derive::Model; -use yew_router::{ - agent::{RouteAgentDispatcher, RouteRequest}, - route::Route, -}; +use yew_router::{prelude::History, scope_ext::RouterScopeExt}; #[derive(GraphQLQuery)] #[graphql( @@ -24,7 +21,6 @@ pub struct CreateGroup; pub struct CreateGroupForm { common: CommonComponentParts, - route_dispatcher: RouteAgentDispatcher, form: yew_form::Form, } @@ -41,7 +37,11 @@ pub enum Msg { } impl CommonComponent for CreateGroupForm { - fn handle_msg(&mut self, msg: ::Message) -> Result { + fn handle_msg( + &mut self, + ctx: &Context, + msg: ::Message, + ) -> Result { match msg { Msg::Update => Ok(true), Msg::SubmitForm => { @@ -53,6 +53,7 @@ impl CommonComponent for CreateGroupForm { name: model.groupname, }; self.common.call_graphql::( + ctx, req, Msg::CreateGroupResponse, "Error trying to create group", @@ -64,8 +65,7 @@ impl CommonComponent for CreateGroupForm { "Created group '{}'", &response?.create_group.display_name )); - self.route_dispatcher - .send(RouteRequest::ChangeRoute(Route::from(AppRoute::ListGroups))); + ctx.link().history().unwrap().push(AppRoute::ListGroups); Ok(true) } } @@ -80,24 +80,19 @@ impl Component for CreateGroupForm { type Message = Msg; type Properties = (); - fn create(props: Self::Properties, link: ComponentLink) -> Self { + fn create(_: &Context) -> Self { Self { - common: CommonComponentParts::::create(props, link), - route_dispatcher: RouteAgentDispatcher::new(), + common: CommonComponentParts::::create(), form: yew_form::Form::::new(CreateGroupModel::default()), } } - fn update(&mut self, msg: Self::Message) -> ShouldRender { - CommonComponentParts::::update(self, msg) + fn update(&mut self, ctx: &Context, msg: Self::Message) -> bool { + CommonComponentParts::::update(self, ctx, msg) } - fn change(&mut self, props: Self::Properties) -> ShouldRender { - self.common.change(props) - } - - fn view(&self) -> Html { - let link = &self.common; + fn view(&self, ctx: &Context) -> Html { + let link = ctx.link(); type Field = yew_form::Field; html! {
    diff --git a/app/src/components/create_user.rs b/app/src/components/create_user.rs index 7ee0fab..34de174 100644 --- a/app/src/components/create_user.rs +++ b/app/src/components/create_user.rs @@ -5,17 +5,14 @@ use crate::{ common_component::{CommonComponent, CommonComponentParts}, }, }; -use anyhow::{bail, Context, Result}; +use anyhow::{bail, Result}; use gloo_console::log; use graphql_client::GraphQLQuery; use lldap_auth::{opaque, registration}; use validator_derive::Validate; use yew::prelude::*; use yew_form_derive::Model; -use yew_router::{ - agent::{RouteAgentDispatcher, RouteRequest}, - route::Route, -}; +use yew_router::{prelude::History, scope_ext::RouterScopeExt}; #[derive(GraphQLQuery)] #[graphql( @@ -28,7 +25,6 @@ pub struct CreateUser; pub struct CreateUserForm { common: CommonComponentParts, - route_dispatcher: RouteAgentDispatcher, form: yew_form::Form, } @@ -73,7 +69,11 @@ pub enum Msg { } impl CommonComponent for CreateUserForm { - fn handle_msg(&mut self, msg: ::Message) -> Result { + fn handle_msg( + &mut self, + ctx: &Context, + msg: ::Message, + ) -> Result { match msg { Msg::Update => Ok(true), Msg::SubmitForm => { @@ -93,6 +93,7 @@ impl CommonComponent for CreateUserForm { }, }; self.common.call_graphql::( + ctx, req, Msg::CreateUserResponse, "Error trying to create user", @@ -122,12 +123,11 @@ impl CommonComponent for CreateUserForm { registration_start_request: message, }; self.common - .call_backend(HostService::register_start, req, move |r| { + .call_backend(ctx, HostService::register_start(req), move |r| { Msg::RegistrationStartResponse((state, r)) - }) - .context("Error trying to create user")?; + }); } else { - self.update(Msg::SuccessfulCreation); + self.update(ctx, Msg::SuccessfulCreation); } Ok(false) } @@ -143,22 +143,19 @@ impl CommonComponent for CreateUserForm { server_data: response.server_data, registration_upload: registration_upload.message, }; - self.common - .call_backend( - HostService::register_finish, - req, - Msg::RegistrationFinishResponse, - ) - .context("Error trying to register user")?; + self.common.call_backend( + ctx, + HostService::register_finish(req), + Msg::RegistrationFinishResponse, + ); Ok(false) } Msg::RegistrationFinishResponse(response) => { response?; - self.handle_msg(Msg::SuccessfulCreation) + self.handle_msg(ctx, Msg::SuccessfulCreation) } Msg::SuccessfulCreation => { - self.route_dispatcher - .send(RouteRequest::ChangeRoute(Route::from(AppRoute::ListUsers))); + ctx.link().history().unwrap().push(AppRoute::ListUsers); Ok(true) } } @@ -173,24 +170,19 @@ impl Component for CreateUserForm { type Message = Msg; type Properties = (); - fn create(props: Self::Properties, link: ComponentLink) -> Self { + fn create(_: &Context) -> Self { Self { - common: CommonComponentParts::::create(props, link), - route_dispatcher: RouteAgentDispatcher::new(), + common: CommonComponentParts::::create(), form: yew_form::Form::::new(CreateUserModel::default()), } } - fn update(&mut self, msg: Self::Message) -> ShouldRender { - CommonComponentParts::::update(self, msg) + fn update(&mut self, ctx: &Context, msg: Self::Message) -> bool { + CommonComponentParts::::update(self, ctx, msg) } - fn change(&mut self, props: Self::Properties) -> ShouldRender { - self.common.change(props) - } - - fn view(&self) -> Html { - let link = &self.common; + fn view(&self, ctx: &Context) -> Html { + let link = &ctx.link(); type Field = yew_form::Field; html! {
    diff --git a/app/src/components/delete_group.rs b/app/src/components/delete_group.rs index 0934976..1fe3ce9 100644 --- a/app/src/components/delete_group.rs +++ b/app/src/components/delete_group.rs @@ -39,16 +39,21 @@ pub enum Msg { } impl CommonComponent for DeleteGroup { - fn handle_msg(&mut self, msg: ::Message) -> Result { + fn handle_msg( + &mut self, + ctx: &Context, + msg: ::Message, + ) -> Result { match msg { Msg::ClickedDeleteGroup => { self.modal.as_ref().expect("modal not initialized").show(); } Msg::ConfirmDeleteGroup => { - self.update(Msg::DismissModal); + self.update(ctx, Msg::DismissModal); self.common.call_graphql::( + ctx, delete_group_query::Variables { - group_id: self.common.group.id, + group_id: ctx.props().group.id, }, Msg::DeleteGroupResponse, "Error trying to delete group", @@ -58,12 +63,8 @@ impl CommonComponent for DeleteGroup { self.modal.as_ref().expect("modal not initialized").hide(); } Msg::DeleteGroupResponse(response) => { - self.common.cancel_task(); response?; - self.common - .props - .on_group_deleted - .emit(self.common.group.id); + ctx.props().on_group_deleted.emit(ctx.props().group.id); } } Ok(true) @@ -78,15 +79,15 @@ impl Component for DeleteGroup { type Message = Msg; type Properties = DeleteGroupProps; - fn create(props: Self::Properties, link: ComponentLink) -> Self { + fn create(_: &Context) -> Self { Self { - common: CommonComponentParts::::create(props, link), + common: CommonComponentParts::::create(), node_ref: NodeRef::default(), modal: None, } } - fn rendered(&mut self, first_render: bool) { + fn rendered(&mut self, _: &Context, first_render: bool) { if first_render { self.modal = Some(Modal::new( self.node_ref @@ -96,20 +97,17 @@ impl Component for DeleteGroup { } } - fn update(&mut self, msg: Self::Message) -> ShouldRender { + fn update(&mut self, ctx: &Context, msg: Self::Message) -> bool { CommonComponentParts::::update_and_report_error( self, + ctx, msg, - self.common.on_error.clone(), + ctx.props().on_error.clone(), ) } - fn change(&mut self, props: Self::Properties) -> ShouldRender { - self.common.change(props) - } - - fn view(&self) -> Html { - let link = &self.common; + fn view(&self, ctx: &Context) -> Html { + let link = &ctx.link(); html! { <> - {self.show_modal()} + {self.show_modal(ctx)} } } } impl DeleteGroup { - fn show_modal(&self) -> Html { - let link = &self.common; + fn show_modal(&self, ctx: &Context) -> Html { + let link = &ctx.link(); html! {