import React from 'react';
import { isNull, isEmpty } from 'lodash';
import { compose, curry } from 'lodash/fp';
import styled from 'styled-components';
import { Link } from 'react-router-dom';
import { ContextMenu2 } from '@blueprintjs/popover2';
import { Menu, MenuItem, MenuDivider, Intent } from '@blueprintjs/core';
import intl from 'react-intl-universal';
import {
BankAccountsList,
BankAccount,
If,
Icon,
T,
Can,
} from '../../../components';
import {
AccountAction,
CashflowAction,
AbilitySubject,
} from '../../../common/abilityOption';
import { useCashFlowAccountsContext } from './CashFlowAccountsProvider';
import withDrawerActions from '../../Drawer/withDrawerActions';
import withAlertsActions from '../../Alert/withAlertActions';
import withDialogActions from '../../Dialog/withDialogActions';
import { safeCallback } from 'utils';
import { addMoneyIn, addMoneyOut } from 'common/cashflowOptions';
const CASHFLOW_SKELETON_N = 4;
/**
* Cashflow accounts skeleton for loading state.
*/
function CashflowAccountsSkeleton() {
return [...Array(CASHFLOW_SKELETON_N)].map((e, i) => (
));
}
/**
* Cashflow bank account.
*/
function CashflowBankAccount({
// #withAlertsDialog
openAlert,
// #withDial
openDialog,
// #withDrawerActions
openDrawer,
account,
}) {
// Handle view detail account.
const handleViewClick = () => {
openDrawer('account-drawer', { accountId: account.id });
};
// Handle delete action account.
const handleDeleteClick = () => {
openAlert('account-delete', { accountId: account.id });
};
// Handle inactivate action account.
const handleInactivateClick = () => {
openAlert('account-inactivate', { accountId: account.id });
};
// Handle activate action account.
const handleActivateClick = () => {
openAlert('account-activate', { accountId: account.id });
};
// Handle edit account action.
const handleEditAccount = () => {
openDialog('account-form', { action: 'edit', id: account.id });
};
// Handle money in menu item actions.
const handleMoneyInClick = (transactionType) => {
openDialog('money-in', {
account_type: transactionType,
account_id: account.id,
});
};
// Handle money out menu item actions.
const handleMoneyOutClick = (transactionType) => {
openDialog('money-out', {
account_type: transactionType,
account_id: account.id,
});
};
return (
}
>
);
}
const CashflowBankAccountEnhanced = compose(
withAlertsActions,
withDrawerActions,
withDialogActions,
)(CashflowBankAccount);
function getUpdatedBeforeText(createdAt) {
return '';
}
/**
* Cashflow accounts grid items.
*/
function CashflowAccountsGridItems({ accounts }) {
return accounts.map((account) => (
));
}
/**
* Cashflow accounts empty state.
*/
function CashflowAccountsEmptyState() {
return (
);
}
/**
* Cashflow accounts grid.
*/
export default function CashflowAccountsGrid() {
// Retrieve list context.
const { cashflowAccounts, isCashFlowAccountsLoading } =
useCashFlowAccountsContext();
return (
{isCashFlowAccountsLoading ? (
) : isEmpty(cashflowAccounts) ? (
) : (
)}
);
}
/**
* Cashflow account money out context menu.
*/
function CashflowAccountMoneyInContextMenu({ onClick }) {
const handleItemClick = curry((transactionType, event) => {
onClick && onClick(transactionType, event);
});
return addMoneyIn.map((option) => (
));
}
/**
* Cashflow account money in context menu.
*/
function CashflowAccountMoneyOutContextMenu({ onClick }) {
const handleItemClick = curry((transactionType, event) => {
onClick && onClick(transactionType, event);
});
return addMoneyOut.map((option) => (
));
}
/**
* Cashflow account context menu.
*/
function CashflowAccountContextMenu({
account,
onViewClick,
onEditClick,
onInactivateClick,
onActivateClick,
onDeleteClick,
onMoneyInClick,
onMoneyOutClick,
}) {
return (
);
}
const CashflowAccountAnchor = styled(Link)`
&,
&:hover,
&:focus,
&:active {
color: inherit;
text-decoration: none;
}
`;
const CashflowAccountsGridWrap = styled.div`
margin: 30px;
`;
const CashflowBankAccountWrap = styled.div``;
const AccountsEmptyStateBase = styled.div`
flex: 1;
text-align: center;
margin: 2rem 0;
`;
const AccountsEmptyStateTitle = styled.h1`
font-size: 16px;
color: #626b76;
opacity: 0.8;
line-height: 1.6;
font-weight: 500;
`;