From 5fcd24db3e8c2aa793bb80925911f8980cca7f62 Mon Sep 17 00:00:00 2001 From: LordMathis Date: Sun, 11 May 2025 15:36:30 +0200 Subject: [PATCH] Migrate useProfileSettings hook --- app/src/hooks/useProfileSettings.js | 71 ----------------------- app/src/hooks/useProfileSettings.ts | 88 +++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+), 71 deletions(-) delete mode 100644 app/src/hooks/useProfileSettings.js create mode 100644 app/src/hooks/useProfileSettings.ts diff --git a/app/src/hooks/useProfileSettings.js b/app/src/hooks/useProfileSettings.js deleted file mode 100644 index 6ffcdec..0000000 --- a/app/src/hooks/useProfileSettings.js +++ /dev/null @@ -1,71 +0,0 @@ -import { useState, useCallback } from 'react'; -import { notifications } from '@mantine/notifications'; -import { updateProfile, deleteProfile } from '../api/git'; - -export function useProfileSettings() { - const [loading, setLoading] = useState(false); - - const handleProfileUpdate = useCallback(async (updates) => { - setLoading(true); - try { - const updatedUser = await updateProfile(updates); - - notifications.show({ - title: 'Success', - message: 'Profile updated successfully', - color: 'green', - }); - - return { success: true, user: updatedUser }; - } catch (error) { - let errorMessage = 'Failed to update profile'; - - if (error.message.includes('password')) { - errorMessage = 'Current password is incorrect'; - } else if (error.message.includes('email')) { - errorMessage = 'Email is already in use'; - } - - notifications.show({ - title: 'Error', - message: errorMessage, - color: 'red', - }); - - return { success: false, error: error.message }; - } finally { - setLoading(false); - } - }, []); - - const handleAccountDeletion = useCallback(async (password) => { - setLoading(true); - try { - await deleteProfile(password); - - notifications.show({ - title: 'Success', - message: 'Account deleted successfully', - color: 'green', - }); - - return { success: true }; - } catch (error) { - notifications.show({ - title: 'Error', - message: error.message || 'Failed to delete account', - color: 'red', - }); - - return { success: false, error: error.message }; - } finally { - setLoading(false); - } - }, []); - - return { - loading, - updateProfile: handleProfileUpdate, - deleteAccount: handleAccountDeletion, - }; -} diff --git a/app/src/hooks/useProfileSettings.ts b/app/src/hooks/useProfileSettings.ts new file mode 100644 index 0000000..fd9ecc4 --- /dev/null +++ b/app/src/hooks/useProfileSettings.ts @@ -0,0 +1,88 @@ +import { useState, useCallback } from 'react'; +import { notifications } from '@mantine/notifications'; +import { updateProfile, deleteUser } from '../api/user'; +import { User } from '../types/authApi'; +import { UpdateProfileRequest } from '../types/userApi'; + +interface UseProfileSettingsResult { + loading: boolean; + updateProfile: (updates: UpdateProfileRequest) => Promise; + deleteAccount: (password: string) => Promise; +} + +export function useProfileSettings(): UseProfileSettingsResult { + const [loading, setLoading] = useState(false); + + const handleProfileUpdate = useCallback( + async (updates: UpdateProfileRequest): Promise => { + setLoading(true); + try { + const updatedUser = await updateProfile(updates); + + notifications.show({ + title: 'Success', + message: 'Profile updated successfully', + color: 'green', + }); + + return updatedUser; + } catch (error) { + let errorMessage = 'Failed to update profile'; + + if (error instanceof Error) { + if (error.message.includes('password')) { + errorMessage = 'Current password is incorrect'; + } else if (error.message.includes('email')) { + errorMessage = 'Email is already in use'; + } + } + + notifications.show({ + title: 'Error', + message: errorMessage, + color: 'red', + }); + + return null; + } finally { + setLoading(false); + } + }, + [] + ); + + const handleAccountDeletion = useCallback( + async (password: string): Promise => { + setLoading(true); + try { + await deleteUser(password); + + notifications.show({ + title: 'Success', + message: 'Account deleted successfully', + color: 'green', + }); + + return true; + } catch (error) { + notifications.show({ + title: 'Error', + message: + error instanceof Error ? error.message : 'Failed to delete account', + color: 'red', + }); + + return false; + } finally { + setLoading(false); + } + }, + [] + ); + + return { + loading, + updateProfile: handleProfileUpdate, + deleteAccount: handleAccountDeletion, + }; +}