Refactored multiple files for code styling consistency, using proper indentation and spacing to align with team standards. Improved readability and maintainability across composables, Apollo plugin, and localization files. Enhancements: - Standardized import and function indentation across all composables. - Updated `biome.json` schema to the latest version (v2.4.4) for tool compatibility. - Organized code blocks in Apollo plugin for better understandability. No functional changes introduced—this is a non-breaking, code refinement commit.
306 lines
7.1 KiB
TypeScript
306 lines
7.1 KiB
TypeScript
import { useNotification } from '@composables/notification';
|
|
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,
|
|
IProduct,
|
|
IRemoveAllFromOrderResponse,
|
|
IRemoveFromOrderResponse,
|
|
IRemoveKindFromOrderResponse,
|
|
} from '@types';
|
|
|
|
interface IOverwriteOrderArguments {
|
|
type: string;
|
|
product: IProduct;
|
|
bulkAction?: string;
|
|
isBulkSync?: boolean;
|
|
products?: {
|
|
uuid: string;
|
|
}[];
|
|
}
|
|
|
|
export function useOrderOverwrite() {
|
|
const { t } = useI18n();
|
|
const cartStore = useCartStore();
|
|
const userStore = useUserStore();
|
|
|
|
const isAuthenticated = computed(() => userStore.isAuthenticated);
|
|
const orderUuid = computed(() => cartStore.currentOrder?.uuid);
|
|
|
|
const cookieCart = useCookie($appHelpers.COOKIES_CART_KEY, {
|
|
default: () => [],
|
|
path: '/',
|
|
});
|
|
|
|
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.product.uuid,
|
|
});
|
|
|
|
if (addResult?.data?.addOrderProduct?.order) {
|
|
cartStore.setCurrentOrders(addResult.data.addOrderProduct.order);
|
|
|
|
useNotification({
|
|
message: t('popup.success.addToCart', {
|
|
product: args.product.name,
|
|
}),
|
|
type: 'success',
|
|
});
|
|
}
|
|
|
|
break;
|
|
}
|
|
|
|
case 'remove': {
|
|
const removeResult = await removeMutate({
|
|
orderUuid: orderUuid.value,
|
|
productUuid: args.product.uuid,
|
|
});
|
|
|
|
if (removeResult?.data?.removeOrderProduct?.order) {
|
|
cartStore.setCurrentOrders(removeResult.data.removeOrderProduct.order);
|
|
|
|
useNotification({
|
|
message: t('popup.success.removeFromCart', {
|
|
product: args.product.name,
|
|
}),
|
|
type: 'success',
|
|
});
|
|
}
|
|
|
|
break;
|
|
}
|
|
|
|
case 'removeKind': {
|
|
const removeKindResult = await removeKindMutate({
|
|
orderUuid: orderUuid.value,
|
|
productUuid: args.product.uuid,
|
|
});
|
|
|
|
if (removeKindResult?.data?.removeOrderProductsOfAKind?.order) {
|
|
cartStore.setCurrentOrders(removeKindResult.data.removeOrderProductsOfAKind.order);
|
|
|
|
useNotification({
|
|
message: t('popup.success.removeFromCart', {
|
|
product: args.product.name,
|
|
}),
|
|
type: 'success',
|
|
});
|
|
}
|
|
|
|
break;
|
|
}
|
|
|
|
case 'removeAll': {
|
|
const removeAllResult = await removeAllMutate({
|
|
orderUuid: orderUuid.value,
|
|
});
|
|
|
|
if (removeAllResult?.data?.removeAllOrderProducts?.order) {
|
|
cartStore.setCurrentOrders(removeAllResult.data.removeAllOrderProducts.order);
|
|
|
|
useNotification({
|
|
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);
|
|
useNotification({
|
|
message: t('popup.success.bulkRemoveOrder'),
|
|
type: 'success',
|
|
});
|
|
}
|
|
|
|
break;
|
|
}
|
|
|
|
default:
|
|
console.error('No type provided for overwriteOrder');
|
|
}
|
|
} else {
|
|
switch (args.type) {
|
|
case 'add': {
|
|
const currentCart = cookieCart.value || [];
|
|
const existingItem = currentCart.find((item) => item.product.uuid === args.product.uuid);
|
|
|
|
if (existingItem) {
|
|
existingItem.quantity += 1;
|
|
cookieCart.value = [
|
|
...currentCart,
|
|
];
|
|
} else {
|
|
cookieCart.value = [
|
|
...currentCart,
|
|
{
|
|
product: args.product,
|
|
quantity: 1,
|
|
},
|
|
];
|
|
}
|
|
|
|
useNotification({
|
|
message: t('popup.success.addToCart', {
|
|
product: args.product.name,
|
|
}),
|
|
type: 'success',
|
|
});
|
|
|
|
break;
|
|
}
|
|
|
|
case 'remove': {
|
|
const currentCart = cookieCart.value || [];
|
|
const existingItem = currentCart.find((item) => item.product.uuid === args.product.uuid);
|
|
|
|
if (existingItem) {
|
|
if (existingItem.quantity > 1) {
|
|
existingItem.quantity -= 1;
|
|
cookieCart.value = [
|
|
...currentCart,
|
|
];
|
|
} else {
|
|
cookieCart.value = currentCart.filter((item) => item.product.uuid !== args.product.uuid);
|
|
}
|
|
|
|
useNotification({
|
|
message: t('popup.success.removeFromCart', {
|
|
product: args.product.name,
|
|
}),
|
|
type: 'success',
|
|
});
|
|
}
|
|
|
|
break;
|
|
}
|
|
|
|
case 'removeKind': {
|
|
cookieCart.value = cookieCart.value.filter((item) => item.product.uuid !== args.product.uuid);
|
|
|
|
useNotification({
|
|
message: t('popup.success.removeFromCart', {
|
|
product: args.product.name,
|
|
}),
|
|
type: 'success',
|
|
});
|
|
|
|
break;
|
|
}
|
|
|
|
case 'removeAll': {
|
|
cookieCart.value = [];
|
|
|
|
useNotification({
|
|
message: t('popup.success.removeAllFromCart'),
|
|
type: 'success',
|
|
});
|
|
|
|
break;
|
|
}
|
|
|
|
case 'bulk': {
|
|
if (args.bulkAction === 'remove' && args.products) {
|
|
const uuidsToRemove = args.products.map((p) => p.uuid);
|
|
cookieCart.value = cookieCart.value.filter((item) => !uuidsToRemove.includes(item.product.uuid));
|
|
|
|
useNotification({
|
|
message: t('popup.success.bulkRemoveOrder'),
|
|
type: 'success',
|
|
});
|
|
} else if (args.bulkAction === 'add' && args.products) {
|
|
const currentCart = cookieCart.value || [];
|
|
|
|
for (const productRef of args.products) {
|
|
const existingItem = currentCart.find((item) => item.product.uuid === productRef.uuid);
|
|
|
|
if (existingItem) {
|
|
existingItem.quantity += 1;
|
|
}
|
|
}
|
|
|
|
cookieCart.value = [
|
|
...currentCart,
|
|
];
|
|
}
|
|
|
|
break;
|
|
}
|
|
|
|
default:
|
|
console.error('No type provided for overwriteOrder');
|
|
}
|
|
}
|
|
}
|
|
|
|
watch(addError || removedError || removedKindError || removedAllError || bulkError, (err) => {
|
|
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;
|
|
}
|
|
useNotification({
|
|
message,
|
|
type: 'error',
|
|
title: t('popup.errors.main'),
|
|
});
|
|
});
|
|
|
|
return {
|
|
addLoading,
|
|
removeLoading,
|
|
removeKindLoading,
|
|
removeAllLoading,
|
|
bulkLoading,
|
|
overwriteOrder,
|
|
};
|
|
}
|