refactoring(invoice drawer): invoice drawer.

This commit is contained in:
elforjani3
2021-03-06 20:29:32 +02:00
parent fe5fc80ecb
commit 252f3b4617
6 changed files with 109 additions and 21 deletions

View File

@@ -5,9 +5,7 @@ import withDrawerActions from 'containers/Drawer/withDrawerActions';
import { Drawer, DrawerSuspense } from 'components';
import { compose } from 'utils';
const InvoicesDrawerContent = lazy(() =>
import('containers/Drawers/PaperTemplate/PaperTemplate'),
);
const InvoicesDrawerContent = lazy(() => import('./InvoiceDrawerContent'));
/**
* invoice drawer.
@@ -16,7 +14,7 @@ function InvoiceDrawer({
name,
//#withDrawer
isOpen,
payload,
payload: { invoiceId },
closeDrawer,
}) {
@@ -25,22 +23,10 @@ function InvoiceDrawer({
closeDrawer(name);
};
const propLabels = {
labels: {
name: 'Invoice',
billedTo: 'Billed to',
date: 'Invoice date',
refNo: 'Invoice No.',
billedFrom: 'Billed from',
amount: 'Invoice amount',
dueDate: 'Due date',
},
};
return (
<Drawer isOpen={isOpen} isClose={handleDrawerClose}>
<DrawerSuspense>
<InvoicesDrawerContent labels={propLabels.labels} />
<InvoicesDrawerContent invoiceId={invoiceId} />
</DrawerSuspense>
</Drawer>
);

View File

@@ -0,0 +1,18 @@
import React from 'react';
import { InvoiceDrawerProvider } from './InvoiceDrawerProvider';
import InvoicePaper from './InvoicePaper';
/**
* Invoice drawer content.
*/
export default function InvoiceDrawerContent({
// #ownProp
invoiceId,
}) {
return (
<InvoiceDrawerProvider invoiceId={invoiceId}>
<InvoicePaper />
</InvoiceDrawerProvider>
);
}

View File

@@ -0,0 +1,35 @@
import React, { createContext, useContext } from 'react';
import { useInvoice } from 'hooks/query';
import DashboardInsider from 'components/Dashboard/DashboardInsider';
const InvoiceDrawerContext = createContext();
/**
* Invoice drawer provider.
*/
function InvoiceDrawerProvider({ invoiceId, ...props }) {
// Fetch sale invoice details.
const {
data: { entries, ...invoice },
isLoading: isInvoiceLoading,
} = useInvoice(invoiceId, {
enabled: !!invoiceId,
});
// Provider payload.
const provider = {
invoiceId,
invoice,
entries,
isInvoiceLoading,
};
return (
<DashboardInsider loading={isInvoiceLoading}>
<InvoiceDrawerContext.Provider value={provider} {...props} />
</DashboardInsider>
);
}
const useInvoiceDrawerContext = () => useContext(InvoiceDrawerContext);
export { InvoiceDrawerProvider, useInvoiceDrawerContext };

View File

@@ -0,0 +1,37 @@
import React from 'react';
import { useInvoiceDrawerContext } from './InvoiceDrawerProvider';
import PaperTemplate from 'containers/Drawers/PaperTemplate/PaperTemplate';
export default function InvoicePaper() {
const { invoice, entries } = useInvoiceDrawerContext();
const propLabels = {
labels: {
name: 'Invoice',
billedTo: 'Billed to',
date: 'Invoice date',
refNo: 'Invoice No.',
billedFrom: 'Billed from',
amount: 'Invoice amount',
dueDate: 'Due date',
},
};
const defaultValues = {
billedTo: invoice.customer.display_name,
date: invoice.invoice_date,
amount: invoice.balance,
billedFrom: '',
dueDate: invoice.due_date,
referenceNo: invoice.invoice_no,
...invoice,
};
return (
<PaperTemplate
labels={propLabels.labels}
paperData={defaultValues}
entries={entries}
/>
);
}

View File

@@ -33,7 +33,7 @@ function InvoicesDataTable({
// #withAlertsActions
openAlert,
// #withDrawerActions
openDrawer,
}) {
@@ -67,8 +67,8 @@ function InvoicesDataTable({
};
// Handle drawer invoice.
const handleDrawerInvoice = () => {
openDrawer('invoice-drawer', {});
const handleDrawerInvoice = ({ id }) => {
openDrawer('invoice-drawer', { invoiceId: id });
};
// Handles fetch data once the table state change.

View File

@@ -90,6 +90,18 @@ export const handleDeleteErrors = (errors) => {
intent: Intent.DANGER,
});
}
if (
errors.find(
(error) => error.type === 'INVOICE_AMOUNT_SMALLER_THAN_PAYMENT_AMOUNT',
)
) {
AppToaster.show({
message: formatMessage({
id: 'the_payment_amount_that_received',
}),
intent: Intent.DANGER,
});
}
};
export function ActionsMenu({
@@ -120,7 +132,7 @@ export function ActionsMenu({
<MenuItem
icon={<Icon icon={'receipt-24'} iconSize={16} />}
text={formatMessage({ id: 'invoice_paper' })}
onClick={() => onDrawer()}
onClick={safeCallback(onDrawer, original)}
/>
<MenuItem
text={formatMessage({ id: 'delete_invoice' })}