app: Migrate user_details to CommonComponent

This commit is contained in:
Valentin Tolmer 2021-10-31 21:20:19 +09:00 committed by nitnelave
parent 65dd1d1fd3
commit 29962881cf

View File

@ -5,14 +5,11 @@ use crate::{
router::{AppRoute, Link, NavButton}, router::{AppRoute, Link, NavButton},
user_details_form::UserDetailsForm, user_details_form::UserDetailsForm,
}, },
infra::api::HostService, infra::common_component::{CommonComponent, CommonComponentParts},
}; };
use anyhow::{bail, Error, Result}; use anyhow::{bail, Error, Result};
use graphql_client::GraphQLQuery; use graphql_client::GraphQLQuery;
use yew::{ use yew::prelude::*;
prelude::*,
services::{fetch::FetchTask, ConsoleService},
};
#[derive(GraphQLQuery)] #[derive(GraphQLQuery)]
#[graphql( #[graphql(
@ -27,15 +24,10 @@ pub type User = get_user_details::GetUserDetailsUser;
pub type Group = get_user_details::GetUserDetailsUserGroups; pub type Group = get_user_details::GetUserDetailsUserGroups;
pub struct UserDetails { pub struct UserDetails {
link: ComponentLink<Self>, common: CommonComponentParts<Self>,
props: Props,
/// The user info. If none, the error is in `error`. If `error` is None, then we haven't /// The user info. If none, the error is in `error`. If `error` is None, then we haven't
/// received the server response yet. /// received the server response yet.
user: Option<User>, user: Option<User>,
/// Error message displayed to the user.
error: Option<Error>,
// Used to keep the request alive long enough.
_task: Option<FetchTask>,
} }
/// State machine describing the possible transitions of the component state. /// State machine describing the possible transitions of the component state.
@ -54,22 +46,7 @@ pub struct Props {
pub is_admin: bool, pub is_admin: bool,
} }
impl UserDetails { impl CommonComponent<UserDetails> for UserDetails {
fn get_user_details(&mut self) {
self._task = HostService::graphql_query::<GetUserDetails>(
get_user_details::Variables {
id: self.props.username.clone(),
},
self.link.callback(Msg::UserDetailsResponse),
"Error trying to fetch user details",
)
.map_err(|e| {
ConsoleService::log(&e.to_string());
e
})
.ok();
}
fn handle_msg(&mut self, msg: <Self as Component>::Message) -> Result<bool> { fn handle_msg(&mut self, msg: <Self as Component>::Message) -> Result<bool> {
match msg { match msg {
Msg::UserDetailsResponse(response) => match response { Msg::UserDetailsResponse(response) => match response {
@ -94,6 +71,22 @@ impl UserDetails {
Ok(true) Ok(true)
} }
fn mut_common(&mut self) -> &mut CommonComponentParts<Self> {
&mut self.common
}
}
impl UserDetails {
fn get_user_details(&mut self) {
self.common.call_graphql::<GetUserDetails, _>(
get_user_details::Variables {
id: self.common.props.username.clone(),
},
Msg::UserDetailsResponse,
"Error trying to fetch user details",
);
}
fn view_messages(&self, error: &Option<Error>) -> Html { fn view_messages(&self, error: &Option<Error>) -> Html {
if let Some(e) = error { if let Some(e) = error {
html! { html! {
@ -111,7 +104,7 @@ impl UserDetails {
let display_name = group.display_name.clone(); let display_name = group.display_name.clone();
html! { html! {
<tr key="groupRow_".to_string() + &display_name> <tr key="groupRow_".to_string() + &display_name>
{if self.props.is_admin { html! { {if self.common.props.is_admin { html! {
<> <>
<td> <td>
<Link route=AppRoute::GroupDetails(group.id)> <Link route=AppRoute::GroupDetails(group.id)>
@ -122,8 +115,8 @@ impl UserDetails {
<RemoveUserFromGroupComponent <RemoveUserFromGroupComponent
username=u.id.clone() username=u.id.clone()
group_id=group.id group_id=group.id
on_user_removed_from_group=self.link.callback(Msg::OnUserRemovedFromGroup) on_user_removed_from_group=self.common.callback(Msg::OnUserRemovedFromGroup)
on_error=self.link.callback(Msg::OnError)/> on_error=self.common.callback(Msg::OnError)/>
</td> </td>
</> </>
} } else { html! { } } else { html! {
@ -140,7 +133,7 @@ impl UserDetails {
<thead> <thead>
<tr key="headerRow"> <tr key="headerRow">
<th>{"Group"}</th> <th>{"Group"}</th>
{ if self.props.is_admin { html!{ <th></th> }} else { html!{} }} { if self.common.props.is_admin { html!{ <th></th> }} else { html!{} }}
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
@ -161,13 +154,13 @@ impl UserDetails {
} }
fn view_add_group_button(&self, u: &User) -> Html { fn view_add_group_button(&self, u: &User) -> Html {
if self.props.is_admin { if self.common.props.is_admin {
html! { html! {
<AddUserToGroupComponent <AddUserToGroupComponent
username=u.id.clone() username=u.id.clone()
groups=u.groups.clone() groups=u.groups.clone()
on_error=self.link.callback(Msg::OnError) on_error=self.common.callback(Msg::OnError)
on_user_added_to_group=self.link.callback(Msg::OnUserAddedToGroup)/> on_user_added_to_group=self.common.callback(Msg::OnUserAddedToGroup)/>
} }
} else { } else {
html! {} html! {}
@ -181,26 +174,15 @@ impl Component for UserDetails {
fn create(props: Self::Properties, link: ComponentLink<Self>) -> Self { fn create(props: Self::Properties, link: ComponentLink<Self>) -> Self {
let mut table = Self { let mut table = Self {
link, common: CommonComponentParts::<Self>::create(props, link),
props,
_task: None,
user: None, user: None,
error: None,
}; };
table.get_user_details(); table.get_user_details();
table table
} }
fn update(&mut self, msg: Self::Message) -> ShouldRender { fn update(&mut self, msg: Self::Message) -> ShouldRender {
self.error = None; CommonComponentParts::<Self>::update(self, msg)
match self.handle_msg(msg) {
Err(e) => {
ConsoleService::error(&e.to_string());
self.error = Some(e);
true
}
Ok(b) => b,
}
} }
fn change(&mut self, _: Self::Properties) -> ShouldRender { fn change(&mut self, _: Self::Properties) -> ShouldRender {
@ -208,7 +190,7 @@ impl Component for UserDetails {
} }
fn view(&self) -> Html { fn view(&self) -> Html {
match (&self.user, &self.error) { match (&self.user, &self.common.error) {
(None, None) => html! {{"Loading..."}}, (None, None) => html! {{"Loading..."}},
(None, Some(e)) => html! {<div>{"Error: "}{e.to_string()}</div>}, (None, Some(e)) => html! {<div>{"Error: "}{e.to_string()}</div>},
(Some(u), error) => { (Some(u), error) => {
@ -217,7 +199,7 @@ impl Component for UserDetails {
<h3>{u.id.to_string()}</h3> <h3>{u.id.to_string()}</h3>
<UserDetailsForm <UserDetailsForm
user=u.clone() user=u.clone()
on_error=self.link.callback(Msg::OnError)/> on_error=self.common.callback(Msg::OnError)/>
<div class="row justify-content-center"> <div class="row justify-content-center">
<NavButton <NavButton
route=AppRoute::ChangePassword(u.id.clone()) route=AppRoute::ChangePassword(u.id.clone())