From 768ba766d076698edbd0ba62835fd619dfe7e77b Mon Sep 17 00:00:00 2001 From: Hazem Krimi Date: Wed, 16 Jun 2021 00:41:14 +0100 Subject: [PATCH] Add update project page --- src/pages/UpdateProject/index.tsx | 218 ++++++++++++++++++++++++++++++ src/pages/UpdateProject/styles.ts | 18 +++ src/pages/index.tsx | 2 + 3 files changed, 238 insertions(+) create mode 100644 src/pages/UpdateProject/index.tsx create mode 100644 src/pages/UpdateProject/styles.ts diff --git a/src/pages/UpdateProject/index.tsx b/src/pages/UpdateProject/index.tsx new file mode 100644 index 0000000..6042fcd --- /dev/null +++ b/src/pages/UpdateProject/index.tsx @@ -0,0 +1,218 @@ +import * as Yup from 'yup'; +import { useFormik } from 'formik'; +import { useHistory, useParams } from 'react-router'; +import { useLazyQuery, useMutation, useReactiveVar } from '@apollo/client'; +import { useState, useEffect } from 'react'; +import { roleVar } from '../../graphql/state'; +import { Wrapper } from './styles'; +import { Alert, Box, Button, Input, Spinner, Text } from '../../components'; +import { + GetProjectByIdQuery, + GetProjectByIdQueryVariables, + ProjectOutput, + UpdateProjectMutation, + UpdateProjectMutationVariables, +} from '../../graphql/types'; +import { GET_PROJECT_BY_ID, UPDATE_PROJECT } from '../../graphql/project.api'; +import { ArrowLeft } from '../../assets'; +import { theme } from '../../themes'; + +const UpdateProject = () => { + const history = useHistory(); + const role = useReactiveVar(roleVar); + const [error, setError] = useState(''); + const [project, setProject] = useState(); + const { id } = useParams<{ id: string }>(); + + const [getProject, { loading: projectLoading }] = useLazyQuery< + GetProjectByIdQuery, + GetProjectByIdQueryVariables + >(GET_PROJECT_BY_ID, { + onCompleted({ getProjectById }) { + setProject(getProjectById); + }, + fetchPolicy: 'network-only', + }); + + const [updateProject, { loading: updateProjectLoading }] = useMutation< + UpdateProjectMutation, + UpdateProjectMutationVariables + >(UPDATE_PROJECT, { + onCompleted() { + history.push(`/project/${id}`); + }, + onError({ graphQLErrors }) { + setError(graphQLErrors[0].extensions?.info); + setTimeout(() => setError(''), 3000); + }, + }); + + useEffect(() => { + if (id) { + getProject({ variables: { id } }); + } + + // eslint-disable-next-line + }, [id]); + + const basicInfoForm = useFormik({ + initialValues: { + name: project?.name || '', + imageName: project?.image.name || '', + imageSource: project?.image.src || '', + }, + validationSchema: Yup.object().shape({ + name: Yup.string().required('Name is required'), + imageName: Yup.string().required('Image is required'), + imageSource: Yup.string().required('Image is required'), + }), + onSubmit: ({ name, imageName, imageSource }) => { + updateProject({ + variables: { + id, + name, + image: { name: imageName, src: imageSource }, + }, + }); + }, + enableReinitialize: true, + }); + + return !projectLoading ? ( + + + + +