From 881ea34dfe222a565999563bb863a1d402130622 Mon Sep 17 00:00:00 2001 From: Mihir Shah <48630359+MihirGH@users.noreply.github.com> Date: Thu, 3 Nov 2022 06:01:13 +0530 Subject: [PATCH] chore: convert AddGroupToCollection component to functional component (#4204) (#4360) --- .../AddGroupsToCollection.tsx | 212 +++++++++--------- 1 file changed, 108 insertions(+), 104 deletions(-) diff --git a/app/scenes/CollectionPermissions/AddGroupsToCollection.tsx b/app/scenes/CollectionPermissions/AddGroupsToCollection.tsx index 59059f52c..69fe235bd 100644 --- a/app/scenes/CollectionPermissions/AddGroupsToCollection.tsx +++ b/app/scenes/CollectionPermissions/AddGroupsToCollection.tsx @@ -1,10 +1,8 @@ import { debounce } from "lodash"; -import { observable } from "mobx"; import { observer } from "mobx-react"; import * as React from "react"; -import { withTranslation, WithTranslation } from "react-i18next"; +import { useTranslation } from "react-i18next"; import styled from "styled-components"; -import RootStore from "~/stores/RootStore"; import Collection from "~/models/Collection"; import Group from "~/models/Group"; import GroupNew from "~/scenes/GroupNew"; @@ -17,50 +15,59 @@ import Input from "~/components/Input"; import Modal from "~/components/Modal"; import PaginatedList from "~/components/PaginatedList"; import Text from "~/components/Text"; -import withStores from "~/components/withStores"; +import useBoolean from "~/hooks/useBoolean"; +import useStores from "~/hooks/useStores"; -type Props = WithTranslation & - RootStore & { - collection: Collection; - onSubmit: () => void; - }; +type Props = { + collection: Collection; + onSubmit: () => void; +}; -@observer -class AddGroupsToCollection extends React.Component { - @observable - newGroupModalOpen = false; +function AddGroupsToCollection(props: Props) { + const { collection } = props; - @observable - query = ""; + const [ + newGroupModalOpen, + handleNewGroupModalOpen, + handleNewGroupModalClose, + ] = useBoolean(false); + const [query, setQuery] = React.useState(""); - handleNewGroupModalOpen = () => { - this.newGroupModalOpen = true; - }; + const { + auth, + collectionGroupMemberships, + groups, + policies, + toasts, + } = useStores(); + const { fetchPage: fetchGroups } = groups; - handleNewGroupModalClose = () => { - this.newGroupModalOpen = false; - }; + const { t } = useTranslation(); - handleFilter = (ev: React.ChangeEvent) => { - this.query = ev.target.value; - this.debouncedFetch(); - }; + const debouncedFetch = React.useMemo( + () => + debounce((query) => { + fetchGroups({ query }); + }, 250), + [fetchGroups] + ); - debouncedFetch = debounce(() => { - this.props.groups.fetchPage({ - query: this.query, - }); - }, 250); - - handleAddGroup = (group: Group) => { - const { t } = this.props; + const handleFilter = React.useCallback( + (ev: React.ChangeEvent) => { + const updatedQuery = ev.target.value; + setQuery(updatedQuery); + debouncedFetch(updatedQuery); + }, + [debouncedFetch] + ); + const handleAddGroup = (group: Group) => { try { - this.props.collectionGroupMemberships.create({ - collectionId: this.props.collection.id, + collectionGroupMemberships.create({ + collectionId: collection.id, groupId: group.id, }); - this.props.toasts.showToast( + toasts.showToast( t("{{ groupName }} was added to the collection", { groupName: group.name, }), @@ -69,84 +76,81 @@ class AddGroupsToCollection extends React.Component { } ); } catch (err) { - this.props.toasts.showToast(t("Could not add user"), { + toasts.showToast(t("Could not add user"), { type: "error", }); console.error(err); } }; - render() { - const { groups, policies, collection, auth, t } = this.props; - const { user, team } = auth; - if (!user || !team) { - return null; - } - - const can = policies.abilities(team.id); - - return ( - - {can.createGroup && ( - - {t("Can’t find the group you’re looking for?")}{" "} - - {t("Create a group")} - - . - - )} - - - {t("No groups matching your search")} - ) : ( - {t("No groups left to add")} - ) - } - items={groups.notInCollection(collection.id, this.query)} - fetch={this.query ? undefined : groups.fetchPage} - renderItem={(item: Group) => ( - ( - - - - )} - /> - )} - /> - {can.createGroup && ( - - - - )} - - ); + const { user, team } = auth; + if (!user || !team) { + return null; } + + const can = policies.abilities(team.id); + + return ( + + {can.createGroup ? ( + + {t("Can’t find the group you’re looking for?")}{" "} + + {t("Create a group")} + + . + + ) : null} + + + {t("No groups matching your search")} + ) : ( + {t("No groups left to add")} + ) + } + items={groups.notInCollection(collection.id, query)} + fetch={query ? undefined : fetchGroups} + renderItem={(item: Group) => ( + ( + + + + )} + /> + )} + /> + {can.createGroup ? ( + + + + ) : null} + + ); } const ButtonWrap = styled.div` margin-left: 6px; `; -export default withTranslation()(withStores(AddGroupsToCollection)); +export default observer(AddGroupsToCollection);