2021-09-12 09:54:47 +00:00
|
|
|
use crate::{
|
2021-09-17 17:43:22 +00:00
|
|
|
components::{
|
|
|
|
add_user_to_group::AddUserToGroupComponent,
|
2021-09-19 14:01:44 +00:00
|
|
|
remove_user_from_group::RemoveUserFromGroupComponent,
|
2021-09-17 17:43:22 +00:00
|
|
|
router::{AppRoute, NavButton},
|
2021-09-19 11:44:58 +00:00
|
|
|
user_details_form::UserDetailsForm,
|
2021-09-17 17:43:22 +00:00
|
|
|
},
|
2021-09-12 09:54:47 +00:00
|
|
|
infra::api::HostService,
|
|
|
|
};
|
2021-09-19 11:44:58 +00:00
|
|
|
use anyhow::{bail, Error, Result};
|
2021-08-30 07:08:42 +00:00
|
|
|
use graphql_client::GraphQLQuery;
|
2021-09-12 09:54:47 +00:00
|
|
|
use yew::{
|
|
|
|
prelude::*,
|
|
|
|
services::{fetch::FetchTask, ConsoleService},
|
|
|
|
};
|
2021-07-05 07:21:13 +00:00
|
|
|
|
2021-08-30 07:08:42 +00:00
|
|
|
#[derive(GraphQLQuery)]
|
|
|
|
#[graphql(
|
|
|
|
schema_path = "../schema.graphql",
|
|
|
|
query_path = "queries/get_user_details.graphql",
|
2021-09-17 17:43:22 +00:00
|
|
|
response_derives = "Debug, Hash, PartialEq, Eq, Clone",
|
2021-09-12 09:54:47 +00:00
|
|
|
custom_scalars_module = "crate::infra::graphql"
|
2021-08-30 07:08:42 +00:00
|
|
|
)]
|
|
|
|
pub struct GetUserDetails;
|
|
|
|
|
2021-09-17 17:43:22 +00:00
|
|
|
pub type User = get_user_details::GetUserDetailsUser;
|
|
|
|
pub type Group = get_user_details::GetUserDetailsUserGroups;
|
2021-08-30 07:08:42 +00:00
|
|
|
|
2021-07-05 07:21:13 +00:00
|
|
|
pub struct UserDetails {
|
|
|
|
link: ComponentLink<Self>,
|
2021-09-19 14:01:44 +00:00
|
|
|
props: Props,
|
2021-09-16 21:22:11 +00:00
|
|
|
/// The user info. If none, the error is in `error`. If `error` is None, then we haven't
|
|
|
|
/// received the server response yet.
|
2021-09-02 16:11:28 +00:00
|
|
|
user: Option<User>,
|
2021-09-16 21:22:11 +00:00
|
|
|
/// Error message displayed to the user.
|
|
|
|
error: Option<Error>,
|
2021-07-05 07:21:13 +00:00
|
|
|
// Used to keep the request alive long enough.
|
|
|
|
_task: Option<FetchTask>,
|
|
|
|
}
|
|
|
|
|
2021-09-16 21:22:11 +00:00
|
|
|
/// State machine describing the possible transitions of the component state.
|
|
|
|
/// It starts out by fetching the user's details from the backend when loading.
|
2021-07-05 07:21:13 +00:00
|
|
|
pub enum Msg {
|
2021-09-16 21:22:11 +00:00
|
|
|
/// Received the user details response, either the user data or an error.
|
2021-08-30 07:08:42 +00:00
|
|
|
UserDetailsResponse(Result<get_user_details::ResponseData>),
|
2021-09-17 17:43:22 +00:00
|
|
|
OnError(Error),
|
|
|
|
OnUserAddedToGroup(Group),
|
2021-09-19 14:01:44 +00:00
|
|
|
OnUserRemovedFromGroup(Group),
|
2021-07-05 07:21:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(yew::Properties, Clone, PartialEq)]
|
|
|
|
pub struct Props {
|
|
|
|
pub username: String,
|
2021-09-16 21:22:11 +00:00
|
|
|
pub is_admin: bool,
|
2021-07-05 07:21:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl UserDetails {
|
|
|
|
fn get_user_details(&mut self) {
|
2021-08-30 07:08:42 +00:00
|
|
|
self._task = HostService::graphql_query::<GetUserDetails>(
|
|
|
|
get_user_details::Variables {
|
2021-09-19 14:01:44 +00:00
|
|
|
id: self.props.username.clone(),
|
2021-08-30 07:08:42 +00:00
|
|
|
},
|
2021-07-05 07:21:13 +00:00
|
|
|
self.link.callback(Msg::UserDetailsResponse),
|
2021-08-30 07:08:42 +00:00
|
|
|
"Error trying to fetch user details",
|
|
|
|
)
|
|
|
|
.map_err(|e| {
|
|
|
|
ConsoleService::log(&e.to_string());
|
|
|
|
e
|
|
|
|
})
|
|
|
|
.ok();
|
2021-07-05 07:21:13 +00:00
|
|
|
}
|
2021-09-16 21:22:11 +00:00
|
|
|
|
2021-09-02 16:11:28 +00:00
|
|
|
fn handle_msg(&mut self, msg: <Self as Component>::Message) -> Result<bool> {
|
|
|
|
match msg {
|
2021-09-16 21:22:11 +00:00
|
|
|
Msg::UserDetailsResponse(response) => match response {
|
|
|
|
Ok(user) => self.user = Some(user.user),
|
|
|
|
Err(e) => {
|
|
|
|
self.user = None;
|
|
|
|
bail!("Error getting user details: {}", e);
|
2021-09-02 16:11:28 +00:00
|
|
|
}
|
2021-09-16 21:22:11 +00:00
|
|
|
},
|
2021-09-17 17:43:22 +00:00
|
|
|
Msg::OnError(e) => return Err(e),
|
|
|
|
Msg::OnUserAddedToGroup(group) => {
|
|
|
|
self.user.as_mut().unwrap().groups.push(group);
|
2021-09-16 21:22:11 +00:00
|
|
|
}
|
2021-09-19 14:01:44 +00:00
|
|
|
Msg::OnUserRemovedFromGroup(group) => {
|
|
|
|
self.user.as_mut().unwrap().groups.retain(|g| g != &group);
|
|
|
|
}
|
2021-09-16 21:22:11 +00:00
|
|
|
}
|
|
|
|
Ok(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn view_messages(&self, error: &Option<Error>) -> Html {
|
2021-09-19 14:01:44 +00:00
|
|
|
if let Some(e) = error {
|
2021-09-16 21:22:11 +00:00
|
|
|
html! {
|
|
|
|
<div>
|
|
|
|
<span>{"Error: "}{e.to_string()}</span>
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
html! {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn view_group_memberships(&self, u: &User) -> Html {
|
|
|
|
let make_group_row = |group: &Group| {
|
|
|
|
let display_name = group.display_name.clone();
|
|
|
|
html! {
|
2021-09-17 19:27:00 +00:00
|
|
|
<tr key="groupRow_".to_string() + &display_name>
|
2021-09-19 14:01:44 +00:00
|
|
|
<RemoveUserFromGroupComponent
|
|
|
|
username=u.id.clone()
|
|
|
|
group=group.clone()
|
|
|
|
is_admin=self.props.is_admin
|
|
|
|
on_user_removed_from_group=self.link.callback(Msg::OnUserRemovedFromGroup)
|
|
|
|
on_error=self.link.callback(Msg::OnError)/>
|
2021-09-16 21:22:11 +00:00
|
|
|
</tr>
|
2021-09-02 16:11:28 +00:00
|
|
|
}
|
2021-09-16 21:22:11 +00:00
|
|
|
};
|
|
|
|
html! {
|
|
|
|
<div>
|
2021-09-19 17:44:53 +00:00
|
|
|
<h3>{"Group memberships"}</h3>
|
|
|
|
<div class="table-responsive">
|
|
|
|
<table class="table table-striped">
|
|
|
|
<thead>
|
|
|
|
<tr key="headerRow">
|
|
|
|
<th>{"Group"}</th>
|
|
|
|
{ if self.props.is_admin { html!{ <th></th> }} else { html!{} }}
|
|
|
|
</tr>
|
|
|
|
</thead>
|
|
|
|
<tbody>
|
|
|
|
{if u.groups.is_empty() {
|
|
|
|
html! {
|
|
|
|
<tr key="EmptyRow">
|
|
|
|
<td>{"Not member of any group"}</td>
|
|
|
|
</tr>
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
html! {<>{u.groups.iter().map(make_group_row).collect::<Vec<_>>()}</>}
|
|
|
|
}}
|
|
|
|
<hr/>
|
|
|
|
<tr key="groupToAddRow">
|
|
|
|
{self.view_add_group_button(u)}
|
|
|
|
</tr>
|
|
|
|
</tbody>
|
|
|
|
</table>
|
|
|
|
</div>
|
2021-09-16 21:22:11 +00:00
|
|
|
</div>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-17 17:43:22 +00:00
|
|
|
fn view_add_group_button(&self, u: &User) -> Html {
|
2021-09-19 14:01:44 +00:00
|
|
|
if self.props.is_admin {
|
2021-09-16 21:22:11 +00:00
|
|
|
html! {
|
2021-09-17 17:43:22 +00:00
|
|
|
<AddUserToGroupComponent
|
2021-09-19 14:01:44 +00:00
|
|
|
username=u.id.clone()
|
|
|
|
groups=u.groups.clone()
|
2021-09-17 17:43:22 +00:00
|
|
|
on_error=self.link.callback(Msg::OnError)
|
|
|
|
on_user_added_to_group=self.link.callback(Msg::OnUserAddedToGroup)/>
|
2021-09-16 21:22:11 +00:00
|
|
|
}
|
|
|
|
} else {
|
2021-09-17 17:43:22 +00:00
|
|
|
html! {}
|
2021-09-02 16:11:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-05 07:21:13 +00:00
|
|
|
impl Component for UserDetails {
|
|
|
|
type Message = Msg;
|
|
|
|
type Properties = Props;
|
|
|
|
|
|
|
|
fn create(props: Self::Properties, link: ComponentLink<Self>) -> Self {
|
2021-09-16 21:22:11 +00:00
|
|
|
let mut table = Self {
|
2021-07-05 07:21:13 +00:00
|
|
|
link,
|
2021-09-19 14:01:44 +00:00
|
|
|
props,
|
2021-07-05 07:21:13 +00:00
|
|
|
_task: None,
|
|
|
|
user: None,
|
2021-09-02 16:11:28 +00:00
|
|
|
error: None,
|
2021-07-05 07:21:13 +00:00
|
|
|
};
|
|
|
|
table.get_user_details();
|
|
|
|
table
|
|
|
|
}
|
|
|
|
|
|
|
|
fn update(&mut self, msg: Self::Message) -> ShouldRender {
|
2021-09-02 16:11:28 +00:00
|
|
|
self.error = None;
|
|
|
|
match self.handle_msg(msg) {
|
|
|
|
Err(e) => {
|
|
|
|
ConsoleService::error(&e.to_string());
|
|
|
|
self.error = Some(e);
|
2021-07-05 07:21:13 +00:00
|
|
|
true
|
|
|
|
}
|
2021-09-02 16:11:28 +00:00
|
|
|
Ok(b) => b,
|
2021-07-05 07:21:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn change(&mut self, _: Self::Properties) -> ShouldRender {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
|
|
|
fn view(&self) -> Html {
|
2021-09-02 16:11:28 +00:00
|
|
|
match (&self.user, &self.error) {
|
|
|
|
(None, None) => html! {{"Loading..."}},
|
|
|
|
(None, Some(e)) => html! {<div>{"Error: "}{e.to_string()}</div>},
|
|
|
|
(Some(u), error) => {
|
2021-07-05 07:21:13 +00:00
|
|
|
html! {
|
2021-09-16 21:22:11 +00:00
|
|
|
<div>
|
2021-09-19 11:44:58 +00:00
|
|
|
<UserDetailsForm
|
|
|
|
user=u.clone()
|
|
|
|
on_error=self.link.callback(Msg::OnError)/>
|
2021-09-16 21:22:11 +00:00
|
|
|
{self.view_messages(error)}
|
|
|
|
{self.view_group_memberships(u)}
|
2021-09-12 09:54:47 +00:00
|
|
|
<div>
|
2021-09-19 17:44:53 +00:00
|
|
|
<NavButton
|
|
|
|
route=AppRoute::ChangePassword(u.id.clone())
|
|
|
|
classes="btn btn-primary">
|
|
|
|
{"Change password"}
|
|
|
|
</NavButton>
|
2021-09-12 09:54:47 +00:00
|
|
|
</div>
|
2021-09-16 21:22:11 +00:00
|
|
|
</div>
|
2021-07-05 07:21:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|