diff --git a/src/pages/AddCategory/index.tsx b/src/pages/AddCategory/index.tsx new file mode 100644 index 0000000..ad89b8c --- /dev/null +++ b/src/pages/AddCategory/index.tsx @@ -0,0 +1,195 @@ +import * as Yup from 'yup'; +import { useFormik } from 'formik'; +import { Redirect, useHistory } from 'react-router'; +import { useMutation, useReactiveVar } from '@apollo/client'; +import React, { useState } from 'react'; +import { roleVar } from '../../graphql/state'; +import { + Box, + Button, + Text, + SectionSelector, + Input, + Alert, + TextArea, +} from '../../components'; +import { Wrapper } from './styles'; +import { ArrowLeft, General } from '../../assets'; +import { + AddCategoryMutation, + AddCategoryMutationVariables, +} from '../../graphql/types'; +import { ADD_CATEGORY } from '../../graphql/category.api'; + +const AddCategory = () => { + const history = useHistory(); + const role = useReactiveVar(roleVar); + + const [error, setError] = useState(''); + + const [addCategory, { loading }] = useMutation< + AddCategoryMutation, + AddCategoryMutationVariables + >(ADD_CATEGORY, { + onCompleted({ addCategory: { id } }) { + history.push(`/category/${id}`); + }, + onError({ graphQLErrors }) { + setError(graphQLErrors[0]?.extensions?.info); + setTimeout(() => setError(''), 3000); + }, + }); + + const form = useFormik({ + initialValues: { + name: '', + description: '', + imageName: '', + imageSource: '', + }, + validationSchema: Yup.object().shape({ + name: Yup.string().required('Name is required'), + description: Yup.string().required('Description is required'), + imageName: Yup.string().required('Image is required'), + imageSource: Yup.string().required('Image is required'), + }), + onSubmit: ({ name, description, imageName, imageSource }) => { + addCategory({ + variables: { + category: { + name, + description, + image: { name: imageName, src: imageSource }, + }, + }, + }); + }, + }); + + return role === 'developer' ? ( + + +