38 lines
1.1 KiB
TypeScript
Raw Normal View History

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