Config.ts import { Module, registerModule } from '@txp-core/runtime'; import {…
- User
Config.ts import { Module, registerModule } from '@txp-core/runtime'; import { outstandingTableSideEffectPattern } from './outstandingTableSideEffectPattern'; import { outstandingTransactionsHandler, updateLinks, amountUpdateHandler, creditsTransactionsHandler, creditsTransactionsHandlerAmount } from './outstandingTransactionsSaga'; import { TransactionTypes } from '@txp-core/transactions-utils'; import { CHARGES_TABLE_DATA_UPDATE, CREDITS_TABLE_DATA_UPDATE, CHARGES_TABLE_DATA_FETCHED, CREDITS_TABLE_WHOLE_UPDATE, CHARGES_TABLE_WHOLE_UPDATE } from '@txp-core/payment-transactions-table'; export const outstandingTransactionsModule: Module = registerModule({ id: 'outstandingTransactions', sideEffects: [ { id: 'outstandingTransactions', description: 'update amount when payment amount get updated', // pattern: outstandingTableSideEffectPattern, pattern: CHARGES_TABLE_DATA_UPDATE, handler: outstandingTransactionsHandler }, { id: 'amountUpdate1', description: 'update amount when payment amount get updated', // pattern: outstandingTableSideEffectPattern, pattern: CHARGES_TABLE_DATA_FETCHED, handler: amountUpdateHandler }, { id: 'amountUpdate', description: 'update amount when payment amount get updated', // pattern: outstandingTableSideEffectPattern, pattern: CREDITS_TABLE_DATA_UPDATE, handler: amountUpdateHandler }, //commented // { // id: 'creditTableUpdate', // description: 'update amount when payment amount get updated', // // pattern: outstandingTableSideEffectPattern, // pattern: CREDITS_TABLE_DATA_UPDATE, // handler: creditsTransactionsHandler // }, //commented //lets try { id: 'credit update on amountUpdate', description: 'update credit when amount entered manullay', // pattern: outstandingTableSideEffectPattern, pattern: CHARGES_TABLE_WHOLE_UPDATE, handler: creditsTransactionsHandlerAmount } ], // preSave: [ // { // id: 'updatelinks', // description: 'update links based on checked table rows of charges and credit table', // handler: updateLinks, // enabled: { // 'environment.txnType': [TransactionTypes.PURCHASE_BILL_PAYMENT] // } // } // ], withModuleDependencies: ['coreTransactionState', 'coreUIState'] });
AmountField import React from 'react'; import { useFormatMessage } from '@txp-core/intl'; import AmountField, { amountFieldValidation } from '@txp-core/amount-field'; import styles from 'src/js/billpayment/modules/AmountField/AmountField.module.css'; import { enabled } from '@txp-core/enabled'; import { TransactionTypes } from '@txp-core/transactions-utils'; import { useTXPStateRead, useTXPStateWrite } from '@txp-core/runtime'; import { TransactionActions } from '@txp-core/transactions-core'; import { PaymentChargesTableAction, paymentTableSelectors } from '@txp-core/payment-transactions-table'; import { cloneDeep } from '@txp-core/basic-utils'; import { CreditChargesPaymentTypes } from '@txp-core/payment-transactions-table/dist/paymentTableStateTypes'; const AMOUNT_MAX_VALUE = 99999999999.99; const Amount = () => { const formatMessage = useFormatMessage(); const onChange = useTXPStateWrite(TransactionActions.genericTxnUpdate); const error = useTXPStateWrite(amountFieldValidation.errorAdded); const chargesTable = useTXPStateRead(paymentTableSelectors.getCharges); const creditTableLines = useTXPStateRead(paymentTableSelectors.getCredits); const customerBalanceAmount = useTXPStateRead(paymentTableSelectors.getCustomerBalanceAmount); const updateTablesData = useTXPStateWrite(PaymentChargesTableAction.chargesTableWholeUpdate); const amountLabel = formatMessage({ id: 'billpayment_amount', defaultMessage: 'Amount' }); const handleChange = (moneyValue: string | null) => { const amountValue = Number(moneyValue); if (moneyValue && amountValue > AMOUNT_MAX_VALUE) { error({ errorText: { id: 'amountReceivedField_range_limit_error', defaultMessage: 'This value is out of range' } }); } onChange({ amount: moneyValue || '0.00' }); // so here we have to update the chargesTable, based on amount value,means we have // charge table having data one field is openBalance and based on amount we need to update its // respective linkedPaymentAmount with openbalance, before updated charge table // we need to check first if we can adjust amount in openbalance with exact match, // otherwiser try to adjust from top const updateChargeTable: CreditChargesPaymentTypes[] = cloneDeep(chargesTable) || []; let balanceAmount = Number(moneyValue); const extactMatch = updateChargeTable.filter((charge) => { return Number(charge.openBalance) === balanceAmount; }); if (extactMatch.length !== 0) { updateChargeTable.map((item) => { if (item.txnId === extactMatch[0].txnId) { item.linkedPaymentAmount = item.openBalance.toString(); } else { item.linkedPaymentAmount = ''; } return item; }); } else { updateChargeTable.map((item) => { if (balanceAmount > 0) { const openBalance = Number(item.openBalance); if (balanceAmount > openBalance) { item.linkedPaymentAmount = item.openBalance.toString(); balanceAmount -= openBalance; } else { item.linkedPaymentAmount = balanceAmount.toFixed(2).toString(); balanceAmount = 0; } } else { item.linkedPaymentAmount = ''; } return item; }); } const result: any = { charges: updateChargeTable, credits: creditTableLines, customerBalanceAmount }; updateTablesData(result); }; return ( <AmountField idsProps={{ label: amountLabel }} enableValidation updateTxnSlice={handleChange} /> ); }; export default enabled( { 'environment.txnType': [TransactionTypes.PURCHASE_BILL_PAYMENT] }, Amount );
- Assistant
Here's an approach you can take based on your requirements:
-
Add a flag
isAmountEnteredManually: booleanto your state to keep track of whether the user has manually entered an amount or not. Set it tofalseby default. -
When the user manually updates the amount field, also update the
isAmountEnteredManuallyflag to .
-