diff --git a/src/lib/client/@tanstack/react-query.gen.ts b/src/lib/client/@tanstack/react-query.gen.ts index 92a0b55..5e4eb20 100644 --- a/src/lib/client/@tanstack/react-query.gen.ts +++ b/src/lib/client/@tanstack/react-query.gen.ts @@ -61,6 +61,7 @@ import { getBaseMarketplaces, getBoards, getClients, + getDealBarcodesPdf, getDealModuleAttributes, getDealProducts, getDeals, @@ -240,6 +241,9 @@ import type { GetBaseMarketplacesData, GetBoardsData, GetClientsData, + GetDealBarcodesPdfData, + GetDealBarcodesPdfError, + GetDealBarcodesPdfResponse2, GetDealModuleAttributesData, GetDealProductsData, GetDealsData, @@ -2902,6 +2906,57 @@ export const getProductBarcodePdfMutation = ( return mutationOptions; }; +export const getDealBarcodesPdfQueryKey = ( + options: Options +) => createQueryKey("getDealBarcodesPdf", options); + +/** + * Get Deal Barcodes Pdf + */ +export const getDealBarcodesPdfOptions = ( + options: Options +) => { + return queryOptions({ + queryFn: async ({ queryKey, signal }) => { + const { data } = await getDealBarcodesPdf({ + ...options, + ...queryKey[0], + signal, + throwOnError: true, + }); + return data; + }, + queryKey: getDealBarcodesPdfQueryKey(options), + }); +}; + +/** + * Get Deal Barcodes Pdf + */ +export const getDealBarcodesPdfMutation = ( + options?: Partial> +): UseMutationOptions< + GetDealBarcodesPdfResponse2, + AxiosError, + Options +> => { + const mutationOptions: UseMutationOptions< + GetDealBarcodesPdfResponse2, + AxiosError, + Options + > = { + mutationFn: async localOptions => { + const { data } = await getDealBarcodesPdf({ + ...options, + ...localOptions, + throwOnError: true, + }); + return data; + }, + }; + return mutationOptions; +}; + export const uploadProductBarcodeImageQueryKey = ( options: Options ) => createQueryKey("uploadProductBarcodeImage", options); diff --git a/src/lib/client/sdk.gen.ts b/src/lib/client/sdk.gen.ts index e3a186e..c0b1ddc 100644 --- a/src/lib/client/sdk.gen.ts +++ b/src/lib/client/sdk.gen.ts @@ -154,6 +154,9 @@ import type { GetClientsData, GetClientsErrors, GetClientsResponses, + GetDealBarcodesPdfData, + GetDealBarcodesPdfErrors, + GetDealBarcodesPdfResponses, GetDealModuleAttributesData, GetDealModuleAttributesErrors, GetDealModuleAttributesResponses, @@ -380,6 +383,8 @@ import { zGetBoardsResponse2, zGetClientsData, zGetClientsResponse2, + zGetDealBarcodesPdfData, + zGetDealBarcodesPdfResponse2, zGetDealModuleAttributesData, zGetDealModuleAttributesResponse2, zGetDealProductsData, @@ -2271,6 +2276,33 @@ export const getProductBarcodePdf = ( }); }; +/** + * Get Deal Barcodes Pdf + */ +export const getDealBarcodesPdf = ( + options: Options +) => { + return (options.client ?? _heyApiClient).post< + GetDealBarcodesPdfResponses, + GetDealBarcodesPdfErrors, + ThrowOnError + >({ + requestValidator: async data => { + return await zGetDealBarcodesPdfData.parseAsync(data); + }, + responseType: "json", + responseValidator: async data => { + return await zGetDealBarcodesPdfResponse2.parseAsync(data); + }, + url: "/crm/v1/fulfillment-base/product/barcode/for-deal/get-pdf", + ...options, + headers: { + "Content-Type": "application/json", + ...options.headers, + }, + }); +}; + /** * Upload Product Barcode Image */ diff --git a/src/lib/client/types.gen.ts b/src/lib/client/types.gen.ts index 507b142..934ad81 100644 --- a/src/lib/client/types.gen.ts +++ b/src/lib/client/types.gen.ts @@ -1613,6 +1613,34 @@ export type GetClientsResponse = { items: Array; }; +/** + * GetDealBarcodesPdfRequest + */ +export type GetDealBarcodesPdfRequest = { + /** + * Dealid + */ + dealId: number; +}; + +/** + * GetDealBarcodesPdfResponse + */ +export type GetDealBarcodesPdfResponse = { + /** + * Base64String + */ + base64String: string; + /** + * Filename + */ + filename: string; + /** + * Mimetype + */ + mimeType: string; +}; + /** * GetDealModuleAttributesResponse */ @@ -5233,6 +5261,33 @@ export type GetProductBarcodePdfResponses = { export type GetProductBarcodePdfResponse2 = GetProductBarcodePdfResponses[keyof GetProductBarcodePdfResponses]; +export type GetDealBarcodesPdfData = { + body: GetDealBarcodesPdfRequest; + path?: never; + query?: never; + url: "/crm/v1/fulfillment-base/product/barcode/for-deal/get-pdf"; +}; + +export type GetDealBarcodesPdfErrors = { + /** + * Validation Error + */ + 422: HttpValidationError; +}; + +export type GetDealBarcodesPdfError = + GetDealBarcodesPdfErrors[keyof GetDealBarcodesPdfErrors]; + +export type GetDealBarcodesPdfResponses = { + /** + * Successful Response + */ + 200: GetDealBarcodesPdfResponse; +}; + +export type GetDealBarcodesPdfResponse2 = + GetDealBarcodesPdfResponses[keyof GetDealBarcodesPdfResponses]; + export type UploadProductBarcodeImageData = { body: BodyUploadProductBarcodeImage; path: { diff --git a/src/lib/client/zod.gen.ts b/src/lib/client/zod.gen.ts index 9b7b869..d8fddd7 100644 --- a/src/lib/client/zod.gen.ts +++ b/src/lib/client/zod.gen.ts @@ -1149,6 +1149,22 @@ export const zGetClientsResponse = z.object({ items: z.array(zClientSchema), }); +/** + * GetDealBarcodesPdfRequest + */ +export const zGetDealBarcodesPdfRequest = z.object({ + dealId: z.int(), +}); + +/** + * GetDealBarcodesPdfResponse + */ +export const zGetDealBarcodesPdfResponse = z.object({ + base64String: z.string(), + filename: z.string(), + mimeType: z.string(), +}); + /** * GetDealModuleAttributesResponse */ @@ -2743,6 +2759,17 @@ export const zGetProductBarcodePdfData = z.object({ */ export const zGetProductBarcodePdfResponse2 = zGetProductBarcodePdfResponse; +export const zGetDealBarcodesPdfData = z.object({ + body: zGetDealBarcodesPdfRequest, + path: z.optional(z.never()), + query: z.optional(z.never()), +}); + +/** + * Successful Response + */ +export const zGetDealBarcodesPdfResponse2 = zGetDealBarcodesPdfResponse; + export const zUploadProductBarcodeImageData = z.object({ body: zBodyUploadProductBarcodeImage, path: z.object({ diff --git a/src/modules/dealModularEditorTabs/FulfillmentBase/desktop/components/DealInfoView/components/ProductsActions/ProductsActions.tsx b/src/modules/dealModularEditorTabs/FulfillmentBase/desktop/components/DealInfoView/components/ProductsActions/ProductsActions.tsx index fc2914c..9a361ce 100644 --- a/src/modules/dealModularEditorTabs/FulfillmentBase/desktop/components/DealInfoView/components/ProductsActions/ProductsActions.tsx +++ b/src/modules/dealModularEditorTabs/FulfillmentBase/desktop/components/DealInfoView/components/ProductsActions/ProductsActions.tsx @@ -1,72 +1,30 @@ import { FC } from "react"; -import { Button, Flex } from "@mantine/core"; -import { modals } from "@mantine/modals"; -import { notifications } from "@/lib/notifications"; -import { useFulfillmentBaseContext } from "@/modules/dealModularEditorTabs/FulfillmentBase/shared/contexts/FulfillmentBaseContext"; +import { IconCubePlus, IconPencilPlus } from "@tabler/icons-react"; +import { Flex } from "@mantine/core"; +import InlineButton from "@/components/ui/InlineButton/InlineButton"; +import useDealProductActions from "@/modules/dealModularEditorTabs/FulfillmentBase/shared/hooks/utils/useDealProductActions"; +import useProductActions from "@/modules/dealModularEditorTabs/FulfillmentBase/shared/hooks/utils/useProductActions"; const ProductsActions: FC = () => { - const { deal, dealProductsList, productsCrud, dealProductsCrud } = - useFulfillmentBaseContext(); - - const onCreateProductClick = () => { - if (!deal.client) { - notifications.error({ message: "Выберите клиента для сделки" }); - return; - } - - modals.openContextModal({ - modal: "productEditorModal", - title: "Создание товара", - withCloseButton: false, - innerProps: { - onCreate: productsCrud.onCreate, - isEditing: false, - clientId: deal.client.id, - refetchProducts: dealProductsList.refetch, - }, - }); - }; - - const onCreateDealProductClick = () => { - if (!deal.client) { - notifications.error({ message: "Выберите клиента для сделки" }); - return; - } - - const productIdsToExclude = dealProductsList.dealProducts.map( - product => product.product.id - ); - - modals.openContextModal({ - modal: "dealProductEditorModal", - title: "Добавление товара", - withCloseButton: false, - innerProps: { - onCreate: values => - dealProductsCrud.onCreate({ ...values, dealId: deal.id }), - productIdsToExclude, - isEditing: false, - clientId: deal.client.id, - }, - }); - }; + const { onCreateClick: onCreateDealProductClick } = useDealProductActions(); + const { onCreateClick: onCreateProductClick } = useProductActions(); return ( - - + ); }; diff --git a/src/modules/dealModularEditorTabs/FulfillmentBase/desktop/components/DealServicesTable/components/ServicesActions.tsx b/src/modules/dealModularEditorTabs/FulfillmentBase/desktop/components/DealServicesTable/components/ServicesActions.tsx index 920f756..5df05a4 100644 --- a/src/modules/dealModularEditorTabs/FulfillmentBase/desktop/components/DealServicesTable/components/ServicesActions.tsx +++ b/src/modules/dealModularEditorTabs/FulfillmentBase/desktop/components/DealServicesTable/components/ServicesActions.tsx @@ -1,67 +1,41 @@ -import { Button, Flex } from "@mantine/core"; -import { modals } from "@mantine/modals"; -import { addKitToDeal, ServicesKitSchema } from "@/lib/client"; -import { useFulfillmentBaseContext } from "@/modules/dealModularEditorTabs/FulfillmentBase/shared/contexts/FulfillmentBaseContext"; -import { ServiceType } from "@/modules/dealModularEditorTabs/FulfillmentBase/shared/types/service"; +import { IconBarcode, IconTextPlus } from "@tabler/icons-react"; +import { Flex, Tooltip } from "@mantine/core"; +import InlineButton from "@/components/ui/InlineButton/InlineButton"; +import useBarcodePrintActions from "@/modules/dealModularEditorTabs/FulfillmentBase/shared/hooks/utils/useBarcodePrintActions"; +import useServiceActions from "@/modules/dealModularEditorTabs/FulfillmentBase/shared/hooks/utils/useServiceActions"; const ServicesActions = () => { - const { dealServicesList, dealServicesCrud, deal } = - useFulfillmentBaseContext(); - - const onCreateClick = () => { - const serviceIdsToExclude = dealServicesList.dealServices.map( - service => service.service.id - ); - modals.openContextModal({ - modal: "dealServiceEditorModal", - innerProps: { - onCreate: values => - dealServicesCrud.onCreate({ ...values, dealId: deal.id }), - serviceIdsToExclude, - isEditing: false, - }, - withCloseButton: false, - }); - }; - - const onServicesKitAdd = (servicesKit: ServicesKitSchema) => { - addKitToDeal({ - body: { - dealId: deal.id, - kitId: servicesKit.id, - }, - }) - .then(() => dealServicesList.refetch()) - .catch(err => console.error(err)); - }; - - const onAddKitClick = () => { - modals.openContextModal({ - modal: "servicesKitSelectModal", - innerProps: { - onSelect: onServicesKitAdd, - serviceType: ServiceType.DEAL_SERVICE, - }, - withCloseButton: false, - }); - }; + const { onCreateClick, onAddKitClick } = useServiceActions(); + const { onPrintDealBarcodesClick } = useBarcodePrintActions(); return ( - - + + + + Услуга + + + + + + Набор + + + + + + Печать + + ); }; diff --git a/src/modules/dealModularEditorTabs/FulfillmentBase/desktop/components/ProductView/components/ProductViewActions.tsx b/src/modules/dealModularEditorTabs/FulfillmentBase/desktop/components/ProductView/components/ProductViewActions.tsx index 93c1539..e280186 100644 --- a/src/modules/dealModularEditorTabs/FulfillmentBase/desktop/components/ProductView/components/ProductViewActions.tsx +++ b/src/modules/dealModularEditorTabs/FulfillmentBase/desktop/components/ProductView/components/ProductViewActions.tsx @@ -1,50 +1,20 @@ import { FC } from "react"; import { IconBarcode, IconEdit, IconTrash } from "@tabler/icons-react"; import { Flex } from "@mantine/core"; -import { modals } from "@mantine/modals"; import ActionIconWithTip from "@/components/ui/ActionIconWithTip/ActionIconWithTip"; import { DealProductSchema } from "@/lib/client"; import { useFulfillmentBaseContext } from "@/modules/dealModularEditorTabs/FulfillmentBase/shared/contexts/FulfillmentBaseContext"; +import useBarcodePrintActions from "@/modules/dealModularEditorTabs/FulfillmentBase/shared/hooks/utils/useBarcodePrintActions"; +import useProductActions from "@/modules/dealModularEditorTabs/FulfillmentBase/shared/hooks/utils/useProductActions"; type Props = { dealProduct: DealProductSchema; }; const ProductViewActions: FC = ({ dealProduct }) => { - const { dealProductsCrud, dealProductsList, productsCrud } = - useFulfillmentBaseContext(); - - const onProductEditClick = () => { - modals.openContextModal({ - modal: "productEditorModal", - title: "Редактирование товара", - withCloseButton: false, - innerProps: { - onChange: values => - productsCrud.onUpdate( - dealProduct.productId, - values, - dealProductsList.refetch - ), - entity: dealProduct.product, - isEditing: true, - clientId: dealProduct.product.clientId, - refetchProducts: dealProductsList.refetch, - }, - }); - }; - - const onPrintBarcodeClick = () => { - modals.openContextModal({ - modal: "printBarcodeModal", - title: "Печать штрихкода", - withCloseButton: true, - innerProps: { - product: dealProduct.product, - defaultQuantity: dealProduct.quantity, - }, - }); - }; + const { dealProductsCrud } = useFulfillmentBaseContext(); + const { onChangeClick } = useProductActions(); + const { onPrintProductBarcodeClick } = useBarcodePrintActions(); return ( = ({ dealProduct }) => { ml={"auto"} gap={"sm"}> onPrintProductBarcodeClick(dealProduct)} tipLabel="Печать штрихкода"> onChangeClick(dealProduct)} tipLabel="Редактировать товар"> diff --git a/src/modules/dealModularEditorTabs/FulfillmentBase/mobile/components/AddDealProductButton/AddDealProductButton.tsx b/src/modules/dealModularEditorTabs/FulfillmentBase/mobile/components/AddDealProductButton/AddDealProductButton.tsx index d2b9892..cfd57a0 100644 --- a/src/modules/dealModularEditorTabs/FulfillmentBase/mobile/components/AddDealProductButton/AddDealProductButton.tsx +++ b/src/modules/dealModularEditorTabs/FulfillmentBase/mobile/components/AddDealProductButton/AddDealProductButton.tsx @@ -1,40 +1,13 @@ import { FC } from "react"; import { IconPlus } from "@tabler/icons-react"; import { ButtonProps, Text } from "@mantine/core"; -import { modals } from "@mantine/modals"; import InlineButton from "@/components/ui/InlineButton/InlineButton"; -import { notifications } from "@/lib/notifications"; -import { useFulfillmentBaseContext } from "@/modules/dealModularEditorTabs/FulfillmentBase/shared/contexts/FulfillmentBaseContext"; +import useDealProductActions from "../../../shared/hooks/utils/useDealProductActions"; type Props = ButtonProps; const AddDealProductButton: FC = props => { - const { dealProductsList, dealProductsCrud, deal } = - useFulfillmentBaseContext(); - - const onCreateClick = () => { - if (!deal.client) { - notifications.error({ message: "Выберите клиента для сделки" }); - return; - } - - const productIdsToExclude = dealProductsList.dealProducts.map( - product => product.product.id - ); - - modals.openContextModal({ - modal: "dealProductEditorModal", - title: "Добавление товара", - withCloseButton: false, - innerProps: { - onCreate: values => - dealProductsCrud.onCreate({ ...values, dealId: deal.id }), - productIdsToExclude, - isEditing: false, - clientId: deal.client.id, - }, - }); - }; + const { onCreateClick } = useDealProductActions(); return ( = props => { - const { dealServicesList, dealServicesCrud, deal } = - useFulfillmentBaseContext(); - - const onCreateClick = () => { - const serviceIdsToExclude = dealServicesList.dealServices.map( - service => service.service.id - ); - modals.openContextModal({ - modal: "dealServiceEditorModal", - innerProps: { - onCreate: values => - dealServicesCrud.onCreate({ ...values, dealId: deal.id }), - serviceIdsToExclude, - isEditing: false, - }, - withCloseButton: false, - }); - }; + const { onCreateClick } = useServiceActions(); return ( = ({ dealProduct }) => { - const { dealProductsCrud, deal } = useFulfillmentBaseContext(); - - const onChangeDealProductClick = () => { - if (!deal.client) { - notifications.error({ message: "Выберите клиента для сделки" }); - return; - } - - modals.openContextModal({ - modal: "dealProductEditorModal", - title: "Добавление товара", - withCloseButton: false, - innerProps: { - onChange: values => - dealProductsCrud.onUpdate( - dealProduct.dealId, - dealProduct.productId, - values - ), - entity: dealProduct, - isEditing: true, - clientId: deal.client.id, - }, - }); - }; + const { dealProductsCrud } = useFulfillmentBaseContext(); + const { onChangeClick } = useDealProductActions(); return ( = ({ dealProduct }) => { {dealProduct.product.name} diff --git a/src/modules/dealModularEditorTabs/FulfillmentBase/shared/hooks/utils/useBarcodePrintActions.tsx b/src/modules/dealModularEditorTabs/FulfillmentBase/shared/hooks/utils/useBarcodePrintActions.tsx new file mode 100644 index 0000000..415a47c --- /dev/null +++ b/src/modules/dealModularEditorTabs/FulfillmentBase/shared/hooks/utils/useBarcodePrintActions.tsx @@ -0,0 +1,55 @@ +import { useMutation } from "@tanstack/react-query"; +import { modals } from "@mantine/modals"; +import { DealProductSchema } from "@/lib/client"; +import { getDealBarcodesPdfMutation } from "@/lib/client/@tanstack/react-query.gen"; +import { notifications } from "@/lib/notifications"; +import { useFulfillmentBaseContext } from "@/modules/dealModularEditorTabs/FulfillmentBase/shared/contexts/FulfillmentBaseContext"; +import base64ToBlob from "@/utils/base64ToBlob"; + +const useBarcodePrintActions = () => { + const { deal } = useFulfillmentBaseContext(); + + const onPrintProductBarcodeClick = (dealProduct: DealProductSchema) => { + modals.openContextModal({ + modal: "printBarcodeModal", + title: "Печать штрихкода", + withCloseButton: true, + innerProps: { + product: dealProduct.product, + defaultQuantity: dealProduct.quantity, + }, + }); + }; + + const getBarcodePdfMutation = useMutation({ + ...getDealBarcodesPdfMutation(), + onSuccess: response => { + const pdfBlob = base64ToBlob( + response.base64String, + response.mimeType + ); + const pdfUrl = URL.createObjectURL(pdfBlob); + const pdfWindow = window.open(pdfUrl); + if (!pdfWindow) { + notifications.error({ message: "Ошибка" }); + return; + } + pdfWindow.onload = () => pdfWindow.print(); + }, + }); + + const onPrintDealBarcodesClick = () => { + getBarcodePdfMutation.mutate({ + body: { + dealId: deal.id, + }, + }); + }; + + return { + onPrintProductBarcodeClick, + onPrintDealBarcodesClick, + }; +}; + +export default useBarcodePrintActions; diff --git a/src/modules/dealModularEditorTabs/FulfillmentBase/shared/hooks/utils/useDealProductActions.tsx b/src/modules/dealModularEditorTabs/FulfillmentBase/shared/hooks/utils/useDealProductActions.tsx new file mode 100644 index 0000000..316e8b7 --- /dev/null +++ b/src/modules/dealModularEditorTabs/FulfillmentBase/shared/hooks/utils/useDealProductActions.tsx @@ -0,0 +1,64 @@ +import { modals } from "@mantine/modals"; +import { DealProductSchema } from "@/lib/client"; +import { notifications } from "@/lib/notifications"; +import { useFulfillmentBaseContext } from "@/modules/dealModularEditorTabs/FulfillmentBase/shared/contexts/FulfillmentBaseContext"; + +const useDealProductActions = () => { + const { dealProductsList, dealProductsCrud, deal } = + useFulfillmentBaseContext(); + + const onCreateClick = () => { + if (!deal.client) { + notifications.error({ message: "Выберите клиента для сделки" }); + return; + } + + const productIdsToExclude = dealProductsList.dealProducts.map( + product => product.product.id + ); + + modals.openContextModal({ + modal: "dealProductEditorModal", + title: "Добавление товара", + withCloseButton: false, + innerProps: { + onCreate: values => + dealProductsCrud.onCreate({ ...values, dealId: deal.id }), + productIdsToExclude, + isEditing: false, + clientId: deal.client.id, + }, + }); + }; + + const onChangeClick = (dealProduct: DealProductSchema) => { + if (!deal.client) { + notifications.error({ message: "Выберите клиента для сделки" }); + return; + } + + modals.openContextModal({ + modal: "dealProductEditorModal", + title: "Добавление товара", + withCloseButton: false, + innerProps: { + onChange: values => + dealProductsCrud.onUpdate( + dealProduct.dealId, + dealProduct.productId, + values + ), + entity: dealProduct, + isEditing: true, + clientId: deal.client.id, + }, + }); + }; + + return { + onCreateClick, + onChangeClick, + }; +}; + +export default useDealProductActions; diff --git a/src/modules/dealModularEditorTabs/FulfillmentBase/shared/hooks/utils/useProductActions.tsx b/src/modules/dealModularEditorTabs/FulfillmentBase/shared/hooks/utils/useProductActions.tsx new file mode 100644 index 0000000..e8829f0 --- /dev/null +++ b/src/modules/dealModularEditorTabs/FulfillmentBase/shared/hooks/utils/useProductActions.tsx @@ -0,0 +1,55 @@ +import { modals } from "@mantine/modals"; +import { notifications } from "@/lib/notifications"; +import { useFulfillmentBaseContext } from "@/modules/dealModularEditorTabs/FulfillmentBase/shared/contexts/FulfillmentBaseContext"; +import { DealProductSchema } from "@/lib/client"; + +const useProductActions = () => { + const { deal, dealProductsList, productsCrud } = + useFulfillmentBaseContext(); + + const onCreateClick = () => { + if (!deal.client) { + notifications.error({ message: "Выберите клиента для сделки" }); + return; + } + + modals.openContextModal({ + modal: "productEditorModal", + title: "Создание товара", + withCloseButton: false, + innerProps: { + onCreate: productsCrud.onCreate, + isEditing: false, + clientId: deal.client.id, + refetchProducts: dealProductsList.refetch, + }, + }); + }; + + const onChangeClick = (dealProduct: DealProductSchema) => { + modals.openContextModal({ + modal: "productEditorModal", + title: "Редактирование товара", + withCloseButton: false, + innerProps: { + onChange: values => + productsCrud.onUpdate( + dealProduct.productId, + values, + dealProductsList.refetch + ), + entity: dealProduct.product, + isEditing: true, + clientId: dealProduct.product.clientId, + refetchProducts: dealProductsList.refetch, + }, + }); + }; + + return { + onCreateClick, + onChangeClick, + }; +}; + +export default useProductActions; diff --git a/src/modules/dealModularEditorTabs/FulfillmentBase/shared/hooks/utils/useServiceActions.tsx b/src/modules/dealModularEditorTabs/FulfillmentBase/shared/hooks/utils/useServiceActions.tsx new file mode 100644 index 0000000..1429019 --- /dev/null +++ b/src/modules/dealModularEditorTabs/FulfillmentBase/shared/hooks/utils/useServiceActions.tsx @@ -0,0 +1,54 @@ +import { modals } from "@mantine/modals"; +import { useFulfillmentBaseContext } from "@/modules/dealModularEditorTabs/FulfillmentBase/shared/contexts/FulfillmentBaseContext"; +import { addKitToDeal, ServicesKitSchema } from "@/lib/client"; +import { ServiceType } from "@/modules/dealModularEditorTabs/FulfillmentBase/shared/types/service"; + +const useServiceActions = () => { + const { dealServicesList, dealServicesCrud, deal } = + useFulfillmentBaseContext(); + + const onCreateClick = () => { + const serviceIdsToExclude = dealServicesList.dealServices.map( + service => service.service.id + ); + modals.openContextModal({ + modal: "dealServiceEditorModal", + innerProps: { + onCreate: values => + dealServicesCrud.onCreate({ ...values, dealId: deal.id }), + serviceIdsToExclude, + isEditing: false, + }, + withCloseButton: false, + }); + }; + + const onServicesKitAdd = (servicesKit: ServicesKitSchema) => { + addKitToDeal({ + body: { + dealId: deal.id, + kitId: servicesKit.id, + }, + }) + .then(() => dealServicesList.refetch()) + .catch(err => console.error(err)); + }; + + const onAddKitClick = () => { + modals.openContextModal({ + modal: "servicesKitSelectModal", + innerProps: { + onSelect: onServicesKitAdd, + serviceType: ServiceType.DEAL_SERVICE, + }, + withCloseButton: false, + }); + }; + + return { + onCreateClick, + onAddKitClick, + }; +}; + +export default useServiceActions; diff --git a/src/modules/dealModularEditorTabs/FulfillmentBase/shared/modals/PrintBarcodeModal/PrintBarcodeModal.tsx b/src/modules/dealModularEditorTabs/FulfillmentBase/shared/modals/PrintBarcodeModal/PrintBarcodeModal.tsx index 55f0a32..0be76ef 100644 --- a/src/modules/dealModularEditorTabs/FulfillmentBase/shared/modals/PrintBarcodeModal/PrintBarcodeModal.tsx +++ b/src/modules/dealModularEditorTabs/FulfillmentBase/shared/modals/PrintBarcodeModal/PrintBarcodeModal.tsx @@ -38,10 +38,10 @@ const PrintBarcodeModal = ({ innerProps }: ContextModalProps) => { }); const printBarcode = () => { - if (!barcode) return; + if (!barcode && !product.barcodeImageUrl) return; getBarcodePdfMutation.mutate({ body: { - barcode, + barcode: barcode ?? "", quantity, productId: product.id, },