113 lines
3.4 KiB
TypeScript
113 lines
3.4 KiB
TypeScript
import { useCardPageContext } from "../../../contexts/CardPageContext.tsx";
|
||
import {
|
||
CreateBoxInCardSchema,
|
||
CreateBoxInPalletSchema, PalletSchema,
|
||
ShippingService,
|
||
} from "../../../../../client";
|
||
import { notifications } from "../../../../../shared/lib/notifications.ts";
|
||
import { modals } from "@mantine/modals";
|
||
import { Text } from "@mantine/core";
|
||
import useUpdateCard from "./useUpdateCard.tsx";
|
||
import { useState } from "react";
|
||
import { ShippingProductParentData } from "../types/ShippingProductData.tsx";
|
||
|
||
const useShipping = () => {
|
||
const { selectedCard: card } = useCardPageContext();
|
||
const { update } = useUpdateCard();
|
||
const [palletIds, setPalletIds] = useState<number[]>([]);
|
||
|
||
const onCreatePalletClick = () => {
|
||
if (!card) return;
|
||
|
||
ShippingService.createPallet({
|
||
cardId: card.id,
|
||
})
|
||
.then(({ ok, message }) => {
|
||
notifications.guess(ok, { message });
|
||
update();
|
||
})
|
||
.catch(err => console.log(err));
|
||
};
|
||
|
||
const onDeletePallet = (palletId: number) => {
|
||
ShippingService.deletePallet({
|
||
palletId: palletId,
|
||
})
|
||
.then(({ ok, message }) => {
|
||
if (!ok) notifications.guess(ok, { message });
|
||
update();
|
||
})
|
||
.catch(err => console.log(err));
|
||
};
|
||
|
||
const onDeletePalletClick = (pallet: PalletSchema) => {
|
||
if (!card) return;
|
||
if (pallet.shippingProducts.length === 0 && pallet.boxes.length === 0) {
|
||
onDeletePallet(pallet.id);
|
||
return;
|
||
}
|
||
|
||
modals.openConfirmModal({
|
||
title: "Удаление паллета",
|
||
children: <Text size="sm">Вы уверены что хотите удалить паллет?</Text>,
|
||
labels: { confirm: "Да", cancel: "Нет" },
|
||
confirmProps: { color: "red" },
|
||
onConfirm: () => onDeletePallet(pallet.id),
|
||
});
|
||
};
|
||
|
||
const onCreateBox = (data: CreateBoxInPalletSchema | CreateBoxInCardSchema) => {
|
||
ShippingService.createBox({
|
||
requestBody: {
|
||
data,
|
||
},
|
||
})
|
||
.then(({ ok, message }) => {
|
||
notifications.guess(ok, { message });
|
||
update();
|
||
})
|
||
.catch(err => console.log(err));
|
||
};
|
||
|
||
const onCreateBoxInCardClick = () => {
|
||
onCreateBox({ cardId: card?.id ?? -1 });
|
||
};
|
||
|
||
const onCreateBoxInPallet = (palletId: number) => {
|
||
onCreateBox({ palletId });
|
||
};
|
||
|
||
const onCreateShippingProduct = ({ palletId, boxId }: ShippingProductParentData) => {
|
||
if (!card) return;
|
||
const postfix = palletId ? "на паллет" : "в короб";
|
||
|
||
modals.openContextModal({
|
||
modal: "shippingProductModal",
|
||
title: "Добавление товара " + postfix,
|
||
withCloseButton: false,
|
||
innerProps: {
|
||
card,
|
||
updateOnSubmit: update,
|
||
shippingData: {
|
||
palletId,
|
||
boxId,
|
||
productId: null,
|
||
quantity: null,
|
||
},
|
||
},
|
||
});
|
||
};
|
||
|
||
return {
|
||
onCreateBoxInCardClick,
|
||
onCreateBoxInPallet,
|
||
onCreateShippingProduct,
|
||
onCreatePalletClick,
|
||
onDeletePalletClick,
|
||
palletIds,
|
||
setPalletIds,
|
||
};
|
||
};
|
||
|
||
export default useShipping;
|