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.
253 lines
5.8 KiB
TypeScript
253 lines
5.8 KiB
TypeScript
import { useNotification } from '@composables/notification';
|
|
import {
|
|
ADD_TO_WISHLIST,
|
|
BULK_WISHLIST,
|
|
REMOVE_ALL_FROM_WISHLIST,
|
|
REMOVE_FROM_WISHLIST,
|
|
} from '@graphql/mutations/wishlist';
|
|
import type {
|
|
IAddToWishlistResponse,
|
|
IBulkWishlistResponse, IProduct,
|
|
IRemoveAllFromWishlistResponse,
|
|
IRemoveFromWishlistResponse,
|
|
} from '@types';
|
|
|
|
interface IOverwriteWishlistArguments {
|
|
type: string;
|
|
product: IProduct;
|
|
bulkAction?: string;
|
|
isBulkSync?: boolean;
|
|
products?: {
|
|
uuid: string;
|
|
}[];
|
|
}
|
|
|
|
export function useWishlistOverwrite() {
|
|
const { t } = useI18n();
|
|
const wishlistStore = useWishlistStore();
|
|
const userStore = useUserStore();
|
|
const { $appHelpers } = useNuxtApp();
|
|
|
|
const isAuthenticated = computed(() => userStore.isAuthenticated);
|
|
const wishlistUuid = computed(() => wishlistStore.wishlist?.uuid);
|
|
|
|
const cookieWishlist = useCookie($appHelpers.COOKIES_WISHLIST_KEY, {
|
|
default: () => [],
|
|
path: '/',
|
|
});
|
|
|
|
const {
|
|
mutate: addMutate,
|
|
loading: addLoading,
|
|
error: addError,
|
|
} = useMutation<IAddToWishlistResponse>(ADD_TO_WISHLIST);
|
|
const {
|
|
mutate: removeMutate,
|
|
loading: removeLoading,
|
|
error: removedError,
|
|
} = useMutation<IRemoveFromWishlistResponse>(REMOVE_FROM_WISHLIST);
|
|
const {
|
|
mutate: removeAllMutate,
|
|
loading: removeAllLoading,
|
|
error: removeAllError,
|
|
} = useMutation<IRemoveAllFromWishlistResponse>(REMOVE_ALL_FROM_WISHLIST);
|
|
const {
|
|
mutate: bulkMutate,
|
|
loading: bulkLoading,
|
|
error: bulkError,
|
|
} = useMutation<IBulkWishlistResponse>(BULK_WISHLIST);
|
|
|
|
async function overwriteWishlist(args: IOverwriteWishlistArguments) {
|
|
if (isAuthenticated.value) {
|
|
switch (args.type) {
|
|
case 'add': {
|
|
const addResult = await addMutate({
|
|
wishlistUuid: wishlistUuid.value,
|
|
productUuid: args.product.uuid,
|
|
});
|
|
|
|
if (addResult?.data?.addWishlistProduct?.wishlist) {
|
|
wishlistStore.setWishlist(addResult.data.addWishlistProduct.wishlist);
|
|
|
|
useNotification({
|
|
message: t('popup.success.addToWishlist', {
|
|
product: args.product.name,
|
|
}),
|
|
type: 'success',
|
|
});
|
|
}
|
|
|
|
break;
|
|
}
|
|
|
|
case 'remove': {
|
|
const removeResult = await removeMutate({
|
|
wishlistUuid: wishlistUuid.value,
|
|
productUuid: args.product.uuid,
|
|
});
|
|
|
|
if (removeResult?.data?.removeWishlistProduct?.wishlist) {
|
|
wishlistStore.setWishlist(removeResult.data.removeWishlistProduct.wishlist);
|
|
|
|
useNotification({
|
|
message: t('popup.success.removeFromWishlist', {
|
|
product: args.product.name,
|
|
}),
|
|
type: 'success',
|
|
});
|
|
}
|
|
|
|
break;
|
|
}
|
|
|
|
case 'removeAll': {
|
|
const removeAllResult = await removeAllMutate({
|
|
wishlistUuid: wishlistUuid.value,
|
|
productUuid: args.product.uuid,
|
|
});
|
|
|
|
if (removeAllResult?.data?.removeAllWishlistProducts?.wishlist) {
|
|
wishlistStore.setWishlist(removeAllResult.data.removeAllWishlistProducts.wishlist);
|
|
|
|
useNotification({
|
|
message: t('popup.success.removeAllFromWishlist'),
|
|
type: 'success',
|
|
});
|
|
}
|
|
|
|
break;
|
|
}
|
|
|
|
case 'bulk': {
|
|
const bulkResult = await bulkMutate({
|
|
wishlistUuid: wishlistUuid.value,
|
|
action: args.bulkAction,
|
|
products: args.products,
|
|
});
|
|
|
|
if (bulkResult?.data?.bulkWishlistAction?.wishlist) {
|
|
wishlistStore.setWishlist(bulkResult.data.bulkWishlistAction.wishlist);
|
|
|
|
if (args.isBulkSync) {
|
|
cookieWishlist.value = [];
|
|
} else {
|
|
useNotification({
|
|
message: t('popup.success.bulkRemoveWishlist'),
|
|
type: 'success',
|
|
});
|
|
}
|
|
}
|
|
|
|
break;
|
|
}
|
|
|
|
default:
|
|
console.error('No type provided for overwriteWishlist');
|
|
}
|
|
} else {
|
|
switch (args.type) {
|
|
case 'add': {
|
|
const isAlreadyInWishlist = cookieWishlist.value.some(
|
|
(item) => item.uuid === args.product.uuid
|
|
);
|
|
|
|
if (isAlreadyInWishlist) {
|
|
useNotification({
|
|
message: t('popup.errors.alreadyInWishlist', {
|
|
product: args.product.name,
|
|
}),
|
|
type: 'warning',
|
|
});
|
|
} else {
|
|
cookieWishlist.value = [...cookieWishlist.value, args.product];
|
|
|
|
useNotification({
|
|
message: t('popup.success.addToWishlist', {
|
|
product: args.product.name,
|
|
}),
|
|
type: 'success',
|
|
});
|
|
}
|
|
|
|
break;
|
|
}
|
|
|
|
case 'remove': {
|
|
cookieWishlist.value = cookieWishlist.value.filter(
|
|
(item) => item.uuid !== args.product.uuid
|
|
);
|
|
|
|
useNotification({
|
|
message: t('popup.success.removeFromWishlist', {
|
|
product: args.product.name,
|
|
}),
|
|
type: 'success',
|
|
});
|
|
|
|
break;
|
|
}
|
|
|
|
case 'removeAll': {
|
|
cookieWishlist.value = [];
|
|
|
|
useNotification({
|
|
message: t('popup.success.removeAllFromWishlist'),
|
|
type: 'success',
|
|
});
|
|
|
|
break;
|
|
}
|
|
|
|
case 'bulk': {
|
|
const bulkResult = await bulkMutate({
|
|
wishlistUuid: wishlistUuid.value,
|
|
action: args.bulkAction,
|
|
products: args.products,
|
|
});
|
|
|
|
if (bulkResult?.data?.bulkWishlistAction?.wishlist) {
|
|
wishlistStore.setWishlist(bulkResult.data.bulkWishlistAction.wishlist);
|
|
|
|
if (args.isBulkSync) {
|
|
cookieWishlist.value = [];
|
|
} else {
|
|
useNotification({
|
|
message: t('popup.success.bulkRemoveWishlist'),
|
|
type: 'success',
|
|
});
|
|
}
|
|
}
|
|
|
|
break;
|
|
}
|
|
|
|
default:
|
|
console.error('No type provided for overwriteWishlist');
|
|
}
|
|
}
|
|
}
|
|
|
|
watch(addError || removedError || removeAllError || bulkError, (err) => {
|
|
if (!err) return;
|
|
console.error('useWishlistOverwrite error:', err);
|
|
let message = t('popup.errors.defaultError');
|
|
if (isGraphQLError(err)) {
|
|
message = err.graphQLErrors?.[0]?.message || message;
|
|
} else {
|
|
message = err.message;
|
|
}
|
|
useNotification({
|
|
message,
|
|
type: 'error',
|
|
title: t('popup.errors.main'),
|
|
});
|
|
});
|
|
|
|
return {
|
|
addLoading,
|
|
removeLoading,
|
|
removeAllLoading,
|
|
bulkLoading,
|
|
overwriteWishlist,
|
|
};
|
|
}
|