diff --git a/client/src/components/Dashboard/Profile/Profile.tsx b/client/src/components/Dashboard/Profile/Profile.tsx index f2895940b8ac6f2ff3fe2867d309e11ed089b6c8..7edf70fced658aa8475d47f7eaeb2950e2f13169 100644 --- a/client/src/components/Dashboard/Profile/Profile.tsx +++ b/client/src/components/Dashboard/Profile/Profile.tsx @@ -1,11 +1,13 @@ import { useState } from 'react'; import { useForm } from 'react-hook-form'; +import { AxiosError } from 'axios'; import { useAuth } from '../../../hooks/useAuth'; import avatar from './avatar.jpg'; import plane from './airplane.jpg'; +import { patchUser } from '../../../services/PatchUser/PatchUser'; import './Profile.scss'; -interface IProfileForm { +export interface IProfileForm { name: string; email: string; password: string; @@ -13,7 +15,7 @@ interface IProfileForm { } function Profile() { - const { user } = useAuth(); + const { user, updateUser } = useAuth(); const [disabled, setDisabled] = useState(true); const [error, setError] = useState(''); const isAirline = user?.type === 1; @@ -46,7 +48,7 @@ function Profile() { resetFormValues(); }; - const onSubmit = (formValue: IProfileForm) => { + const onSubmit = async (formValue: IProfileForm) => { if (formValue.password.length < 7) { setError('password length must be greater than 7 characters'); return; @@ -58,7 +60,31 @@ function Profile() { } setError(''); - console.log('ready to make update details api call'); + + try { + if (user) { + await patchUser(formValue, user.id); + + updateUser({ + id: user.id, + username: formValue.name, + email: formValue.email, + type: user.type + }); + toggleEdit(); + + } else { + setError('No user id, please relog'); + } + } catch (error) { + const errorMessage = (error as AxiosError).response?.data; + + if (typeof errorMessage == 'string') { + setError(errorMessage); + } else { + setError('An unexpected error has occurred'); + } + } }; return ( diff --git a/client/src/services/PatchUser/PatchUser.ts b/client/src/services/PatchUser/PatchUser.ts new file mode 100644 index 0000000000000000000000000000000000000000..1630b97cfa4499bd47221aa3c77d7026c155dda0 --- /dev/null +++ b/client/src/services/PatchUser/PatchUser.ts @@ -0,0 +1,10 @@ +import Api from '../../helpers/Api'; +import { IProfileForm } from '../../components/Dashboard/Profile/Profile'; + +export function patchUser(form: IProfileForm, id: number) { + return Api.patch(`User/${id}`, { + Username: form.name, + Email: form.email, + Password: form.password + }, { withCredentials: true }); +}