mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-16 12:50:38 +00:00
78 lines
1.9 KiB
JavaScript
78 lines
1.9 KiB
JavaScript
import React from 'react';
|
|
import { DataTableEditable } from 'components';
|
|
import {
|
|
compose,
|
|
saveInvoke,
|
|
updateMinEntriesLines,
|
|
updateRemoveLineByIndex,
|
|
updateAutoAddNewLine,
|
|
updateTableRow,
|
|
} from 'utils';
|
|
import { useMakeJournalFormContext } from './MakeJournalProvider';
|
|
import { useJournalTableEntriesColumns } from './components';
|
|
import { updateAdjustEntries } from './utils';
|
|
|
|
/**
|
|
* Make journal entries table component.
|
|
*/
|
|
export default function MakeJournalEntriesTable({
|
|
// #ownPorps
|
|
onChange,
|
|
entries,
|
|
defaultEntry,
|
|
error,
|
|
initialLinesNumber = 4,
|
|
minLinesNumber = 4,
|
|
currencyCode,
|
|
}) {
|
|
const { accounts, contacts } = useMakeJournalFormContext();
|
|
|
|
// Memorized data table columns.
|
|
const columns = useJournalTableEntriesColumns();
|
|
|
|
// Handles update datatable data.
|
|
const handleUpdateData = (rowIndex, columnId, value) => {
|
|
const newRows = compose(
|
|
// Auto-adding new lines.
|
|
updateAutoAddNewLine(defaultEntry, ['account_id', 'credit', 'debit']),
|
|
// Update items entries total.
|
|
updateAdjustEntries(rowIndex, columnId, value),
|
|
// Update entry of the given row index and column id.
|
|
updateTableRow(rowIndex, columnId, value),
|
|
)(entries);
|
|
|
|
saveInvoke(onChange, newRows);
|
|
};
|
|
|
|
// Handle remove datatable row.
|
|
const handleRemoveRow = (rowIndex) => {
|
|
const newRows = compose(
|
|
// Ensure minimum lines count.
|
|
updateMinEntriesLines(minLinesNumber, defaultEntry),
|
|
// Remove the line by the given index.
|
|
updateRemoveLineByIndex(rowIndex),
|
|
)(entries);
|
|
|
|
saveInvoke(onChange, newRows);
|
|
};
|
|
|
|
return (
|
|
<DataTableEditable
|
|
columns={columns}
|
|
data={entries}
|
|
sticky={true}
|
|
totalRow={true}
|
|
footer={true}
|
|
payload={{
|
|
accounts,
|
|
errors: error,
|
|
updateData: handleUpdateData,
|
|
removeRow: handleRemoveRow,
|
|
contacts,
|
|
autoFocus: ['account_id', 0],
|
|
currencyCode,
|
|
}}
|
|
/>
|
|
);
|
|
}
|