2021-10-11 16:41:55 +00:00
|
|
|
use crate::{
|
|
|
|
components::{
|
|
|
|
add_group_member::{self, AddGroupMemberComponent},
|
|
|
|
remove_user_from_group::RemoveUserFromGroupComponent,
|
|
|
|
router::{AppRoute, Link},
|
|
|
|
},
|
2021-10-31 12:19:54 +00:00
|
|
|
infra::common_component::{CommonComponent, CommonComponentParts},
|
2021-10-11 16:41:55 +00:00
|
|
|
};
|
|
|
|
use anyhow::{bail, Error, Result};
|
|
|
|
use graphql_client::GraphQLQuery;
|
2021-10-31 12:19:54 +00:00
|
|
|
use yew::prelude::*;
|
2021-10-11 16:41:55 +00:00
|
|
|
|
|
|
|
#[derive(GraphQLQuery)]
|
|
|
|
#[graphql(
|
|
|
|
schema_path = "../schema.graphql",
|
|
|
|
query_path = "queries/get_group_details.graphql",
|
|
|
|
response_derives = "Debug, Hash, PartialEq, Eq, Clone",
|
|
|
|
custom_scalars_module = "crate::infra::graphql"
|
|
|
|
)]
|
|
|
|
pub struct GetGroupDetails;
|
|
|
|
|
|
|
|
pub type Group = get_group_details::GetGroupDetailsGroup;
|
|
|
|
pub type User = get_group_details::GetGroupDetailsGroupUsers;
|
|
|
|
pub type AddGroupMemberUser = add_group_member::User;
|
|
|
|
|
|
|
|
pub struct GroupDetails {
|
2021-10-29 03:32:52 +00:00
|
|
|
common: CommonComponentParts<Self>,
|
2021-10-11 16:41:55 +00:00
|
|
|
/// The group info. If none, the error is in `error`. If `error` is None, then we haven't
|
|
|
|
/// received the server response yet.
|
|
|
|
group: Option<Group>,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// State machine describing the possible transitions of the component state.
|
|
|
|
/// It starts out by fetching the user's details from the backend when loading.
|
|
|
|
pub enum Msg {
|
|
|
|
/// Received the group details response, either the group data or an error.
|
|
|
|
GroupDetailsResponse(Result<get_group_details::ResponseData>),
|
|
|
|
OnError(Error),
|
|
|
|
OnUserAddedToGroup(AddGroupMemberUser),
|
|
|
|
OnUserRemovedFromGroup((String, i64)),
|
|
|
|
}
|
|
|
|
|
2022-09-27 04:48:39 +00:00
|
|
|
#[derive(yew::Properties, Clone, PartialEq, Eq)]
|
2021-10-11 16:41:55 +00:00
|
|
|
pub struct Props {
|
|
|
|
pub group_id: i64,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl GroupDetails {
|
|
|
|
fn get_group_details(&mut self) {
|
2021-10-31 12:19:54 +00:00
|
|
|
self.common.call_graphql::<GetGroupDetails, _>(
|
2021-10-11 16:41:55 +00:00
|
|
|
get_group_details::Variables {
|
2021-10-29 03:32:52 +00:00
|
|
|
id: self.common.group_id,
|
2021-10-11 16:41:55 +00:00
|
|
|
},
|
2021-10-31 12:19:54 +00:00
|
|
|
Msg::GroupDetailsResponse,
|
2021-10-11 16:41:55 +00:00
|
|
|
"Error trying to fetch group details",
|
2021-10-31 12:19:54 +00:00
|
|
|
);
|
2021-10-11 16:41:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn view_messages(&self, error: &Option<Error>) -> Html {
|
|
|
|
if let Some(e) = error {
|
|
|
|
html! {
|
|
|
|
<div class="alert alert-danger">
|
|
|
|
<span>{"Error: "}{e.to_string()}</span>
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
html! {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-21 10:10:37 +00:00
|
|
|
fn view_details(&self, g: &Group) -> Html {
|
|
|
|
html! {
|
|
|
|
<>
|
|
|
|
<h3>{g.display_name.to_string()}</h3>
|
|
|
|
<div class="py-3">
|
|
|
|
<form class="form">
|
|
|
|
<div class="form-group row mb-3">
|
|
|
|
<label for="displayName"
|
|
|
|
class="form-label col-4 col-form-label">
|
|
|
|
{"Group: "}
|
|
|
|
</label>
|
|
|
|
<div class="col-8">
|
|
|
|
<span id="groupId" class="form-constrol-static">{g.display_name.to_string()}</span>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div class="form-group row mb-3">
|
|
|
|
<label for="creationDate"
|
|
|
|
class="form-label col-4 col-form-label">
|
|
|
|
{"Creation date: "}
|
|
|
|
</label>
|
|
|
|
<div class="col-8">
|
2022-11-21 08:13:25 +00:00
|
|
|
<span id="creationDate" class="form-constrol-static">{g.creation_date.naive_local().date()}</span>
|
2022-07-21 10:10:37 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div class="form-group row mb-3">
|
|
|
|
<label for="uuid"
|
|
|
|
class="form-label col-4 col-form-label">
|
|
|
|
{"UUID: "}
|
|
|
|
</label>
|
|
|
|
<div class="col-8">
|
|
|
|
<span id="uuid" class="form-constrol-static">{g.uuid.to_string()}</span>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-11 16:41:55 +00:00
|
|
|
fn view_user_list(&self, g: &Group) -> Html {
|
|
|
|
let make_user_row = |user: &User| {
|
|
|
|
let user_id = user.id.clone();
|
|
|
|
let display_name = user.display_name.clone();
|
|
|
|
html! {
|
|
|
|
<tr>
|
|
|
|
<td>
|
|
|
|
<Link route=AppRoute::UserDetails(user_id.clone())>
|
|
|
|
{user_id.clone()}
|
|
|
|
</Link>
|
|
|
|
</td>
|
|
|
|
<td>{display_name}</td>
|
|
|
|
<td>
|
|
|
|
<RemoveUserFromGroupComponent
|
|
|
|
username=user_id
|
|
|
|
group_id=g.id
|
2021-10-31 12:19:54 +00:00
|
|
|
on_user_removed_from_group=self.common.callback(Msg::OnUserRemovedFromGroup)
|
|
|
|
on_error=self.common.callback(Msg::OnError)/>
|
2021-10-11 16:41:55 +00:00
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
}
|
|
|
|
};
|
|
|
|
html! {
|
2021-10-15 08:20:37 +00:00
|
|
|
<>
|
|
|
|
<h5 class="fw-bold">{"Members"}</h5>
|
2021-10-11 17:20:13 +00:00
|
|
|
<div class="table-responsive">
|
2022-11-03 14:40:02 +00:00
|
|
|
<table class="table table-hover">
|
2021-10-11 17:20:13 +00:00
|
|
|
<thead>
|
|
|
|
<tr key="headerRow">
|
|
|
|
<th>{"User Id"}</th>
|
|
|
|
<th>{"Display name"}</th>
|
|
|
|
<th></th>
|
|
|
|
</tr>
|
|
|
|
</thead>
|
|
|
|
<tbody>
|
|
|
|
{if g.users.is_empty() {
|
|
|
|
html! {
|
|
|
|
<tr key="EmptyRow">
|
2022-11-03 14:40:02 +00:00
|
|
|
<td>{"There are no users in this group."}</td>
|
2021-10-11 17:20:13 +00:00
|
|
|
<td/>
|
|
|
|
</tr>
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
html! {<>{g.users.iter().map(make_user_row).collect::<Vec<_>>()}</>}
|
|
|
|
}}
|
|
|
|
</tbody>
|
|
|
|
</table>
|
|
|
|
</div>
|
2021-10-15 08:20:37 +00:00
|
|
|
</>
|
2021-10-11 16:41:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn view_add_user_button(&self, g: &Group) -> Html {
|
|
|
|
let users: Vec<_> = g
|
|
|
|
.users
|
|
|
|
.iter()
|
|
|
|
.map(|u| AddGroupMemberUser {
|
|
|
|
id: u.id.clone(),
|
|
|
|
display_name: u.display_name.clone(),
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
html! {
|
|
|
|
<AddGroupMemberComponent
|
|
|
|
group_id=g.id
|
|
|
|
users=users
|
2021-10-31 12:19:54 +00:00
|
|
|
on_error=self.common.callback(Msg::OnError)
|
|
|
|
on_user_added_to_group=self.common.callback(Msg::OnUserAddedToGroup)/>
|
2021-10-29 03:32:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CommonComponent<GroupDetails> for GroupDetails {
|
|
|
|
fn handle_msg(&mut self, msg: <Self as Component>::Message) -> Result<bool> {
|
|
|
|
match msg {
|
|
|
|
Msg::GroupDetailsResponse(response) => match response {
|
|
|
|
Ok(group) => self.group = Some(group.group),
|
|
|
|
Err(e) => {
|
|
|
|
self.group = None;
|
|
|
|
bail!("Error getting user details: {}", e);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Msg::OnError(e) => return Err(e),
|
|
|
|
Msg::OnUserAddedToGroup(user) => {
|
|
|
|
self.group.as_mut().unwrap().users.push(User {
|
|
|
|
id: user.id,
|
|
|
|
display_name: user.display_name,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
Msg::OnUserRemovedFromGroup((user_id, _)) => {
|
|
|
|
self.group
|
|
|
|
.as_mut()
|
|
|
|
.unwrap()
|
|
|
|
.users
|
|
|
|
.retain(|u| u.id != user_id);
|
|
|
|
}
|
2021-10-11 16:41:55 +00:00
|
|
|
}
|
2021-10-29 03:32:52 +00:00
|
|
|
Ok(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn mut_common(&mut self) -> &mut CommonComponentParts<Self> {
|
|
|
|
&mut self.common
|
2021-10-11 16:41:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Component for GroupDetails {
|
|
|
|
type Message = Msg;
|
|
|
|
type Properties = Props;
|
|
|
|
|
|
|
|
fn create(props: Self::Properties, link: ComponentLink<Self>) -> Self {
|
|
|
|
let mut table = Self {
|
2021-10-29 03:32:52 +00:00
|
|
|
common: CommonComponentParts::<Self>::create(props, link),
|
2021-10-11 16:41:55 +00:00
|
|
|
group: None,
|
|
|
|
};
|
|
|
|
table.get_group_details();
|
|
|
|
table
|
|
|
|
}
|
|
|
|
|
|
|
|
fn update(&mut self, msg: Self::Message) -> ShouldRender {
|
2021-10-29 03:32:52 +00:00
|
|
|
CommonComponentParts::<Self>::update(self, msg)
|
2021-10-11 16:41:55 +00:00
|
|
|
}
|
|
|
|
|
2022-06-30 07:27:51 +00:00
|
|
|
fn change(&mut self, props: Self::Properties) -> ShouldRender {
|
|
|
|
self.common.change(props)
|
2021-10-11 16:41:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn view(&self) -> Html {
|
2021-10-29 03:32:52 +00:00
|
|
|
match (&self.group, &self.common.error) {
|
2021-10-11 16:41:55 +00:00
|
|
|
(None, None) => html! {{"Loading..."}},
|
|
|
|
(None, Some(e)) => html! {<div>{"Error: "}{e.to_string()}</div>},
|
|
|
|
(Some(u), error) => {
|
|
|
|
html! {
|
|
|
|
<div>
|
2022-07-21 10:10:37 +00:00
|
|
|
{self.view_details(u)}
|
2021-10-11 16:41:55 +00:00
|
|
|
{self.view_user_list(u)}
|
2021-10-11 17:20:13 +00:00
|
|
|
{self.view_add_user_button(u)}
|
2021-10-11 16:41:55 +00:00
|
|
|
{self.view_messages(error)}
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|