refactoring(receipt drawer): receipt drawer.

This commit is contained in:
elforjani3
2021-03-06 20:36:36 +02:00
parent 252f3b4617
commit 51059bc277
6 changed files with 95 additions and 23 deletions

View File

@@ -5,9 +5,7 @@ import withDrawerActions from 'containers/Drawer/withDrawerActions';
import { Drawer, DrawerSuspense } from 'components';
import { compose } from 'utils';
const ReceiptDrawerContent = lazy(() =>
import('containers/Drawers/PaperTemplate/PaperTemplate'),
);
const ReceiptDrawerContent = lazy(() => import('./ReceiptDrawerContent'));
/**
* receipt drawer.
@@ -16,7 +14,7 @@ const ReceiptDrawer = ({
name,
//#withDrawer
isOpen,
payload,
payload: { receiptId },
closeDrawer,
}) => {
@@ -25,23 +23,11 @@ const ReceiptDrawer = ({
closeDrawer(name);
};
const propLabels = {
labels: {
name: 'Receipt',
billedTo: 'Billed to',
date: 'Receipt date',
refNo: 'Receipt No.',
billedFrom: 'Billed from',
amount: 'Receipt amount',
dueDate: 'Due date',
},
};
return (
<div>
<Drawer isOpen={isOpen} isClose={handleDrawerClose}>
<DrawerSuspense>
<ReceiptDrawerContent labels={propLabels.labels} />
<ReceiptDrawerContent receiptId={receiptId} />
</DrawerSuspense>
</Drawer>
</div>

View File

@@ -0,0 +1,17 @@
import React from 'react';
import { ReceiptDrawerProvider } from './ReceiptDrawerProvider';
import ReceiptPaper from './ReceiptPaper';
/**
* Receipt drawer content.
*/
export default function ReceiptDrawerContent({
// #ownProp
receiptId,
}) {
return (
<ReceiptDrawerProvider receiptId={receiptId}>
<ReceiptPaper />
</ReceiptDrawerProvider>
);
}

View File

@@ -0,0 +1,32 @@
import React, { createContext, useContext } from 'react';
import { useReceipt } from 'hooks/query';
import DashboardInsider from 'components/Dashboard/DashboardInsider';
const ReceiptDrawerContext = createContext();
function ReceiptDrawerProvider({ receiptId, ...props }) {
// Fetch sale receipt details.
const {
data: { entries, ...receipt },
isFetching: isReceiptLoading,
} = useReceipt(receiptId, {
enabled: !!receiptId,
});
// Provider payload.
const provider = {
receiptId,
receipt,
entries,
isReceiptLoading,
};
return (
<DashboardInsider loading={isReceiptLoading}>
<ReceiptDrawerContext.Provider value={provider} {...props} />
</DashboardInsider>
);
}
const useReceiptDrawerContext = () => useContext(ReceiptDrawerContext);
export { ReceiptDrawerProvider, useReceiptDrawerContext };

View File

@@ -0,0 +1,37 @@
import React from 'react';
import { useReceiptDrawerContext } from './ReceiptDrawerProvider';
import PaperTemplate from 'containers/Drawers/PaperTemplate/PaperTemplate';
export default function ReceiptPaper() {
const { receipt, entries } = useReceiptDrawerContext();
const propLabels = {
labels: {
name: 'Receipt',
billedTo: 'Billed to',
date: 'Receipt date',
refNo: 'Receipt No.',
billedFrom: 'Billed from',
amount: 'Receipt amount',
dueDate: 'Due date',
},
};
const receipts = {
billedTo: receipt.customer.display_name,
date: receipt.receipt_date,
amount: '',
billedFrom: '',
dueDate: receipt.due_date,
referenceNo: receipt.receipt_number,
...receipt,
};
return (
<PaperTemplate
labels={propLabels.labels}
paperData={receipts}
entries={entries}
/>
);
}

View File

@@ -65,10 +65,10 @@ function ReceiptsDataTable({
openAlert('receipt-close', { receiptId: receipt.id });
};
// Handle drawer receipts.
const handleDrawerReceipt = () => {
openDrawer('receipt-drawer', {});
};
// Handle drawer receipts.
const handleDrawerReceipt = ({ id }) => {
openDrawer('receipt-drawer', { receiptId: id });
};
// Handles the datable fetch data once the state changing.
const handleDataTableFetchData = useCallback(
@@ -111,7 +111,7 @@ function ReceiptsDataTable({
onEdit: handleEditReceipt,
onDelete: handleDeleteReceipt,
onClose: handleCloseReceipt,
onDrawer:handleDrawerReceipt,
onDrawer: handleDrawerReceipt,
baseCurrency,
}}
/>

View File

@@ -41,7 +41,7 @@ export function ActionsMenu({
<MenuItem
icon={<Icon icon={'receipt-24'} iconSize={16} />}
text={formatMessage({ id: 'receipt_paper' })}
onClick={() => onDrawer()}
onClick={safeCallback(onDrawer, receipt)}
/>
<MenuItem
text={formatMessage({ id: 'delete_receipt' })}