From bbed87bc341ac49f02143f26780e2344186f55aa Mon Sep 17 00:00:00 2001
From: lcross2002 <liamdcross@outlook.com>
Date: Sun, 7 Apr 2024 13:39:49 +0100
Subject: [PATCH] Wired up update user form

---
 .../components/Dashboard/Profile/Profile.tsx  | 34 ++++++++++++++++---
 client/src/services/PatchUser/PatchUser.ts    | 10 ++++++
 2 files changed, 40 insertions(+), 4 deletions(-)
 create mode 100644 client/src/services/PatchUser/PatchUser.ts

diff --git a/client/src/components/Dashboard/Profile/Profile.tsx b/client/src/components/Dashboard/Profile/Profile.tsx
index f289594..7edf70f 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 0000000..1630b97
--- /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 });
+}
-- 
GitLab