Skip to content
Snippets Groups Projects
Commit bbed87bc authored by lcross2002's avatar lcross2002
Browse files

Wired up update user form

parent 4a576de9
No related branches found
No related tags found
No related merge requests found
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 (
......
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 });
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment