fix: Pdf branding templates request data

This commit is contained in:
Ahmed Bouhuolia
2024-10-07 16:03:56 +02:00
parent bbdfe00c7a
commit 9edfb83221
20 changed files with 142 additions and 72 deletions

View File

@@ -1,19 +1,21 @@
import React, { createContext, useContext } from 'react';
import { Spinner } from '@blueprintjs/core';
import {
GetPdfTemplateBrandingStateResponse,
GetPdfTemplateResponse,
useGetPdfTemplate,
useGetPdfTemplateBrandingState,
} from '@/hooks/query/pdf-templates';
import { Spinner } from '@blueprintjs/core';
interface PdfTemplateContextValue {
templateId: number | string;
pdfTemplate: GetPdfTemplateResponse | undefined;
// Pdf template.
pdfTemplate: GetPdfTemplateResponse;
isPdfTemplateLoading: boolean;
// Branding state.
brandingTemplateState: GetPdfTemplateBrandingStateResponse | undefined;
brandingTemplateState: GetPdfTemplateBrandingStateResponse;
isBrandingTemplateLoading: boolean;
}
@@ -34,10 +36,18 @@ export const BrandingTemplateBoot = ({
useGetPdfTemplate(templateId, {
enabled: !!templateId,
});
// Retreives the branding template state.
// Retrieves the branding template state.
const { data: brandingTemplateState, isLoading: isBrandingTemplateLoading } =
useGetPdfTemplateBrandingState();
const isLoading = isPdfTemplateLoading ||
isBrandingTemplateLoading ||
!brandingTemplateState ||
!pdfTemplate;
if (isLoading) {
return <Spinner size={20} />;
}
const value = {
templateId,
pdfTemplate,
@@ -47,11 +57,6 @@ export const BrandingTemplateBoot = ({
isBrandingTemplateLoading,
};
const isLoading = isPdfTemplateLoading || isBrandingTemplateLoading;
if (isLoading) {
return <Spinner size={20} />;
}
return (
<PdfTemplateContext.Provider value={value}>
{children}

View File

@@ -8,6 +8,7 @@ import {
import {
transformToEditRequest,
transformToNewRequest,
useBrandingState,
useBrandingTemplateFormInitialValues,
} from './_utils';
import { AppToaster } from '@/components';
@@ -17,31 +18,42 @@ import {
useEditPdfTemplate,
} from '@/hooks/query/pdf-templates';
import { FormikHelpers } from 'formik';
import { BrandingTemplateValues } from './types';
import { BrandingTemplateValues, BrandingState } from './types';
import { useUploadAttachments } from '@/hooks/query/attachments';
import { excludePrivateProps } from '@/utils';
interface BrandingTemplateFormProps<T> extends ElementCustomizeProps<T> {
interface BrandingTemplateFormProps<
T extends BrandingTemplateValues,
Y extends BrandingState
> extends ElementCustomizeProps<T, Y> {
resource: string;
templateId?: number;
onSuccess?: () => void;
onError?: () => void;
/* The default values hold the initial values of the form and the values being sent to the server. */
defaultValues?: T;
}
export function BrandingTemplateForm<T extends BrandingTemplateValues>({
export function BrandingTemplateForm<
T extends BrandingTemplateValues,
Y extends BrandingState,
>({
templateId,
onSuccess,
onError,
defaultValues,
resource,
...props
}: BrandingTemplateFormProps<T>) {
}: BrandingTemplateFormProps<T, Y>) {
// Create/edit pdf template mutators.
const { mutateAsync: createPdfTemplate } = useCreatePdfTemplate();
const { mutateAsync: editPdfTemplate } = useEditPdfTemplate();
const initialValues = useBrandingTemplateFormInitialValues<T>(defaultValues);
const [isUploading, setIsLoading] = useState<boolean>(false);
const brandingState = useBrandingState();
const [, setIsLoading] = useState<boolean>(false);
// Uploads the attachments.
const { mutateAsync: uploadAttachments } = useUploadAttachments({
@@ -122,10 +134,11 @@ export function BrandingTemplateForm<T extends BrandingTemplateValues>({
};
return (
<ElementCustomize<T>
<ElementCustomize<T, Y>
initialValues={initialValues}
validationSchema={validationSchema}
onSubmit={handleFormSubmit}
brandingState={brandingState || {}}
{...props}
/>
);

View File

@@ -6,7 +6,7 @@ import {
} from '@/hooks/query/pdf-templates';
import { useBrandingTemplateBoot } from './BrandingTemplateBoot';
import { transformToForm } from '@/utils';
import { BrandingTemplateValues } from './types';
import { BrandingState, BrandingTemplateValues } from './types';
import { DRAWERS } from '@/constants/drawers';
const commonExcludedAttrs = ['templateName', 'companyLogoUri'];
@@ -48,7 +48,7 @@ export const useBrandingTemplateFormInitialValues = <
const brandingAttributes = {
templateName: pdfTemplate?.templateName,
...brandingTemplateState,
// ...brandingTemplateState,
...pdfTemplate?.attributes,
};
return {
@@ -57,6 +57,15 @@ export const useBrandingTemplateFormInitialValues = <
};
};
export const useBrandingState = (state?: Partial<BrandingState>): BrandingState => {
const { brandingTemplateState } = useBrandingTemplateBoot();
return {
...brandingTemplateState,
...state
}
}
export const getCustomizeDrawerNameFromResource = (resource: string) => {
const pairs = {
SaleInvoice: DRAWERS.INVOICE_CUSTOMIZE,

View File

@@ -6,4 +6,18 @@ export interface BrandingTemplateValues {
// Company logo
companyLogoKey?: string;
companyLogoUri?: string;
}
export interface BrandingState extends ElementPreviewState {
companyName: string;
companyAddress: string;
companyLogoKey: string;
companyLogoUri: string;
primaryColor: string;
}
export interface ElementPreviewState {
}