feat: link pdf template to sales transactions

This commit is contained in:
Ahmed Bouhuolia
2024-09-11 16:49:44 +02:00
parent c0769662bd
commit ef74e250f1
12 changed files with 294 additions and 22 deletions

View File

@@ -1,7 +1,7 @@
import React from 'react';
import * as R from 'ramda';
import { Box } from '@/components';
import { Classes } from '@blueprintjs/core';
import { AppToaster, Box } from '@/components';
import { Classes, Intent } from '@blueprintjs/core';
import { useFormikContext } from 'formik';
import {
InvoicePaperTemplate,
@@ -12,9 +12,51 @@ import { InvoiceCustomizeGeneralField } from './InvoiceCustomizeGeneralFields';
import { InvoiceCustomizeContentFields } from './InvoiceCutomizeContentFields';
import { InvoiceCustomizeValues } from './types';
import { initialValues } from './constants';
import {
useCreatePdfTemplate,
useEditPdfTemplate,
} from '@/hooks/query/pdf-templates';
import { transformToEditRequest, transformToNewRequest } from './utils';
export default function InvoiceCustomizeContent() {
const handleFormSubmit = (values: InvoiceCustomizeValues) => {};
const { mutateAsync: createPdfTemplate } = useCreatePdfTemplate();
const { mutateAsync: editPdfTemplate } = useEditPdfTemplate();
const templateId: number = 1;
const handleFormSubmit = (values: InvoiceCustomizeValues) => {
const handleSuccess = (message: string) => {
AppToaster.show({
intent: Intent.SUCCESS,
message,
});
};
const handleError = (message: string) => {
AppToaster.show({
intent: Intent.DANGER,
message,
});
};
if (templateId) {
const reqValues = transformToEditRequest(values, templateId);
// Edit existing template
editPdfTemplate({ ...reqValues })
.then(() => handleSuccess('PDF template updated successfully!'))
.catch(() =>
handleError('An error occurred while updating the PDF template.'),
);
} else {
const reqValues = transformToNewRequest(values);
// Create new template
createPdfTemplate(reqValues)
.then(() => handleSuccess('PDF template created successfully!'))
.catch(() =>
handleError('An error occurred while creating the PDF template.'),
);
}
};
return (
<Box className={Classes.DRAWER_BODY}>

View File

@@ -0,0 +1,24 @@
import { omit } from 'lodash';
import { InvoiceCustomizeValues } from './types';
import { CreatePdfTemplateValues, EditPdfTemplateValues } from '@/hooks/query/pdf-templates';
export const transformToEditRequest = (
values: InvoiceCustomizeValues,
templateId: number,
): EditPdfTemplateValues => {
return {
templateId,
templateName: 'Template Name',
attributes: omit(values, ['templateName']),
};
};
export const transformToNewRequest = (
values: InvoiceCustomizeValues,
): CreatePdfTemplateValues => {
return {
resource: 'SaleInvoice',
templateName: 'Template Name',
attributes: omit(values, ['templateName']),
};
};