Introduced `useExactProducts` composable to fetch precise product details for guest cart and wishlist items. Improved cookie-based cart and wishlist fallback handling for unauthenticated users. Updated related components and composables for better synchronization and type safety. - Added `useExactProducts` composable leveraging the `GET_EXACT_PRODUCTS` query. - Enhanced `wishlist.vue` and `cart.vue` for reactive updates on guest state changes. - Improved product synchronization logic in `useOrderSync` and `useWishlistSync`. - Updated translations and fixed minor typos in localization files. Improves user experience by ensuring consistent product details, even for guests. No breaking changes.
353 lines
7.6 KiB
TypeScript
353 lines
7.6 KiB
TypeScript
import {
|
|
ADD_TO_CART,
|
|
BULK_CART,
|
|
REMOVE_ALL_FROM_CART,
|
|
REMOVE_FROM_CART,
|
|
REMOVE_KIND_FROM_CART,
|
|
} from '@graphql/mutations/cart';
|
|
import type {
|
|
IAddToOrderResponse,
|
|
IBulkOrderResponse,
|
|
IRemoveAllFromOrderResponse,
|
|
IRemoveFromOrderResponse,
|
|
IRemoveKindFromOrderResponse,
|
|
} from '@types';
|
|
|
|
interface IOverwriteOrderArguments {
|
|
type: string;
|
|
productUuid: string;
|
|
productName: string;
|
|
bulkAction?: string;
|
|
isBulkSync?: boolean;
|
|
products?: {
|
|
uuid: string;
|
|
}[];
|
|
}
|
|
|
|
export function useOrderOverwrite() {
|
|
const { t } = useI18n();
|
|
const cartStore = useCartStore();
|
|
const userStore = useUserStore();
|
|
const { $appHelpers, $notify } = useNuxtApp();
|
|
|
|
const isAuthenticated = computed(() => userStore.isAuthenticated);
|
|
const orderUuid = computed(() => cartStore.currentOrder?.uuid);
|
|
|
|
const cookieCart = useCookie($appHelpers.COOKIES_CART_KEY, {
|
|
default: () => [],
|
|
path: '/',
|
|
watch: true,
|
|
});
|
|
|
|
const {
|
|
mutate: addMutate,
|
|
loading: addLoading,
|
|
error: addError,
|
|
} = useMutation<IAddToOrderResponse>(ADD_TO_CART);
|
|
const {
|
|
mutate: removeMutate,
|
|
loading: removeLoading,
|
|
error: removedError,
|
|
} = useMutation<IRemoveFromOrderResponse>(REMOVE_FROM_CART);
|
|
const {
|
|
mutate: removeKindMutate,
|
|
loading: removeKindLoading,
|
|
error: removedKindError,
|
|
} = useMutation<IRemoveKindFromOrderResponse>(REMOVE_KIND_FROM_CART);
|
|
const {
|
|
mutate: removeAllMutate,
|
|
loading: removeAllLoading,
|
|
error: removedAllError,
|
|
} = useMutation<IRemoveAllFromOrderResponse>(REMOVE_ALL_FROM_CART);
|
|
const {
|
|
mutate: bulkMutate,
|
|
loading: bulkLoading,
|
|
error: bulkError,
|
|
} = useMutation<IBulkOrderResponse>(BULK_CART);
|
|
|
|
async function overwriteOrder(args: IOverwriteOrderArguments) {
|
|
if (isAuthenticated.value) {
|
|
switch (args.type) {
|
|
case 'add': {
|
|
const addResult = await addMutate({
|
|
orderUuid: orderUuid.value,
|
|
productUuid: args.productUuid,
|
|
});
|
|
|
|
if (addResult?.data?.addOrderProduct?.order) {
|
|
cartStore.setCurrentOrders(addResult.data.addOrderProduct.order);
|
|
|
|
$notify({
|
|
message: t('popup.success.addToCart', {
|
|
product: args.productName,
|
|
}),
|
|
type: 'success',
|
|
});
|
|
}
|
|
|
|
break;
|
|
}
|
|
|
|
case 'remove': {
|
|
const removeResult = await removeMutate({
|
|
orderUuid: orderUuid.value,
|
|
productUuid: args.productUuid,
|
|
});
|
|
|
|
if (removeResult?.data?.removeOrderProduct?.order) {
|
|
cartStore.setCurrentOrders(removeResult.data.removeOrderProduct.order);
|
|
|
|
$notify({
|
|
message: t('popup.success.removeFromCart', {
|
|
product: args.productName,
|
|
}),
|
|
type: 'success',
|
|
});
|
|
}
|
|
|
|
break;
|
|
}
|
|
|
|
case 'removeKind': {
|
|
const removeKindResult = await removeKindMutate({
|
|
orderUuid: orderUuid.value,
|
|
productUuid: args.productUuid,
|
|
});
|
|
|
|
if (removeKindResult?.data?.removeOrderProductsOfAKind?.order) {
|
|
cartStore.setCurrentOrders(removeKindResult.data.removeOrderProductsOfAKind.order);
|
|
|
|
$notify({
|
|
message: t('popup.success.removeFromCart', {
|
|
product: args.productName,
|
|
}),
|
|
type: 'success',
|
|
});
|
|
}
|
|
|
|
break;
|
|
}
|
|
|
|
case 'removeAll': {
|
|
const removeAllResult = await removeAllMutate({
|
|
orderUuid: orderUuid.value,
|
|
});
|
|
|
|
if (removeAllResult?.data?.removeAllOrderProducts?.order) {
|
|
cartStore.setCurrentOrders(removeAllResult.data.removeAllOrderProducts.order);
|
|
|
|
$notify({
|
|
message: t('popup.success.removeAllFromCart'),
|
|
type: 'success',
|
|
});
|
|
}
|
|
|
|
break;
|
|
}
|
|
|
|
case 'bulk': {
|
|
const bulkResult = await bulkMutate({
|
|
orderUuid: orderUuid.value,
|
|
action: args.bulkAction,
|
|
products: args.products,
|
|
});
|
|
|
|
if (bulkResult?.data?.bulkOrderAction?.order) {
|
|
cartStore.setCurrentOrders(bulkResult.data.bulkOrderAction.order);
|
|
$notify({
|
|
message: t('popup.success.bulkAddOrder'),
|
|
type: 'success',
|
|
});
|
|
}
|
|
|
|
break;
|
|
}
|
|
|
|
default:
|
|
console.error('No type provided for overwriteOrder');
|
|
}
|
|
} else {
|
|
switch (args.type) {
|
|
case 'add': {
|
|
const current = Array.isArray(cookieCart.value) ? [...cookieCart.value] : [];
|
|
|
|
const index = current.findIndex(
|
|
(item) => item.productUuid === args.productUuid
|
|
);
|
|
|
|
if (index !== -1) {
|
|
current[index] = {
|
|
...current[index],
|
|
quantity: current[index].quantity + 1,
|
|
};
|
|
} else {
|
|
current.push({
|
|
productUuid: args.productUuid,
|
|
quantity: 1,
|
|
});
|
|
}
|
|
|
|
cookieCart.value = current;
|
|
|
|
$notify({
|
|
message: t('popup.success.addToCart', {
|
|
product: args.productName,
|
|
}),
|
|
type: 'success',
|
|
});
|
|
|
|
break;
|
|
}
|
|
|
|
case 'remove': {
|
|
const current = Array.isArray(cookieCart.value)
|
|
? [...cookieCart.value]
|
|
: [];
|
|
|
|
const index = current.findIndex(
|
|
(item) => item.productUuid === args.productUuid
|
|
);
|
|
|
|
if (index !== -1) {
|
|
if (current[index].quantity > 1) {
|
|
current[index] = {
|
|
...current[index],
|
|
quantity: current[index].quantity - 1,
|
|
};
|
|
} else {
|
|
current.splice(index, 1);
|
|
}
|
|
|
|
cookieCart.value = current;
|
|
|
|
$notify({
|
|
message: t('popup.success.removeFromCart', {
|
|
product: args.productName,
|
|
}),
|
|
type: 'success',
|
|
});
|
|
}
|
|
|
|
break;
|
|
}
|
|
|
|
case 'removeKind': {
|
|
cookieCart.value = (cookieCart.value ?? []).filter(
|
|
(item) => item.productUuid !== args.productUuid
|
|
);
|
|
|
|
$notify({
|
|
message: t('popup.success.removeFromCart', {
|
|
product: args.productName,
|
|
}),
|
|
type: 'success',
|
|
});
|
|
|
|
break;
|
|
}
|
|
|
|
case 'removeAll': {
|
|
cookieCart.value = [];
|
|
|
|
$notify({
|
|
message: t('popup.success.removeAllFromCart'),
|
|
type: 'success',
|
|
});
|
|
|
|
break;
|
|
}
|
|
|
|
case 'bulk': {
|
|
const current = Array.isArray(cookieCart.value)
|
|
? [...cookieCart.value]
|
|
: [];
|
|
|
|
if (!args.products?.length) return;
|
|
|
|
switch (args.bulkAction) {
|
|
case 'add': {
|
|
args.products.forEach(({ uuid }) => {
|
|
const index = current.findIndex(
|
|
item => item.productUuid === uuid
|
|
);
|
|
|
|
if (index !== -1) {
|
|
current[index] = {
|
|
...current[index],
|
|
quantity: current[index].quantity + 1,
|
|
};
|
|
} else {
|
|
current.push({
|
|
productUuid: uuid,
|
|
quantity: 1,
|
|
});
|
|
}
|
|
});
|
|
|
|
cookieCart.value = current;
|
|
|
|
$notify({
|
|
message: t('popup.success.bulkAddOrder'),
|
|
type: 'success',
|
|
});
|
|
|
|
break;
|
|
}
|
|
|
|
case 'remove': {
|
|
const uuidsToRemove = args.products.map(p => p.uuid);
|
|
|
|
cookieCart.value = current.filter(
|
|
item => !uuidsToRemove.includes(item.productUuid)
|
|
);
|
|
|
|
$notify({
|
|
message: t('popup.success.bulkRemoveOrder'),
|
|
type: 'success',
|
|
});
|
|
|
|
break;
|
|
}
|
|
|
|
default:
|
|
console.error('Invalid bulkAction');
|
|
}
|
|
|
|
break;
|
|
}
|
|
|
|
default:
|
|
console.error('No type provided for overwriteOrder');
|
|
}
|
|
}
|
|
}
|
|
|
|
watch(
|
|
[addError, removedError, removedKindError, removedAllError, bulkError],
|
|
(errors) => {
|
|
const err = errors.find(Boolean);
|
|
if (!err) return;
|
|
console.error('useOrderOverwrite 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,
|
|
removeKindLoading,
|
|
removeAllLoading,
|
|
bulkLoading,
|
|
overwriteOrder,
|
|
};
|
|
}
|