// #region [Imports] =================================================================================================== // Libraries import { useEffect, useState, useRef } from 'react'; import { connect } from 'react-redux'; import { bindActionCreators } from 'redux'; import { Select, Input, Button, Space, Checkbox, message } from 'antd'; // Actions import { StoreCreditsCustomersActions } from '../../store/actions/storeCreditsCustomers'; // Types import { IStoreCreditCustomer } from '../../types/storeCredits'; // Helpers import { priceFormat, validatePrice, parsePrice } from '../../helpers/currency'; import { sanitizeHtml } from '../../../shared/helpers/sanitize'; // #endregion [Imports] // #region [Variables] ================================================================================================= declare var acfwAdminApp: any; const { adjustCustomerStoreCredits, setStoreCreditsCustomerBalance } = StoreCreditsCustomersActions; // #endregion [Variables] // #region [Interfaces] ================================================================================================ interface IActions { adjustCustomerStoreCredits: typeof adjustCustomerStoreCredits; setStoreCreditsCustomerBalance: typeof setStoreCreditsCustomerBalance; } interface IProps { customer: IStoreCreditCustomer | null; setAdjustCustomer: (customer: IStoreCreditCustomer | null) => void; actions: IActions; } // #endregion [Interfaces] // #region [Component] ================================================================================================= const AdjustCustomerBalance = (props: IProps) => { const { customer, setAdjustCustomer, actions } = props; const { adjust_modal, currency } = acfwAdminApp.store_credits_page; const [loading, setLoading] = useState(false); const [type, setType] = useState('increase'); const [amount, setAmount] = useState(''); const [newBalance, setNewBalance] = useState(''); const [entryNote, setEntryNote] = useState(''); const [noteVisible, setNoteVisible] = useState(false); const noteTextArea = useRef(null); const [sendEmailNotification, setSendEmailNotification] = useState(true); /** * Show or hide the note input. */ const handleAddNote = () => { setNoteVisible(!noteVisible); // Toggle visibility // Debounce event, to focus on the input after it's visible setTimeout(() => noteTextArea.current?.focus(), 400); }; /** * Handle the adjustment of the customer's store credits. */ const handleAdjustment = () => { if (!customer) return; if (!validatePrice(amount)) { message.error(adjust_modal.invalid_price); return; } setLoading(true); actions.adjustCustomerStoreCredits({ id: customer?.id, type, amount: parsePrice(amount), note: noteVisible && entryNote ? entryNote : '', send_email_notification: sendEmailNotification, successCB: (response: any) => { setLoading(false); message.success(response.data.message); actions.setStoreCreditsCustomerBalance({ id: customer?.id, balance: response.data.balance, balance_raw: response.data.balance_raw, }); setAdjustCustomer(null); setAmount(''); setType('increase'); setEntryNote(''); }, failCB: ({ error }: any) => { setLoading(false); message.error(error.response.data.message); }, }); }; // calculate new balance when either customer or amount values are changed. useEffect(() => { if (!customer || !amount) { setNewBalance(''); return; } const newBalance = 'increase' === type ? customer.balance_raw + parsePrice(amount) : customer.balance_raw - parsePrice(amount); setNewBalance(priceFormat(Math.max(0, newBalance))); }, [customer, amount, type]); if (!customer) return null; return (

{adjust_modal.title}

{adjust_modal.description}

{adjust_modal.current_balance.replace('{balance}', customer.balance)}

setAmount(e.target.value)} addonBefore={currency.symbol} allowClear /> {adjust_modal.add_note} {noteVisible ? ( setEntryNote(e.target.value)} maxLength={255} /> ) : null}
{newBalance ? (

${newBalance}`)), }} /> ) : null}
setSendEmailNotification(e.target.checked)}> {adjust_modal.send_email_notification}

); }; const mapDispatchToProps = (dispatch: any) => ({ actions: bindActionCreators({ adjustCustomerStoreCredits, setStoreCreditsCustomerBalance }, dispatch), }); export default connect(null, mapDispatchToProps)(AdjustCustomerBalance); // #endregion [Component]