From 0364ad3c9a03803e2ef7d8dd2a41a576977ebb6c Mon Sep 17 00:00:00 2001 From: Hazem Krimi Date: Wed, 26 May 2021 19:51:42 +0100 Subject: [PATCH] Add delete category functionality --- src/pages/CategorySettings/index.tsx | 39 +++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/src/pages/CategorySettings/index.tsx b/src/pages/CategorySettings/index.tsx index 1301ea7..8f1556f 100644 --- a/src/pages/CategorySettings/index.tsx +++ b/src/pages/CategorySettings/index.tsx @@ -13,17 +13,21 @@ import { Alert, TextArea, Spinner, + Modal, } from '../../components'; import { Wrapper } from './styles'; import { ArrowLeft, General } from '../../assets'; import { CategoryOutput, + DeleteCategoryMutation, + DeleteCategoryMutationVariables, GetCategoryByIdQuery, GetCategoryByIdQueryVariables, UpdateCategoryMutation, UpdateCategoryMutationVariables, } from '../../graphql/types'; import { + DELETE_CATEGORY, GET_CATEGORY_BY_ID, UPDATE_CATEGORY, } from '../../graphql/category.api'; @@ -37,6 +41,10 @@ const CategorySettings = () => { const [success, setSuccess] = useState(false); const [category, setCategory] = useState(); + const [deleteCategoryModal, setDeleteCategoryModal] = useState( + false + ); + const [getCategory, { loading: categoryLoading }] = useLazyQuery< GetCategoryByIdQuery, GetCategoryByIdQueryVariables @@ -61,6 +69,19 @@ const CategorySettings = () => { }, }); + const [deleteCategory] = useMutation< + DeleteCategoryMutation, + DeleteCategoryMutationVariables + >(DELETE_CATEGORY, { + onCompleted() { + history.push('/category'); + }, + onError({ graphQLErrors }) { + setError(graphQLErrors[0]?.extensions?.info); + setTimeout(() => setError(''), 3000); + }, + }); + useEffect(() => { getCategory({ variables: { id } }); @@ -97,6 +118,16 @@ const CategorySettings = () => { return role === 'developer' ? ( + {deleteCategoryModal && ( + setDeleteCategoryModal(false)} + onConfirm={() => deleteCategory({ variables: { id } })} + > + )}