From 252f3b46173c68c45baba2df35e3ac033e18af7f Mon Sep 17 00:00:00 2001 From: elforjani3 Date: Sat, 6 Mar 2021 20:29:32 +0200 Subject: [PATCH] refactoring(invoice drawer): invoice drawer. --- .../Invoices/InvoiceDetails/InvoiceDrawer.js | 20 ++-------- .../InvoiceDetails/InvoiceDrawerContent.js | 18 +++++++++ .../InvoiceDetails/InvoiceDrawerProvider.js | 35 ++++++++++++++++++ .../Invoices/InvoiceDetails/InvoicePaper.js | 37 +++++++++++++++++++ .../InvoicesLanding/InvoicesDataTable.js | 6 +-- .../Invoices/InvoicesLanding/components.js | 14 ++++++- 6 files changed, 109 insertions(+), 21 deletions(-) create mode 100644 client/src/containers/Sales/Invoices/InvoiceDetails/InvoiceDrawerContent.js create mode 100644 client/src/containers/Sales/Invoices/InvoiceDetails/InvoiceDrawerProvider.js create mode 100644 client/src/containers/Sales/Invoices/InvoiceDetails/InvoicePaper.js diff --git a/client/src/containers/Sales/Invoices/InvoiceDetails/InvoiceDrawer.js b/client/src/containers/Sales/Invoices/InvoiceDetails/InvoiceDrawer.js index b7369eedc..a33d32379 100644 --- a/client/src/containers/Sales/Invoices/InvoiceDetails/InvoiceDrawer.js +++ b/client/src/containers/Sales/Invoices/InvoiceDetails/InvoiceDrawer.js @@ -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 ( - + ); diff --git a/client/src/containers/Sales/Invoices/InvoiceDetails/InvoiceDrawerContent.js b/client/src/containers/Sales/Invoices/InvoiceDetails/InvoiceDrawerContent.js new file mode 100644 index 000000000..c7a590539 --- /dev/null +++ b/client/src/containers/Sales/Invoices/InvoiceDetails/InvoiceDrawerContent.js @@ -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 ( + + + + ); +} diff --git a/client/src/containers/Sales/Invoices/InvoiceDetails/InvoiceDrawerProvider.js b/client/src/containers/Sales/Invoices/InvoiceDetails/InvoiceDrawerProvider.js new file mode 100644 index 000000000..4ae1b4fad --- /dev/null +++ b/client/src/containers/Sales/Invoices/InvoiceDetails/InvoiceDrawerProvider.js @@ -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 ( + + + + ); +} +const useInvoiceDrawerContext = () => useContext(InvoiceDrawerContext); + +export { InvoiceDrawerProvider, useInvoiceDrawerContext }; diff --git a/client/src/containers/Sales/Invoices/InvoiceDetails/InvoicePaper.js b/client/src/containers/Sales/Invoices/InvoiceDetails/InvoicePaper.js new file mode 100644 index 000000000..2e0d01757 --- /dev/null +++ b/client/src/containers/Sales/Invoices/InvoiceDetails/InvoicePaper.js @@ -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 ( + + ); +} diff --git a/client/src/containers/Sales/Invoices/InvoicesLanding/InvoicesDataTable.js b/client/src/containers/Sales/Invoices/InvoicesLanding/InvoicesDataTable.js index cc500013a..08d8c493a 100644 --- a/client/src/containers/Sales/Invoices/InvoicesLanding/InvoicesDataTable.js +++ b/client/src/containers/Sales/Invoices/InvoicesLanding/InvoicesDataTable.js @@ -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. diff --git a/client/src/containers/Sales/Invoices/InvoicesLanding/components.js b/client/src/containers/Sales/Invoices/InvoicesLanding/components.js index 1d1499822..fb880bca0 100644 --- a/client/src/containers/Sales/Invoices/InvoicesLanding/components.js +++ b/client/src/containers/Sales/Invoices/InvoicesLanding/components.js @@ -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({ } text={formatMessage({ id: 'invoice_paper' })} - onClick={() => onDrawer()} + onClick={safeCallback(onDrawer, original)} />