2025-09-17 18:43:43 +09:00
|
|
|
"use client";
|
|
|
|
|
|
2025-09-19 16:34:10 +09:00
|
|
|
import { useCallback } from "react";
|
2025-12-15 10:32:07 +09:00
|
|
|
import { useQueryClient } from "@tanstack/react-query";
|
|
|
|
|
import { queryKeys } from "@/lib/api";
|
2025-09-17 18:43:43 +09:00
|
|
|
import { accountService } from "@/features/account/services/account.service";
|
2025-09-25 17:42:36 +09:00
|
|
|
import {
|
|
|
|
|
addressFormSchema,
|
2025-09-19 16:34:10 +09:00
|
|
|
addressFormToRequest,
|
2025-09-25 17:42:36 +09:00
|
|
|
type AddressFormData,
|
2025-10-07 17:38:39 +09:00
|
|
|
} from "@customer-portal/domain/customer";
|
2025-12-02 11:06:54 +09:00
|
|
|
import { useZodForm } from "@/hooks/useZodForm";
|
2025-09-17 18:43:43 +09:00
|
|
|
|
|
|
|
|
export function useAddressEdit(initial: AddressFormData) {
|
2025-12-15 10:32:07 +09:00
|
|
|
const queryClient = useQueryClient();
|
|
|
|
|
|
|
|
|
|
const handleSave = useCallback(
|
|
|
|
|
async (formData: AddressFormData) => {
|
|
|
|
|
const requestData = addressFormToRequest(formData);
|
|
|
|
|
await accountService.updateAddress(requestData);
|
|
|
|
|
// Address changes can affect server-personalized catalog results (eligibility).
|
2025-12-25 13:20:45 +09:00
|
|
|
await queryClient.invalidateQueries({ queryKey: queryKeys.services.all() });
|
2025-12-15 10:32:07 +09:00
|
|
|
},
|
|
|
|
|
[queryClient]
|
|
|
|
|
);
|
2025-09-17 18:43:43 +09:00
|
|
|
|
2025-09-19 16:34:10 +09:00
|
|
|
return useZodForm({
|
|
|
|
|
schema: addressFormSchema,
|
|
|
|
|
initialValues: initial,
|
|
|
|
|
onSubmit: handleSave,
|
|
|
|
|
});
|
2025-09-17 18:43:43 +09:00
|
|
|
}
|