Refactored i18n configuration, replacing `DEFAULT_LOCALE` with `DEFAULT_LOCALE_FALLBACK` and enhancing environment-based locale validation. Improved cookie persistence for cart and wishlist, ensuring fallback handling for unauthenticated users. Enhancements: - Added `createProjectKey` utility for consistent project key generation. - Reworked cart and wishlist composables (`useOrderOverwrite`, `useWishlistOverwrite`) to decouple product identifier and handle cookies robustly. - Centralized `DEFAULT_LOCALE` logic for better maintainability. - Refined `useOrderSync` and `useWishlistSync` for clean synchronization across auth states. - Updated SCSS in hero and header styles for alignment corrections. Breaking Changes: `DEFAULT_LOCALE` constant removed; replaced with runtime config and fallback logic. Consumers must adapt to `DEFAULT_LOCALE_FALLBACK` and `$appHelpers.DEFAULT_LOCALE`.
66 lines
1.5 KiB
TypeScript
66 lines
1.5 KiB
TypeScript
import { useWishlistOverwrite } from '@composables/wishlist/useWishlistOverwrite';
|
|
|
|
export function useWishlistSync() {
|
|
const wishlistStore = useWishlistStore();
|
|
const userStore = useUserStore();
|
|
const { $appHelpers } = useNuxtApp();
|
|
|
|
const { overwriteWishlist } = useWishlistOverwrite();
|
|
|
|
const isAuthenticated = computed(() => userStore.isAuthenticated);
|
|
const wishlistUuid = computed(() => wishlistStore.wishlist?.uuid);
|
|
|
|
const cookieWishlist = useCookie($appHelpers.COOKIES_WISHLIST_KEY, {
|
|
default: () => [],
|
|
path: '/',
|
|
});
|
|
|
|
async function syncWishlist() {
|
|
if (!isAuthenticated.value || !wishlistUuid.value) {
|
|
return;
|
|
}
|
|
|
|
const cookieProducts = cookieWishlist.value || [];
|
|
|
|
if (cookieProducts.length === 0) {
|
|
return;
|
|
}
|
|
|
|
const apiProductUuids = wishlistStore.wishlist?.products?.edges.map((e) => e.node.uuid) || [];
|
|
|
|
const productsToAdd = cookieProducts.filter((product) => !apiProductUuids.includes(product));
|
|
|
|
if (productsToAdd.length === 0) {
|
|
cookieWishlist.value = [];
|
|
return;
|
|
}
|
|
|
|
try {
|
|
await overwriteWishlist({
|
|
type: 'bulk',
|
|
bulkAction: 'add',
|
|
isBulkSync: true,
|
|
products: productsToAdd.map((p) => ({
|
|
uuid: p,
|
|
})),
|
|
});
|
|
|
|
if (bulkResult?.data?.bulkWishlistAction?.wishlist) {
|
|
wishlistStore.setWishlist(bulkResult.data.bulkWishlistAction.wishlist);
|
|
|
|
cookieWishlist.value = [];
|
|
}
|
|
} catch (err) {
|
|
console.error('Failed to sync wishlist:', err);
|
|
}
|
|
}
|
|
|
|
watch(syncError, (err) => {
|
|
if (!err) return;
|
|
console.error('useWishlistSync error:', err);
|
|
});
|
|
|
|
return {
|
|
syncWishlist,
|
|
};
|
|
}
|