diff --git a/src/pages/AddFeature/index.tsx b/src/pages/AddFeature/index.tsx new file mode 100644 index 0000000..3f7dd33 --- /dev/null +++ b/src/pages/AddFeature/index.tsx @@ -0,0 +1,386 @@ +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, + CheckBox, +} from '../../components'; +import { Wrapper } from './styles'; +import { ArrowLeft, General, Design } from '../../assets'; +import { + AddFeatureMutation, + AddFeatureMutationVariables, +} from '../../graphql/types'; +import { ADD_CATEGORY } from '../../graphql/category.api'; +import { ADD_FEATURE } from '../../graphql/feature.api'; + +const AddFeature = () => { + const history = useHistory(); + const role = useReactiveVar(roleVar); + const [newFeature, setNewFeature] = useState<{ + name: string; + description: string; + featureType: string; + image: { + name: string; + src: string; + }; + wireframes?: Array<{ + name: string; + src: string; + }>; + price: number; + repo: string; + }>(); + + const [selectedSection, setSelectedSection] = useState< + 'general' | 'wireframes' + >('general'); + const [error, setError] = useState(''); + + const [addFeature, { loading }] = useMutation< + AddFeatureMutation, + AddFeatureMutationVariables + >(ADD_FEATURE, { + onCompleted({ addFeature: { id } }) { + history.push(`/feature/${id}`); + }, + onError({ graphQLErrors }) { + setError(graphQLErrors[0]?.extensions?.info); + setTimeout(() => setError(''), 3000); + }, + }); + + const generalForm = useFormik({ + initialValues: { + name: '', + description: '', + imageName: '', + imageSource: '', + featureType: '', + price: '', + repo: '', + }, + 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'), + featureType: Yup.string().required('Feature Type is required'), + // prettier-ignore + price: Yup.number().typeError('Price must be a number').required('Price is required'), + repo: Yup.string().required('Repo is required'), + }), + onSubmit: ({ + name, + description, + imageName, + imageSource, + featureType, + price, + repo, + }) => { + setNewFeature({ + name, + description, + featureType, + image: { name: imageName, src: imageSource }, + price: parseFloat(price), + repo, + }); + setSelectedSection('wireframes'); + }, + }); + + return role === 'developer' ? ( + + +