2025-09-17 18:43:43 +09:00
|
|
|
"use client";
|
|
|
|
|
|
|
|
|
|
import { useEffect, useState, 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 { useAuthStore } from "@/features/auth/services/auth.store";
|
|
|
|
|
import { accountService } from "@/features/account/services/account.service";
|
2025-11-05 15:47:06 +09:00
|
|
|
import { logger } from "@/lib/logger";
|
2025-09-17 18:43:43 +09:00
|
|
|
|
|
|
|
|
// Use centralized profile types
|
2025-10-09 10:49:03 +09:00
|
|
|
import type { ProfileEditFormData, Address } from "@customer-portal/domain/customer";
|
2025-09-17 18:43:43 +09:00
|
|
|
|
|
|
|
|
export function useProfileData() {
|
|
|
|
|
const { user } = useAuthStore();
|
2025-12-15 10:32:07 +09:00
|
|
|
const queryClient = useQueryClient();
|
2025-09-17 18:43:43 +09:00
|
|
|
const [loading, setLoading] = useState(true);
|
|
|
|
|
const [error, setError] = useState<string | null>(null);
|
|
|
|
|
const [isSavingProfile, setIsSavingProfile] = useState(false);
|
|
|
|
|
const [isSavingAddress, setIsSavingAddress] = useState(false);
|
|
|
|
|
const [billingInfo, setBillingInfo] = useState<{ address: Address } | null>(null);
|
|
|
|
|
|
2025-09-19 16:34:10 +09:00
|
|
|
const [formData, setFormData] = useState<ProfileEditFormData>({
|
2025-12-15 17:29:48 +09:00
|
|
|
email: user?.email || "",
|
2025-10-09 10:49:03 +09:00
|
|
|
phonenumber: user?.phonenumber || "",
|
2025-09-17 18:43:43 +09:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const [addressData, setAddress] = useState<Address>({
|
2025-10-07 17:38:39 +09:00
|
|
|
address1: "",
|
|
|
|
|
address2: "",
|
2025-09-17 18:43:43 +09:00
|
|
|
city: "",
|
|
|
|
|
state: "",
|
2025-10-07 17:38:39 +09:00
|
|
|
postcode: "",
|
2025-09-17 18:43:43 +09:00
|
|
|
country: "",
|
2025-10-07 17:38:39 +09:00
|
|
|
countryCode: "",
|
|
|
|
|
phoneNumber: "",
|
|
|
|
|
phoneCountryCode: "",
|
2025-09-17 18:43:43 +09:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const fetchBillingInfo = useCallback(async () => {
|
|
|
|
|
try {
|
|
|
|
|
setLoading(true);
|
|
|
|
|
const address = await accountService.getAddress().catch(() => null);
|
2025-10-21 13:44:14 +09:00
|
|
|
if (address) {
|
|
|
|
|
const normalizedAddress: Address = {
|
2025-10-07 17:38:39 +09:00
|
|
|
address1: address.address1 || "",
|
|
|
|
|
address2: address.address2 || "",
|
2025-09-17 18:43:43 +09:00
|
|
|
city: address.city || "",
|
|
|
|
|
state: address.state || "",
|
2025-10-07 17:38:39 +09:00
|
|
|
postcode: address.postcode || "",
|
2025-10-29 18:36:25 +09:00
|
|
|
country: address.country || "",
|
|
|
|
|
countryCode: address.countryCode || "",
|
2025-10-07 17:38:39 +09:00
|
|
|
phoneNumber: address.phoneNumber || "",
|
|
|
|
|
phoneCountryCode: address.phoneCountryCode || "",
|
2025-10-21 13:44:14 +09:00
|
|
|
};
|
|
|
|
|
setBillingInfo({ address: normalizedAddress });
|
|
|
|
|
setAddress(normalizedAddress);
|
|
|
|
|
}
|
2025-09-17 18:43:43 +09:00
|
|
|
} catch (err) {
|
|
|
|
|
setError(err instanceof Error ? err.message : "Failed to load address information");
|
|
|
|
|
} finally {
|
|
|
|
|
setLoading(false);
|
|
|
|
|
}
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
void fetchBillingInfo();
|
|
|
|
|
}, [fetchBillingInfo]);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (user) {
|
|
|
|
|
setFormData({
|
2025-12-15 17:29:48 +09:00
|
|
|
email: user.email || "",
|
2025-10-09 10:49:03 +09:00
|
|
|
phonenumber: user.phonenumber || "",
|
2025-09-17 18:43:43 +09:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}, [user]);
|
|
|
|
|
|
2025-09-19 16:34:10 +09:00
|
|
|
const saveProfile = async (next: ProfileEditFormData) => {
|
2025-09-17 18:43:43 +09:00
|
|
|
setIsSavingProfile(true);
|
|
|
|
|
try {
|
2025-09-18 16:00:20 +09:00
|
|
|
const updatedUser = await accountService.updateProfile({
|
2025-12-15 17:29:48 +09:00
|
|
|
email: next.email,
|
2025-10-09 10:49:03 +09:00
|
|
|
phonenumber: next.phonenumber,
|
2025-09-17 18:43:43 +09:00
|
|
|
});
|
|
|
|
|
useAuthStore.setState(state => ({
|
|
|
|
|
...state,
|
|
|
|
|
user: state.user ? { ...state.user, ...updatedUser } : state.user,
|
|
|
|
|
}));
|
|
|
|
|
setFormData(next);
|
|
|
|
|
return true;
|
|
|
|
|
} catch (err) {
|
2025-11-17 10:31:33 +09:00
|
|
|
logger.error("Error updating profile", err);
|
2025-09-17 18:43:43 +09:00
|
|
|
setError(err instanceof Error ? err.message : "Failed to update profile");
|
|
|
|
|
return false;
|
|
|
|
|
} finally {
|
|
|
|
|
setIsSavingProfile(false);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const saveAddress = async (next: Address) => {
|
|
|
|
|
setIsSavingAddress(true);
|
|
|
|
|
setError(null);
|
|
|
|
|
try {
|
|
|
|
|
await accountService.updateAddress({
|
2025-10-07 17:38:39 +09:00
|
|
|
address1: next.address1,
|
|
|
|
|
address2: next.address2,
|
2025-09-17 18:43:43 +09:00
|
|
|
city: next.city,
|
|
|
|
|
state: next.state,
|
2025-10-07 17:38:39 +09:00
|
|
|
postcode: next.postcode,
|
2025-09-17 18:43:43 +09:00
|
|
|
country: next.country,
|
2025-10-07 17:38:39 +09:00
|
|
|
countryCode: next.countryCode,
|
|
|
|
|
phoneNumber: next.phoneNumber,
|
|
|
|
|
phoneCountryCode: next.phoneCountryCode,
|
2025-09-17 18:43:43 +09:00
|
|
|
});
|
2025-12-15 10:32:07 +09:00
|
|
|
// Address changes can affect server-personalized catalog results (eligibility).
|
|
|
|
|
await queryClient.invalidateQueries({ queryKey: queryKeys.catalog.all() });
|
2025-09-17 18:43:43 +09:00
|
|
|
setBillingInfo({ address: next });
|
|
|
|
|
setAddress(next);
|
|
|
|
|
return true;
|
|
|
|
|
} catch (err) {
|
2025-11-17 10:31:33 +09:00
|
|
|
logger.error("Error updating address", err);
|
2025-09-17 18:43:43 +09:00
|
|
|
setError(err instanceof Error ? err.message : "Failed to update address");
|
|
|
|
|
return false;
|
|
|
|
|
} finally {
|
|
|
|
|
setIsSavingAddress(false);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
loading,
|
|
|
|
|
error,
|
|
|
|
|
billingInfo,
|
|
|
|
|
formData,
|
|
|
|
|
setFormData,
|
|
|
|
|
addressData,
|
|
|
|
|
setAddressData: setAddress,
|
|
|
|
|
saveProfile,
|
|
|
|
|
saveAddress,
|
|
|
|
|
isSavingProfile,
|
|
|
|
|
isSavingAddress,
|
|
|
|
|
} as const;
|
|
|
|
|
}
|