import React, { useEffect, useCallback, useMemo } from 'react'; import { Intent, Button, Popover, Menu, MenuItem, MenuDivider, Position, } from '@blueprintjs/core'; import { useParams } from 'react-router-dom'; import { withRouter } from 'react-router'; import { FormattedMessage as T, useIntl } from 'react-intl'; import moment from 'moment'; import classNames from 'classnames'; import Icon from 'components/Icon'; import { compose, saveInvoke } from 'utils'; import { CLASSES } from 'common/classes'; import { useIsValuePassed } from 'hooks'; import { LoadingIndicator, Choose } from 'components'; import DataTable from 'components/DataTable'; import BillsEmptyStatus from './BillsEmptyStatus'; import withDialogActions from 'containers/Dialog/withDialogActions'; import withDashboardActions from 'containers/Dashboard/withDashboardActions'; import withViewDetails from 'containers/Views/withViewDetails'; import withBills from './withBills'; import withBillActions from './withBillActions'; import withCurrentView from 'containers/Views/withCurrentView'; // Bills transactions datatable. function BillsDataTable({ // #withBills billsCurrentPage, billsLoading, billsPageination, billsCurrentViewId, // #withDashboardActions changeCurrentView, changePageSubtitle, setTopbarEditView, // #withBillsActions addBillsTableQueries, // #withView viewMeta, // #ownProps loading, onFetchData, onEditBill, onDeleteBill, onSelectedRowsChange, }) { const { custom_view_id: customViewId } = useParams(); const { formatMessage } = useIntl(); const isLoadedBefore = useIsValuePassed(billsLoading, false); useEffect(() => { if (customViewId) { changeCurrentView(customViewId); setTopbarEditView(customViewId); } changePageSubtitle(customViewId && viewMeta ? viewMeta.name : ''); }, [ customViewId, changeCurrentView, changePageSubtitle, setTopbarEditView, viewMeta, ]); const handleFetchData = useCallback( ({ pageIndex, pageSize, sortBy }) => { const page = pageIndex + 1; addBillsTableQueries({ ...(sortBy.length > 0 ? { column_sort_by: sortBy[0].id, sort_order: sortBy[0].desc ? 'desc' : 'asc', } : {}), page_size: pageSize, page, }); }, [addBillsTableQueries], ); const handleEditBill = useCallback( (_bill) => () => { saveInvoke(onEditBill, _bill); }, [onEditBill], ); const handleDeleteBill = useCallback( (_bill) => () => { saveInvoke(onDeleteBill, _bill); }, [onDeleteBill], ); const actionMenuList = useCallback( (bill) => (
), [handleDeleteBill, handleEditBill, formatMessage], ); const onRowContextMenu = useCallback( (cell) => { return actionMenuList(cell.row.original); }, [actionMenuList], ); const columns = useMemo( () => [ { id: 'bill_date', Header: formatMessage({ id: 'bill_date' }), accessor: (r) => moment(r.bill_date).format('YYYY MMM DD'), width: 140, className: 'bill_date', }, { id: 'vendor_id', Header: formatMessage({ id: 'vendor_name' }), accessor: 'vendor.display_name', width: 140, className: 'vendor_id', }, { id: 'bill_number', Header: formatMessage({ id: 'bill_number' }), accessor: (row) => (row.bill_number ? `#${row.bill_number}` : null), width: 140, className: 'bill_number', }, { id: 'due_date', Header: formatMessage({ id: 'due_date' }), accessor: (r) => moment(r.due_date).format('YYYY MMM DD'), width: 140, className: 'due_date', }, { id: 'amount', Header: formatMessage({ id: 'amount' }), accessor: 'amount', width: 140, className: 'amount', }, { id: 'reference_no', Header: formatMessage({ id: 'reference_no' }), accessor: 'reference_no', width: 140, className: 'reference_no', }, { id: 'status', Header: formatMessage({ id: 'status' }), accessor: 'status', width: 140, className: 'status', }, { id: 'actions', Header: '', Cell: ({ cell }) => (