2021-09-28 13:00:39 +00:00
|
|
|
use crate::{
|
2021-10-11 16:41:55 +00:00
|
|
|
components::{
|
|
|
|
delete_group::DeleteGroup,
|
|
|
|
router::{AppRoute, Link},
|
|
|
|
},
|
2021-10-31 13:34:27 +00:00
|
|
|
infra::common_component::{CommonComponent, CommonComponentParts},
|
2021-09-28 13:00:39 +00:00
|
|
|
};
|
|
|
|
use anyhow::{Error, Result};
|
|
|
|
use graphql_client::GraphQLQuery;
|
|
|
|
use yew::prelude::*;
|
|
|
|
|
|
|
|
#[derive(GraphQLQuery)]
|
|
|
|
#[graphql(
|
|
|
|
schema_path = "../schema.graphql",
|
|
|
|
query_path = "queries/get_group_list.graphql",
|
2021-10-11 16:41:55 +00:00
|
|
|
response_derives = "Debug,Clone,PartialEq",
|
2021-09-28 13:00:39 +00:00
|
|
|
custom_scalars_module = "crate::infra::graphql"
|
|
|
|
)]
|
|
|
|
pub struct GetGroupList;
|
|
|
|
|
|
|
|
use get_group_list::ResponseData;
|
|
|
|
|
2021-10-11 16:41:55 +00:00
|
|
|
pub type Group = get_group_list::GetGroupListGroups;
|
2021-09-28 13:00:39 +00:00
|
|
|
|
|
|
|
pub struct GroupTable {
|
2021-10-31 13:34:27 +00:00
|
|
|
common: CommonComponentParts<Self>,
|
2021-09-28 13:00:39 +00:00
|
|
|
groups: Option<Vec<Group>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub enum Msg {
|
|
|
|
ListGroupsResponse(Result<ResponseData>),
|
|
|
|
OnGroupDeleted(i64),
|
|
|
|
OnError(Error),
|
|
|
|
}
|
|
|
|
|
2021-10-31 13:34:27 +00:00
|
|
|
impl CommonComponent<GroupTable> for GroupTable {
|
|
|
|
fn handle_msg(&mut self, msg: <Self as Component>::Message) -> Result<bool> {
|
|
|
|
match msg {
|
|
|
|
Msg::ListGroupsResponse(groups) => {
|
|
|
|
self.groups = Some(groups?.groups.into_iter().collect());
|
|
|
|
Ok(true)
|
|
|
|
}
|
|
|
|
Msg::OnError(e) => Err(e),
|
|
|
|
Msg::OnGroupDeleted(group_id) => {
|
|
|
|
debug_assert!(self.groups.is_some());
|
|
|
|
self.groups.as_mut().unwrap().retain(|u| u.id != group_id);
|
|
|
|
Ok(true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn mut_common(&mut self) -> &mut CommonComponentParts<Self> {
|
|
|
|
&mut self.common
|
2021-09-28 13:00:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Component for GroupTable {
|
|
|
|
type Message = Msg;
|
|
|
|
type Properties = ();
|
|
|
|
|
2021-10-31 13:34:27 +00:00
|
|
|
fn create(props: Self::Properties, link: ComponentLink<Self>) -> Self {
|
2021-09-28 13:00:39 +00:00
|
|
|
let mut table = GroupTable {
|
2021-10-31 13:34:27 +00:00
|
|
|
common: CommonComponentParts::<Self>::create(props, link),
|
2021-09-28 13:00:39 +00:00
|
|
|
groups: None,
|
|
|
|
};
|
2021-10-31 13:34:27 +00:00
|
|
|
table.common.call_graphql::<GetGroupList, _>(
|
|
|
|
get_group_list::Variables {},
|
|
|
|
Msg::ListGroupsResponse,
|
|
|
|
"Error trying to fetch groups",
|
|
|
|
);
|
2021-09-28 13:00:39 +00:00
|
|
|
table
|
|
|
|
}
|
|
|
|
|
|
|
|
fn update(&mut self, msg: Self::Message) -> ShouldRender {
|
2021-10-31 13:34:27 +00:00
|
|
|
CommonComponentParts::<Self>::update(self, msg)
|
2021-09-28 13:00:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn change(&mut self, _: Self::Properties) -> ShouldRender {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
|
|
|
fn view(&self) -> Html {
|
|
|
|
html! {
|
|
|
|
<div>
|
|
|
|
{self.view_groups()}
|
|
|
|
{self.view_errors()}
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl GroupTable {
|
|
|
|
fn view_groups(&self) -> Html {
|
|
|
|
let make_table = |groups: &Vec<Group>| {
|
|
|
|
html! {
|
|
|
|
<div class="table-responsive">
|
|
|
|
<table class="table table-striped">
|
|
|
|
<thead>
|
|
|
|
<tr>
|
2021-10-11 16:41:55 +00:00
|
|
|
<th>{"Groups"}</th>
|
|
|
|
<th>{"Delete"}</th>
|
2021-09-28 13:00:39 +00:00
|
|
|
</tr>
|
|
|
|
</thead>
|
|
|
|
<tbody>
|
|
|
|
{groups.iter().map(|u| self.view_group(u)).collect::<Vec<_>>()}
|
|
|
|
</tbody>
|
|
|
|
</table>
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
};
|
|
|
|
match &self.groups {
|
|
|
|
None => html! {{"Loading..."}},
|
|
|
|
Some(groups) => make_table(groups),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn view_group(&self, group: &Group) -> Html {
|
|
|
|
html! {
|
2021-10-11 16:41:55 +00:00
|
|
|
<tr key=group.id>
|
|
|
|
<td>
|
|
|
|
<Link route=AppRoute::GroupDetails(group.id)>
|
|
|
|
{&group.display_name}
|
|
|
|
</Link>
|
|
|
|
</td>
|
|
|
|
<td>
|
|
|
|
<DeleteGroup
|
|
|
|
group=group.clone()
|
2021-10-31 13:34:27 +00:00
|
|
|
on_group_deleted=self.common.callback(Msg::OnGroupDeleted)
|
|
|
|
on_error=self.common.callback(Msg::OnError)/>
|
2021-10-11 16:41:55 +00:00
|
|
|
</td>
|
2021-09-28 13:00:39 +00:00
|
|
|
</tr>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn view_errors(&self) -> Html {
|
2021-10-31 13:34:27 +00:00
|
|
|
match &self.common.error {
|
2021-09-28 13:00:39 +00:00
|
|
|
None => html! {},
|
|
|
|
Some(e) => html! {<div>{"Error: "}{e.to_string()}</div>},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|