app: Simplify some CommonComponent uses

This commit is contained in:
Valentin Tolmer 2021-10-31 23:42:19 +09:00 committed by nitnelave
parent 12dfa60eed
commit d60f5ab460
8 changed files with 39 additions and 41 deletions

View File

@ -6,7 +6,6 @@ use anyhow::{Error, Result};
use graphql_client::GraphQLQuery;
use std::collections::HashSet;
use yew::prelude::*;
use yewtil::NeqAssign;
#[derive(GraphQLQuery)]
#[graphql(
@ -69,7 +68,7 @@ impl CommonComponent<AddGroupMemberComponent> for AddGroupMemberComponent {
.expect("Could not get selected user")
.clone();
// Remove the user from the dropdown.
self.common.props.on_user_added_to_group.emit(user);
self.common.on_user_added_to_group.emit(user);
}
Msg::SelectionChanged(option_props) => {
let was_some = self.selected_user.is_some();
@ -105,7 +104,7 @@ impl AddGroupMemberComponent {
self.common.call_graphql::<AddUserToGroup, _>(
add_user_to_group::Variables {
user: user_id,
group: self.common.props.group_id,
group: self.common.group_id,
},
Msg::AddMemberResponse,
"Error trying to initiate adding the user to a group",
@ -114,7 +113,7 @@ impl AddGroupMemberComponent {
}
fn get_selectable_user_list(&self, user_list: &[User]) -> Vec<User> {
let user_groups = self.common.props.users.iter().collect::<HashSet<_>>();
let user_groups = self.common.users.iter().collect::<HashSet<_>>();
user_list
.iter()
.filter(|u| !user_groups.contains(u))
@ -141,12 +140,12 @@ impl Component for AddGroupMemberComponent {
CommonComponentParts::<Self>::update_and_report_error(
self,
msg,
self.common.props.on_error.clone(),
self.common.on_error.clone(),
)
}
fn change(&mut self, props: Self::Properties) -> ShouldRender {
self.common.props.neq_assign(props)
self.common.change(props)
}
fn view(&self) -> Html {

View File

@ -82,7 +82,7 @@ impl CommonComponent<AddUserToGroupComponent> for AddUserToGroupComponent {
.expect("Could not get selected group")
.clone();
// Remove the group from the dropdown.
self.common.props.on_user_added_to_group.emit(group);
self.common.on_user_added_to_group.emit(group);
}
Msg::SelectionChanged(option_props) => {
let was_some = self.selected_group.is_some();
@ -117,7 +117,7 @@ impl AddUserToGroupComponent {
};
self.common.call_graphql::<AddUserToGroup, _>(
add_user_to_group::Variables {
user: self.common.props.username.clone(),
user: self.common.username.clone(),
group: group_id,
},
Msg::AddGroupResponse,
@ -127,7 +127,7 @@ impl AddUserToGroupComponent {
}
fn get_selectable_group_list(&self, group_list: &[Group]) -> Vec<Group> {
let user_groups = self.common.props.groups.iter().collect::<HashSet<_>>();
let user_groups = self.common.groups.iter().collect::<HashSet<_>>();
group_list
.iter()
.filter(|g| !user_groups.contains(g))
@ -153,7 +153,7 @@ impl Component for AddUserToGroupComponent {
CommonComponentParts::<Self>::update_and_report_error(
self,
msg,
self.common.props.on_error.clone(),
self.common.on_error.clone(),
)
}

View File

@ -48,7 +48,7 @@ impl CommonComponent<DeleteGroup> for DeleteGroup {
self.update(Msg::DismissModal);
self.common.call_graphql::<DeleteGroupQuery, _>(
delete_group_query::Variables {
group_id: self.common.props.group.id,
group_id: self.common.group.id,
},
Msg::DeleteGroupResponse,
"Error trying to delete group",
@ -63,7 +63,7 @@ impl CommonComponent<DeleteGroup> for DeleteGroup {
self.common
.props
.on_group_deleted
.emit(self.common.props.group.id);
.emit(self.common.group.id);
}
}
Ok(true)
@ -100,7 +100,7 @@ impl Component for DeleteGroup {
CommonComponentParts::<Self>::update_and_report_error(
self,
msg,
self.common.props.on_error.clone(),
self.common.on_error.clone(),
)
}
@ -128,7 +128,7 @@ impl DeleteGroup {
html! {
<div
class="modal fade"
id="deleteGroupModal".to_string() + &self.common.props.group.id.to_string()
id="deleteGroupModal".to_string() + &self.common.group.id.to_string()
tabindex="-1"
aria-labelledby="deleteGroupModalLabel"
aria-hidden="true"
@ -146,7 +146,7 @@ impl DeleteGroup {
<div class="modal-body">
<span>
{"Are you sure you want to delete group "}
<b>{&self.common.props.group.display_name}</b>{"?"}
<b>{&self.common.group.display_name}</b>{"?"}
</span>
</div>
<div class="modal-footer">

View File

@ -45,7 +45,7 @@ impl CommonComponent<DeleteUser> for DeleteUser {
self.update(Msg::DismissModal);
self.common.call_graphql::<DeleteUserQuery, _>(
delete_user_query::Variables {
user: self.common.props.username.clone(),
user: self.common.username.clone(),
},
Msg::DeleteUserResponse,
"Error trying to delete user",
@ -60,7 +60,7 @@ impl CommonComponent<DeleteUser> for DeleteUser {
self.common
.props
.on_user_deleted
.emit(self.common.props.username.clone());
.emit(self.common.username.clone());
}
}
Ok(true)
@ -97,7 +97,7 @@ impl Component for DeleteUser {
CommonComponentParts::<Self>::update_and_report_error(
self,
msg,
self.common.props.on_error.clone(),
self.common.on_error.clone(),
)
}
@ -125,7 +125,7 @@ impl DeleteUser {
html! {
<div
class="modal fade"
id="deleteUserModal".to_string() + &self.common.props.username
id="deleteUserModal".to_string() + &self.common.username
tabindex="-1"
//role="dialog"
aria-labelledby="deleteUserModalLabel"
@ -144,7 +144,7 @@ impl DeleteUser {
<div class="modal-body">
<span>
{"Are you sure you want to delete user "}
<b>{&self.common.props.username}</b>{"?"}
<b>{&self.common.username}</b>{"?"}
</span>
</div>
<div class="modal-footer">

View File

@ -30,7 +30,7 @@ impl CommonComponent<LogoutButton> for LogoutButton {
Msg::LogoutCompleted(res) => {
res?;
delete_cookie("user_id")?;
self.common.props.on_logged_out.emit(());
self.common.on_logged_out.emit(());
}
}
Ok(false)

View File

@ -37,10 +37,9 @@ impl CommonComponent<RemoveUserFromGroupComponent> for RemoveUserFromGroupCompon
Msg::RemoveGroupResponse(response) => {
response?;
self.common.cancel_task();
self.common.props.on_user_removed_from_group.emit((
self.common.props.username.clone(),
self.common.props.group_id,
));
self.common
.on_user_removed_from_group
.emit((self.common.username.clone(), self.common.group_id));
}
}
Ok(true)
@ -55,8 +54,8 @@ impl RemoveUserFromGroupComponent {
fn submit_remove_group(&mut self) {
self.common.call_graphql::<RemoveUserFromGroup, _>(
remove_user_from_group::Variables {
user: self.common.props.username.clone(),
group: self.common.props.group_id,
user: self.common.username.clone(),
group: self.common.group_id,
},
Msg::RemoveGroupResponse,
"Error trying to initiate removing the user from a group",
@ -78,7 +77,7 @@ impl Component for RemoveUserFromGroupComponent {
CommonComponentParts::<Self>::update_and_report_error(
self,
msg,
self.common.props.on_error.clone(),
self.common.on_error.clone(),
)
}

View File

@ -80,7 +80,7 @@ impl UserDetails {
fn get_user_details(&mut self) {
self.common.call_graphql::<GetUserDetails, _>(
get_user_details::Variables {
id: self.common.props.username.clone(),
id: self.common.username.clone(),
},
Msg::UserDetailsResponse,
"Error trying to fetch user details",
@ -104,7 +104,7 @@ impl UserDetails {
let display_name = group.display_name.clone();
html! {
<tr key="groupRow_".to_string() + &display_name>
{if self.common.props.is_admin { html! {
{if self.common.is_admin { html! {
<>
<td>
<Link route=AppRoute::GroupDetails(group.id)>
@ -133,7 +133,7 @@ impl UserDetails {
<thead>
<tr key="headerRow">
<th>{"Group"}</th>
{ if self.common.props.is_admin { html!{ <th></th> }} else { html!{} }}
{ if self.common.is_admin { html!{ <th></th> }} else { html!{} }}
</tr>
</thead>
<tbody>
@ -154,7 +154,7 @@ impl UserDetails {
}
fn view_add_group_button(&self, u: &User) -> Html {
if self.common.props.is_admin {
if self.common.is_admin {
html! {
<AddUserToGroupComponent
username=u.id.clone()

View File

@ -92,7 +92,7 @@ impl Component for UserDetailsForm {
CommonComponentParts::<Self>::update_and_report_error(
self,
msg,
self.common.props.on_error.clone(),
self.common.on_error.clone(),
)
}
@ -111,7 +111,7 @@ impl Component for UserDetailsForm {
{"User ID: "}
</label>
<div class="col-8">
<span id="userId" class="form-constrol-static">{&self.common.props.user.id}</span>
<span id="userId" class="form-constrol-static">{&self.common.user.id}</span>
</div>
</div>
<div class="form-group row mb-3">
@ -192,7 +192,7 @@ impl Component for UserDetailsForm {
{"Creation date: "}
</label>
<div class="col-8">
<span id="creationDate" class="form-constrol-static">{&self.common.props.user.creation_date.date().naive_local()}</span>
<span id="creationDate" class="form-constrol-static">{&self.common.user.creation_date.date().naive_local()}</span>
</div>
</div>
<div class="form-group row justify-content-center">
@ -218,9 +218,9 @@ impl UserDetailsForm {
if !self.form.validate() {
bail!("Invalid inputs");
}
let base_user = &self.common.props.user;
let base_user = &self.common.user;
let mut user_input = update_user::UpdateUserInput {
id: self.common.props.user.id.clone(),
id: self.common.user.id.clone(),
email: None,
displayName: None,
firstName: None,
@ -260,14 +260,14 @@ impl UserDetailsForm {
Err(e) => return Err(e),
Ok(_) => {
let model = self.form.model();
self.common.props.user = User {
id: self.common.props.user.id.clone(),
self.common.user = User {
id: self.common.user.id.clone(),
email: model.email,
display_name: model.display_name,
first_name: model.first_name,
last_name: model.last_name,
creation_date: self.common.props.user.creation_date,
groups: self.common.props.user.groups.clone(),
creation_date: self.common.user.creation_date,
groups: self.common.user.groups.clone(),
};
self.just_updated = true;
}