diff --git a/src/pages/Auth/AdditionalInfo/index.tsx b/src/pages/Auth/AdditionalInfo/index.tsx index 3ddf5d1..83bcf10 100644 --- a/src/pages/Auth/AdditionalInfo/index.tsx +++ b/src/pages/Auth/AdditionalInfo/index.tsx @@ -2,21 +2,35 @@ import * as Yup from 'yup'; import { useFormik } from 'formik'; import { useState } from 'react'; import { useHistory } from 'react-router-dom'; -import { useMutation, useReactiveVar } from '@apollo/client'; -import { Box, Button, Input, Select, Text, Alert } from '../../../components'; +import { useMutation, useQuery, useReactiveVar } from '@apollo/client'; +import { + Box, + Button, + Input, + Select, + Text, + Alert, + Spinner, +} from '../../../components'; import { theme } from '../../../themes'; import { Wrapper } from './styles'; import { + GetCountryCodesQuery, + GetCountryCodesQueryVariables, UpdateUserInfoMutation, UpdateUserInfoMutationVariables, } from '../../../graphql/types'; -import { UPDATE_USER_INFO } from '../../../graphql/auth.api'; +import { GET_COUNTRY_CODES, UPDATE_USER_INFO } from '../../../graphql/auth.api'; import { userVar } from '../../../graphql/state'; const AdditionalInfo = () => { const history = useHistory(); const [error, setError] = useState(''); const currentUser = useReactiveVar(userVar); + const { data: countryCodes, loading: countryCodesLoading } = useQuery< + GetCountryCodesQuery, + GetCountryCodesQueryVariables + >(GET_COUNTRY_CODES); const [updateUserInfo, { loading }] = useMutation< UpdateUserInfoMutation, @@ -47,33 +61,38 @@ const AdditionalInfo = () => { firstName: Yup.string().required('First Name is required'), lastName: Yup.string().required('Last Name is required'), prefix: Yup.string().required('Prefix is required'), - number: Yup.number().required('Number is required'), + number: Yup.number() + // prettier-ignore + .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'), + zip: Yup.number() + // prettier-ignore + .typeError('Zip must be a number') + .required('Zip is required'), }), - onSubmit: ( - { firstName, lastName, prefix, number, place, city, country, zip }, - { resetForm } - ) => { - try { - updateUserInfo({ - variables: { - id: currentUser?.id!, - email: currentUser?.email!, - firstName, - lastName, - phone: { prefix, number }, - address: { place, city, country, zip }, - }, - }); - } catch (err) { - setError(err.message); - setTimeout(() => setError(''), 3000); - } finally { - resetForm(); - } - }, + onSubmit: ({ + firstName, + lastName, + prefix, + number, + place, + city, + country, + zip, + }) => + updateUserInfo({ + variables: { + id: currentUser?.id!, + email: currentUser?.email!, + firstName, + lastName, + phone: { prefix, number }, + address: { place, city, country, zip }, + }, + }), }); return ( @@ -94,104 +113,160 @@ const AdditionalInfo = () => { Tell us more about yourself -
- - - + {!countryCodesLoading ? ( + - - - - - - - -