Enhancements: - Introduced `wishlist.vue` for displaying and managing the wishlist. - Added guest cart and wishlist handling via cookies for unauthenticated users. - Implemented synchronization logic for wishlist and cart (`useOrderSync` and `useWishlistSync`) upon user login. - Updated `cart.vue` layout with a bulk 'add all to cart' button for wishlist items. - Enhanced `post.vue` prop handling for improved type safety. Fixes: - Fixed breadcrumbs console log removal in `useBreadcrumbs.ts`. - Corrected and unified translations in `en-gb.json` for cart and wishlist descriptions. - Fixed stale routes in footer (`terms-and-condition` -> `terms-and-conditions`, etc.). Extras: - Refactored composables `useWishlistOverwrite` and `useOrderOverwrite` for cookie-based fallback. - Applied code styling improvements, organized imports, and optimized API requests in Apollo plugin.
66 lines
No EOL
1.6 KiB
TypeScript
66 lines
No EOL
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,
|
|
};
|
|
} |