2025-09-17 18:43:43 +09:00
|
|
|
"use client";
|
|
|
|
|
|
2025-09-19 16:34:10 +09:00
|
|
|
import { useCallback } from "react";
|
2025-09-17 18:43:43 +09:00
|
|
|
import { accountService } from "@/features/account/services/account.service";
|
|
|
|
|
import { useAuthStore } from "@/features/auth/services/auth.store";
|
2025-09-19 16:34:10 +09:00
|
|
|
import {
|
|
|
|
|
profileEditFormSchema,
|
|
|
|
|
profileFormToRequest,
|
|
|
|
|
type ProfileEditFormData
|
|
|
|
|
} from "@customer-portal/domain";
|
2025-09-20 11:35:40 +09:00
|
|
|
import { useZodForm } from "@/lib/validation";
|
2025-09-17 18:43:43 +09:00
|
|
|
|
|
|
|
|
export function useProfileEdit(initial: ProfileEditFormData) {
|
2025-09-19 16:34:10 +09:00
|
|
|
const handleSave = useCallback(async (formData: ProfileEditFormData) => {
|
2025-09-17 18:43:43 +09:00
|
|
|
try {
|
2025-09-19 16:34:10 +09:00
|
|
|
const requestData = profileFormToRequest(formData);
|
|
|
|
|
const updated = await accountService.updateProfile(requestData);
|
|
|
|
|
|
2025-09-17 18:43:43 +09:00
|
|
|
useAuthStore.setState(state => ({
|
|
|
|
|
...state,
|
|
|
|
|
user: state.user ? { ...state.user, ...updated } : state.user,
|
|
|
|
|
}));
|
2025-09-19 16:34:10 +09:00
|
|
|
|
|
|
|
|
return updated;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
throw error; // Let useZodForm handle the error state
|
2025-09-17 18:43:43 +09:00
|
|
|
}
|
2025-09-19 16:34:10 +09:00
|
|
|
}, []);
|
2025-09-17 18:43:43 +09:00
|
|
|
|
2025-09-19 16:34:10 +09:00
|
|
|
return useZodForm({
|
|
|
|
|
schema: profileEditFormSchema,
|
|
|
|
|
initialValues: initial,
|
|
|
|
|
onSubmit: handleSave,
|
|
|
|
|
});
|
2025-09-17 18:43:43 +09:00
|
|
|
}
|
2025-09-19 16:34:10 +09:00
|
|
|
|
|
|
|
|
// Re-export the type for backward compatibility
|
|
|
|
|
export type { ProfileEditFormData };
|