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.
68 lines
No EOL
1.7 KiB
TypeScript
68 lines
No EOL
1.7 KiB
TypeScript
import { useOrderOverwrite } from '@composables/orders/useOrderOverwrite';
|
|
|
|
export function useOrderSync() {
|
|
const cartStore = useCartStore();
|
|
const userStore = useUserStore();
|
|
const { $appHelpers } = useNuxtApp();
|
|
|
|
const { overwriteOrder } = useOrderOverwrite();
|
|
|
|
const isAuthenticated = computed(() => userStore.isAuthenticated);
|
|
const orderUuid = computed(() => cartStore.currentOrder?.uuid);
|
|
|
|
const cookieCart = useCookie($appHelpers.COOKIES_CART_KEY, {
|
|
default: () => [],
|
|
path: '/',
|
|
});
|
|
|
|
async function syncOrder() {
|
|
if (!isAuthenticated.value || !orderUuid.value) {
|
|
return;
|
|
}
|
|
|
|
const cookieCartItems = cookieCart.value || [];
|
|
|
|
if (cookieCartItems.length === 0) {
|
|
return;
|
|
}
|
|
|
|
const apiCartProducts = cartStore.currentOrder?.orderProducts?.edges || [];
|
|
|
|
const apiProductMap = new Map(
|
|
apiCartProducts.map(e => [e.node.product.uuid, e.node.quantity])
|
|
);
|
|
|
|
const productsToSync = [];
|
|
|
|
for (const cartItem of cookieCartItems) {
|
|
const apiQuantity = apiProductMap.get(cartItem.product.uuid) || 0;
|
|
const quantityDifference = cartItem.quantity - apiQuantity;
|
|
|
|
if (quantityDifference > 0) {
|
|
for (let i = 0; i < quantityDifference; i++) {
|
|
productsToSync.push({ uuid: cartItem.product.uuid });
|
|
}
|
|
}
|
|
}
|
|
|
|
if (productsToSync.length === 0) {
|
|
cookieCart.value = [];
|
|
return;
|
|
}
|
|
|
|
try {
|
|
await overwriteOrder({
|
|
type: 'bulk',
|
|
bulkAction: 'add',
|
|
isBulkSync: true,
|
|
products: productsToSync
|
|
});
|
|
} catch (err) {
|
|
console.error('Failed to sync cart:', err);
|
|
}
|
|
}
|
|
|
|
return {
|
|
syncOrder,
|
|
};
|
|
} |