schon/storefront/composables/wishlist/useWishlistOverwrite.ts
Alexandr SaVBaD Waltz 761fecf67f Features: 1) Add useWishlistOverwrite composable for wishlist mutations, including adding, removing, and bulk actions; 2) Introduce new localized UI texts for cart and wishlist operations; 3) Enhance filtering logic with parseAttributesString and route query synchronization;
Fixes: 1) Replace `ElNotification` calls with `useNotification` utility across all authentication and user-related composables; 2) Add missing semicolons in multiple index exports and styled components; 3) Resolve issues with reactivity in `useStore` composable by renaming and restructuring product variables;

Extra: 1) Refactor localized strings and translations for better readability and maintenance; 2) Tweak styles including scoped styles, z-index adjustments, and SCSS mixins; 3) Remove unused components and imports to streamline storefront layout.
2025-07-06 19:49:26 +03:00

152 lines
No EOL
4 KiB
TypeScript

import type {
IAddToWishlistResponse, IBulkWishlistResponse,
IRemoveAllFromWishlistResponse,
IRemoveFromWishlistResponse
} from "~/types";
import {
ADD_TO_WISHLIST,
BULK_WISHLIST,
REMOVE_ALL_FROM_WISHLIST,
REMOVE_FROM_WISHLIST
} from "~/graphql/mutations/wishlist";
import {isGraphQLError} from "~/utils/error";
import {useNotification} from "~/composables/notification";
interface IOverwriteWishlistArguments {
type: string,
productUuid?: string,
productName?: string,
bulkAction?: string,
products?: {
uuid: string
}[]
}
export function useWishlistOverwrite() {
const {t} = useI18n();
const wishlistStore = useWishlistStore();
const wishlistUuid = computed(() => wishlistStore.wishlist?.uuid);
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
) {
switch (args.type) {
case "add":
const addResult = await addMutate({
wishlistUuid: wishlistUuid.value,
productUuid: args.productUuid
});
if (addResult?.data?.addWishlistProduct?.wishlist) {
wishlistStore.setWishlist(addResult.data.addWishlistProduct.wishlist);
useNotification(
t('popup.success.addToWishlist', { product: args.productName }),
'success'
);
}
break;
case "remove":
const removeResult = await removeMutate({
wishlistUuid: wishlistUuid.value,
productUuid: args.productUuid
});
if (removeResult?.data?.removeWishlistProduct?.wishlist) {
wishlistStore.setWishlist(removeResult.data.removeWishlistProduct.wishlist);
useNotification(
t('popup.success.removeFromWishlist', { product: args.productName }),
'success'
);
}
break;
case "removeAll":
const removeAllResult = await removeAllMutate({
wishlistUuid: wishlistUuid.value,
productUuid: args.productUuid
});
if (removeAllResult?.data?.removeAllWishlistProducts?.wishlist) {
wishlistStore.setWishlist(removeAllResult.data.removeAllWishlistProducts.wishlist);
useNotification(
t('popup.success.removeAllFromWishlist'),
'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);
useNotification(
t('popup.success.bulkRemoveWishlist'),
'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,
'error',
t('popup.errors.main')
);
});
return{
addLoading,
removeLoading,
removeAllLoading,
bulkLoading,
overwriteWishlist
};
}