schon/storefront/app/composables/wishlist/useWishlistOverwrite.ts
Alexandr SaVBaD Waltz 8d7685ef67 feat(notification): integrate global notification plugin using ElNotification
Added a global `notify` method via Nuxt plugin to replace `useNotification`. Improved messaging structure by embedding progress bars and handled dynamic durations. Updated usage across composables and components for consistency.

- Replaced `useNotification` with `$notify` in all applicable files.
- Updated `app.config.ts` to support customizable notification positions.
- Refactored affected composables for simplified notification calls.
- Enhanced progress indicator display within notifications.

Breaking Changes:
`useNotification` is removed, requiring migration to the new `$notify` API.
2026-03-01 15:30:47 +03:00

262 lines
5.9 KiB
TypeScript

import {
ADD_TO_WISHLIST,
BULK_WISHLIST,
REMOVE_ALL_FROM_WISHLIST,
REMOVE_FROM_WISHLIST,
} from '@graphql/mutations/wishlist';
import type {
IAddToWishlistResponse,
IBulkWishlistResponse,
IRemoveAllFromWishlistResponse,
IRemoveFromWishlistResponse,
} from '@types';
interface IOverwriteWishlistArguments {
type: string;
productUuid: string;
productName: string;
bulkAction?: string;
isBulkSync?: boolean;
products?: {
uuid: string;
}[];
}
export function useWishlistOverwrite() {
const { t } = useI18n();
const wishlistStore = useWishlistStore();
const userStore = useUserStore();
const { $appHelpers, $notify } = useNuxtApp();
const isAuthenticated = computed(() => userStore.isAuthenticated);
const wishlistUuid = computed(() => wishlistStore.wishlist?.uuid);
const cookieWishlist = useCookie($appHelpers.COOKIES_WISHLIST_KEY, {
default: () => [],
path: '/',
watch: true,
});
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.productUuid,
});
if (addResult?.data?.addWishlistProduct?.wishlist) {
wishlistStore.setWishlist(addResult.data.addWishlistProduct.wishlist);
$notify({
message: t('popup.success.addToWishlist', {
product: args.productName,
}),
type: '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);
$notify({
message: t('popup.success.removeFromWishlist', {
product: args.productName,
}),
type: '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);
$notify({
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 {
$notify({
message: t('popup.success.bulkRemoveWishlist'),
type: 'success',
});
}
}
break;
}
default:
console.error('No type provided for overwriteWishlist');
}
} else {
switch (args.type) {
case 'add': {
const current = Array.isArray(cookieWishlist.value)
? [...cookieWishlist.value]
: [];
if (current.includes(args.productUuid)) {
$notify({
message: t('popup.errors.alreadyInWishlist', {
product: args.productName,
}),
type: 'warning',
});
return;
}
current.push(args.productUuid);
cookieWishlist.value = current;
$notify({
message: t('popup.success.addToWishlist', {
product: args.productName,
}),
type: 'success',
});
break;
}
case 'remove': {
cookieWishlist.value = Array.isArray(cookieWishlist.value)
? cookieWishlist.value.filter(
(uuid) => uuid !== args.productUuid
)
: [];
$notify({
message: t('popup.success.removeFromWishlist', {
product: args.productName,
}),
type: 'success',
});
break;
}
case 'removeAll': {
cookieWishlist.value = [];
$notify({
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 {
$notify({
message: t('popup.success.bulkRemoveWishlist'),
type: 'success',
});
}
}
break;
}
default:
console.error('No type provided for overwriteWishlist');
}
}
}
watch(
[addError, removedError, removeAllError, bulkError],
(errors) => {
const err = errors.find(Boolean);
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;
}
$notify({
message,
type: 'error',
title: t('popup.errors.main'),
});
}
);
return {
addLoading,
removeLoading,
removeAllLoading,
bulkLoading,
overwriteWishlist,
};
}