diff --git a/packages/webapp/src/containers/AlertsContainer/registered.tsx b/packages/webapp/src/containers/AlertsContainer/registered.tsx index 9bf51e222..d1257cc1b 100644 --- a/packages/webapp/src/containers/AlertsContainer/registered.tsx +++ b/packages/webapp/src/containers/AlertsContainer/registered.tsx @@ -26,6 +26,7 @@ import BranchesAlerts from '@/containers/Preferences/Branches/BranchesAlerts'; import ProjectAlerts from '@/containers/Projects/containers/ProjectAlerts'; import TaxRatesAlerts from '@/containers/TaxRates/alerts'; import { CashflowAlerts } from '../CashFlow/CashflowAlerts'; +import { BankRulesAlerts } from '../Banking/Rules/RulesList/BankRulesAlerts'; export default [ ...AccountsAlerts, @@ -55,4 +56,5 @@ export default [ ...ProjectAlerts, ...TaxRatesAlerts, ...CashflowAlerts, + ...BankRulesAlerts ]; diff --git a/packages/webapp/src/containers/Banking/Rules/RulesList/BankRulesAlerts.ts b/packages/webapp/src/containers/Banking/Rules/RulesList/BankRulesAlerts.ts new file mode 100644 index 000000000..5daec4359 --- /dev/null +++ b/packages/webapp/src/containers/Banking/Rules/RulesList/BankRulesAlerts.ts @@ -0,0 +1,16 @@ +// @ts-nocheck +import React from 'react'; + +const DeleteBankRuleAlert = React.lazy( + () => import('./alerts/DeleteBankRuleAlert'), +); + +/** + * Cashflow alerts. + */ +export const BankRulesAlerts = [ + { + name: 'bank-rule-delete', + component: DeleteBankRuleAlert, + }, +]; diff --git a/packages/webapp/src/containers/Banking/Rules/RulesList/RulesListBoot.tsx b/packages/webapp/src/containers/Banking/Rules/RulesList/RulesListBoot.tsx index f8c2cbd69..a022d150e 100644 --- a/packages/webapp/src/containers/Banking/Rules/RulesList/RulesListBoot.tsx +++ b/packages/webapp/src/containers/Banking/Rules/RulesList/RulesListBoot.tsx @@ -4,6 +4,7 @@ import { useBankRules } from '@/hooks/query/bank-rules'; interface RulesListBootValues { bankRules: any; + isBankRulesLoading: boolean; } const RulesListBootContext = createContext( @@ -18,10 +19,11 @@ function RulesListBoot({ ...props }: RulesListBootProps) { const { data: bankRules, isLoading: isBankRulesLoading } = useBankRules(); const provider = { bankRules, isBankRulesLoading } as RulesListBootValues; + const isLoading = isBankRulesLoading; return ( - - + + ); } diff --git a/packages/webapp/src/containers/Banking/Rules/RulesList/RulesTable.tsx b/packages/webapp/src/containers/Banking/Rules/RulesList/RulesTable.tsx index 5d5d9a88f..e81d5dbc2 100644 --- a/packages/webapp/src/containers/Banking/Rules/RulesList/RulesTable.tsx +++ b/packages/webapp/src/containers/Banking/Rules/RulesList/RulesTable.tsx @@ -15,6 +15,7 @@ import withDashboardActions from '@/containers/Dashboard/withDashboardActions'; import { useBankRulesTableColumns } from './hooks'; import { BankRulesTableActionsMenu } from './_components'; import { BankRulesLandingEmptyState } from './BankRulesLandingEmptyState'; +import { useRulesListBoot } from './RulesListBoot'; /** * Invoices datatable. @@ -32,8 +33,12 @@ function RulesTable({ // Invoices table columns. const columns = useBankRulesTableColumns(); + const { bankRules } = useRulesListBoot(); + // Handle edit bank rule. - const handleDeleteBankRule = ({ id }) => {}; + const handleDeleteBankRule = ({ id }) => { + openAlert('bank-rule-delete', { id }); + }; // Handle delete bank rule. const handleEditBankRule = () => {}; @@ -49,7 +54,7 @@ function RulesTable({ - } text={'Edit Rule'} onClick={safeCallback(onEdit, original)} /> - { + closeAlert(name); + }; + + // handleConfirm delete project + const handleConfirmBtnClick = () => { + deleteBankRule(id) + .then(() => { + AppToaster.show({ + message: 'The bank rule has deleted successfully.', + intent: Intent.SUCCESS, + }); + closeAlert(name); + }) + .catch( + ({ + response: { + data: { errors }, + }, + }) => { + AppToaster.show({ + message: 'Something went wrong.', + intent: Intent.DANGER, + }); + }, + ); + }; + + return ( + } + confirmButtonText={'Delete'} + intent={Intent.DANGER} + isOpen={isOpen} + onCancel={handleCancelDeleteAlert} + onConfirm={handleConfirmBtnClick} + loading={isLoading} + > +

Are you sure want to delete the bank rule?

+
+ ); +} + +export default compose( + withAlertStoreConnect(), + withAlertActions, + withDrawerActions, +)(BankRuleDeleteAlert); diff --git a/packages/webapp/src/containers/Banking/Rules/RulesList/hooks.ts b/packages/webapp/src/containers/Banking/Rules/RulesList/hooks.ts deleted file mode 100644 index 3ba4e2106..000000000 --- a/packages/webapp/src/containers/Banking/Rules/RulesList/hooks.ts +++ /dev/null @@ -1,3 +0,0 @@ -export const useBankRulesTableColumns = () => { - return []; -}; diff --git a/packages/webapp/src/containers/Banking/Rules/RulesList/hooks.tsx b/packages/webapp/src/containers/Banking/Rules/RulesList/hooks.tsx new file mode 100644 index 000000000..1e3f74d97 --- /dev/null +++ b/packages/webapp/src/containers/Banking/Rules/RulesList/hooks.tsx @@ -0,0 +1,36 @@ +// @ts-nocheck +import { useMemo } from 'react'; +import { Intent, Tag } from '@blueprintjs/core'; + +export const useBankRulesTableColumns = () => { + return useMemo( + () => [ + { + Header: 'Apply to', + accessor: (rule) => + rule.apply_if_transaction_type === 'deposit' ? ( + Deposits + ) : ( + Withdrawals + ), + }, + { + Header: 'Rule Name', + accessor: 'name', + }, + { + Header: 'Categorize As', + accessor: () => 'Expense', + }, + { + Header: 'Apply To', + accessor: () => All Accounts, + }, + { + Header: 'Conditions', + accessor: () => '', + }, + ], + [], + ); +}; diff --git a/packages/webapp/src/hooks/query/bank-rules.ts b/packages/webapp/src/hooks/query/bank-rules.ts index d9fa54d2d..d01d6c47d 100644 --- a/packages/webapp/src/hooks/query/bank-rules.ts +++ b/packages/webapp/src/hooks/query/bank-rules.ts @@ -33,12 +33,15 @@ export function useDeleteBankRule(props) { const queryClient = useQueryClient(); const apiRequest = useApiRequest(); - return useMutation((id: number) => apiRequest.delete(`/bank-rules/${id}`), { - onSuccess: (res, id) => { - // Invalidate queries. + return useMutation( + (id: number) => apiRequest.delete(`/banking/rules/${id}`), + { + onSuccess: (res, id) => { + // Invalidate queries. + }, + ...props, }, - ...props, - }); + ); } /**