mirror of
				https://github.com/nitnelave/lldap.git
				synced 2023-04-12 14:25:13 +00:00 
			
		
		
		
	app: Migrate GroupTable to CommonComponent
This commit is contained in:
		
							parent
							
								
									53f56ee1de
								
							
						
					
					
						commit
						15c1d7214d
					
				@ -3,12 +3,11 @@ use crate::{
 | 
				
			|||||||
        delete_group::DeleteGroup,
 | 
					        delete_group::DeleteGroup,
 | 
				
			||||||
        router::{AppRoute, Link},
 | 
					        router::{AppRoute, Link},
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
    infra::api::HostService,
 | 
					    infra::common_component::{CommonComponent, CommonComponentParts},
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
use anyhow::{Error, Result};
 | 
					use anyhow::{Error, Result};
 | 
				
			||||||
use graphql_client::GraphQLQuery;
 | 
					use graphql_client::GraphQLQuery;
 | 
				
			||||||
use yew::prelude::*;
 | 
					use yew::prelude::*;
 | 
				
			||||||
use yew::services::{fetch::FetchTask, ConsoleService};
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
#[derive(GraphQLQuery)]
 | 
					#[derive(GraphQLQuery)]
 | 
				
			||||||
#[graphql(
 | 
					#[graphql(
 | 
				
			||||||
@ -24,11 +23,8 @@ use get_group_list::ResponseData;
 | 
				
			|||||||
pub type Group = get_group_list::GetGroupListGroups;
 | 
					pub type Group = get_group_list::GetGroupListGroups;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
pub struct GroupTable {
 | 
					pub struct GroupTable {
 | 
				
			||||||
    link: ComponentLink<Self>,
 | 
					    common: CommonComponentParts<Self>,
 | 
				
			||||||
    groups: Option<Vec<Group>>,
 | 
					    groups: Option<Vec<Group>>,
 | 
				
			||||||
    error: Option<Error>,
 | 
					 | 
				
			||||||
    // Used to keep the request alive long enough.
 | 
					 | 
				
			||||||
    _task: Option<FetchTask>,
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
pub enum Msg {
 | 
					pub enum Msg {
 | 
				
			||||||
@ -37,18 +33,24 @@ pub enum Msg {
 | 
				
			|||||||
    OnError(Error),
 | 
					    OnError(Error),
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
impl GroupTable {
 | 
					impl CommonComponent<GroupTable> for GroupTable {
 | 
				
			||||||
    fn get_groups(&mut self) {
 | 
					    fn handle_msg(&mut self, msg: <Self as Component>::Message) -> Result<bool> {
 | 
				
			||||||
        self._task = HostService::graphql_query::<GetGroupList>(
 | 
					        match msg {
 | 
				
			||||||
            get_group_list::Variables {},
 | 
					            Msg::ListGroupsResponse(groups) => {
 | 
				
			||||||
            self.link.callback(Msg::ListGroupsResponse),
 | 
					                self.groups = Some(groups?.groups.into_iter().collect());
 | 
				
			||||||
            "Error trying to fetch groups",
 | 
					                Ok(true)
 | 
				
			||||||
        )
 | 
					            }
 | 
				
			||||||
        .map_err(|e| {
 | 
					            Msg::OnError(e) => Err(e),
 | 
				
			||||||
            ConsoleService::log(&e.to_string());
 | 
					            Msg::OnGroupDeleted(group_id) => {
 | 
				
			||||||
            e
 | 
					                debug_assert!(self.groups.is_some());
 | 
				
			||||||
        })
 | 
					                self.groups.as_mut().unwrap().retain(|u| u.id != group_id);
 | 
				
			||||||
        .ok();
 | 
					                Ok(true)
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    fn mut_common(&mut self) -> &mut CommonComponentParts<Self> {
 | 
				
			||||||
 | 
					        &mut self.common
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -56,27 +58,21 @@ impl Component for GroupTable {
 | 
				
			|||||||
    type Message = Msg;
 | 
					    type Message = Msg;
 | 
				
			||||||
    type Properties = ();
 | 
					    type Properties = ();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    fn create(_: Self::Properties, link: ComponentLink<Self>) -> Self {
 | 
					    fn create(props: Self::Properties, link: ComponentLink<Self>) -> Self {
 | 
				
			||||||
        let mut table = GroupTable {
 | 
					        let mut table = GroupTable {
 | 
				
			||||||
            link,
 | 
					            common: CommonComponentParts::<Self>::create(props, link),
 | 
				
			||||||
            _task: None,
 | 
					 | 
				
			||||||
            groups: None,
 | 
					            groups: None,
 | 
				
			||||||
            error: None,
 | 
					 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
        table.get_groups();
 | 
					        table.common.call_graphql::<GetGroupList, _>(
 | 
				
			||||||
 | 
					            get_group_list::Variables {},
 | 
				
			||||||
 | 
					            Msg::ListGroupsResponse,
 | 
				
			||||||
 | 
					            "Error trying to fetch groups",
 | 
				
			||||||
 | 
					        );
 | 
				
			||||||
        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 {
 | 
				
			||||||
@ -94,21 +90,6 @@ impl Component for GroupTable {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
impl GroupTable {
 | 
					impl 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 view_groups(&self) -> Html {
 | 
					    fn view_groups(&self) -> Html {
 | 
				
			||||||
        let make_table = |groups: &Vec<Group>| {
 | 
					        let make_table = |groups: &Vec<Group>| {
 | 
				
			||||||
            html! {
 | 
					            html! {
 | 
				
			||||||
@ -144,15 +125,15 @@ impl GroupTable {
 | 
				
			|||||||
              <td>
 | 
					              <td>
 | 
				
			||||||
                <DeleteGroup
 | 
					                <DeleteGroup
 | 
				
			||||||
                  group=group.clone()
 | 
					                  group=group.clone()
 | 
				
			||||||
                  on_group_deleted=self.link.callback(Msg::OnGroupDeleted)
 | 
					                  on_group_deleted=self.common.callback(Msg::OnGroupDeleted)
 | 
				
			||||||
                  on_error=self.link.callback(Msg::OnError)/>
 | 
					                  on_error=self.common.callback(Msg::OnError)/>
 | 
				
			||||||
              </td>
 | 
					              </td>
 | 
				
			||||||
          </tr>
 | 
					          </tr>
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    fn view_errors(&self) -> Html {
 | 
					    fn view_errors(&self) -> Html {
 | 
				
			||||||
        match &self.error {
 | 
					        match &self.common.error {
 | 
				
			||||||
            None => html! {},
 | 
					            None => html! {},
 | 
				
			||||||
            Some(e) => html! {<div>{"Error: "}{e.to_string()}</div>},
 | 
					            Some(e) => html! {<div>{"Error: "}{e.to_string()}</div>},
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user