Refactored multiple files for code styling consistency, using proper indentation and spacing to align with team standards. Improved readability and maintainability across composables, Apollo plugin, and localization files. Enhancements: - Standardized import and function indentation across all composables. - Updated `biome.json` schema to the latest version (v2.4.4) for tool compatibility. - Organized code blocks in Apollo plugin for better understandability. No functional changes introduced—this is a non-breaking, code refinement commit.
66 lines
1.6 KiB
TypeScript
66 lines
1.6 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.uuid));
|
|
|
|
if (productsToAdd.length === 0) {
|
|
cookieWishlist.value = [];
|
|
return;
|
|
}
|
|
|
|
try {
|
|
await overwriteWishlist({
|
|
type: 'bulk',
|
|
bulkAction: 'add',
|
|
isBulkSync: true,
|
|
products: productsToAdd.map((p) => ({
|
|
uuid: p.uuid,
|
|
})),
|
|
});
|
|
|
|
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,
|
|
};
|
|
}
|