From d9f824cc85c9634a8a85818c620e25beaf7e17bd Mon Sep 17 00:00:00 2001 From: Hazem Krimi Date: Fri, 7 May 2021 01:44:33 +0100 Subject: [PATCH] Add create user page --- src/pages/CreateUser/index.tsx | 479 +++++++++++++++++++++++++++++++++ src/pages/CreateUser/styles.ts | 9 + src/pages/index.tsx | 2 + 3 files changed, 490 insertions(+) create mode 100644 src/pages/CreateUser/index.tsx create mode 100644 src/pages/CreateUser/styles.ts diff --git a/src/pages/CreateUser/index.tsx b/src/pages/CreateUser/index.tsx new file mode 100644 index 0000000..52b4586 --- /dev/null +++ b/src/pages/CreateUser/index.tsx @@ -0,0 +1,479 @@ +import * as Yup from 'yup'; +import { useFormik } from 'formik'; +import { Redirect, useHistory, useParams } from 'react-router'; +import { useMutation, useQuery, useReactiveVar } from '@apollo/client'; +import { useState } from 'react'; +import { roleVar } from '../../graphql/state'; +import { + Box, + Button, + Text, + SectionSelector, + Input, + Select, + Alert, + Spinner, +} from '../../components'; +import { Wrapper } from './styles'; +import { ArrowLeft, Profile, Security } from '../../assets'; +import { + GetCountryCodesQuery, + GetCountryCodesQueryVariables, + CreateUserMutation, + CreateUserMutationVariables, +} from '../../graphql/types'; +import { GET_COUNTRY_CODES } from '../../graphql/auth.api'; +import { CREATE_USER, GET_ALL_USERS } from '../../graphql/admin.api'; + +const CreateUser = () => { + const history = useHistory(); + const { role: newUserRole } = useParams<{ + role: 'Client' | 'ProductOwner' | 'Developer'; + }>(); + const role = useReactiveVar(roleVar); + const [newUser, setNewUser] = useState<{ + firstName: string; + lastName: string; + email: string; + password: string; + phone: { + prefix: string; + number: string; + }; + address: { + place: string; + city: string; + country: string; + zip: string; + }; + role: 'Client' | 'ProductOwner' | 'Developer'; + }>({ + firstName: '', + lastName: '', + email: '', + password: '', + phone: { + prefix: '', + number: '', + }, + address: { + place: '', + city: '', + country: '', + zip: '', + }, + role: newUserRole, + }); + + const [selectedSection, setSelectedSection] = useState< + 'general' | 'security' + >('general'); + const [error, setError] = useState(''); + + const [createUser, { loading: createUserLoading }] = useMutation< + CreateUserMutation, + CreateUserMutationVariables + >(CREATE_USER, { + onCompleted() { + const location = + newUserRole === 'Client' + ? '/clients' + : newUserRole === 'ProductOwner' + ? '/product-owners' + : '/developers'; + history.push(location); + }, + onError({ graphQLErrors }) { + setError(graphQLErrors[0]?.extensions?.info); + setTimeout(() => setError(''), 3000); + }, + refetchQueries: [{ query: GET_ALL_USERS }], + }); + + const generalForm = useFormik({ + initialValues: { + firstName: '', + lastName: '', + email: '', + prefix: '', + number: '', + place: '', + city: '', + zip: '', + country: '', + }, + validationSchema: Yup.object().shape({ + firstName: Yup.string().required('First Name is required'), + lastName: Yup.string().required('Last Name is required'), + email: Yup.string() + .required('Email is required') + .email('Email is invalid'), + prefix: Yup.string().required('Prefix is required'), + // prettier-ignore + number: Yup.number().typeError('Phone must be a number').required('Phone is required'), + place: Yup.string().required('Address is required'), + city: Yup.string().required('City is required'), + country: Yup.string().required('Country is required'), + // prettier-ignore + zip: Yup.number().typeError('Zip must be a number').required('Zip is required'), + }), + onSubmit: ({ + firstName, + lastName, + email, + prefix, + number, + place, + city, + country, + zip, + }) => { + setNewUser({ + ...newUser, + firstName, + lastName, + email, + phone: { prefix, number }, + address: { place, city, country, zip }, + }); + setSelectedSection('security'); + }, + enableReinitialize: true, + }); + + const { data: countryCodes, loading: countryCodesLoading } = useQuery< + GetCountryCodesQuery, + GetCountryCodesQueryVariables + >(GET_COUNTRY_CODES, { + onCompleted({ getCountryCode }) { + generalForm.setFieldValue('prefix', getCountryCode[0].prefix); + generalForm.setFieldValue('country', getCountryCode[0].country); + }, + }); + + const securityForm = useFormik({ + initialValues: { + password: '', + confirmPassword: '', + }, + validationSchema: Yup.object().shape({ + password: Yup.string() + .required('Password is required') + .min(6, 'Password is 6 characters minimum'), + confirmPassword: Yup.string() + .required('Confirm password is required') + .oneOf( + [Yup.ref('password')], + "Confirm new password doesn't match with new password" + ), + }), + onSubmit: ({ password }) => { + setNewUser({ ...newUser, password }); + createUser({ variables: { ...newUser } }); + }, + }); + + return role === 'admin' ? ( + + +