// The reason this runs standalone is because it is called from Editor.jsx, but the location is inside a Form, so it has to run independently. import { useEffect, useState } from 'react'; import { useOutletContext } from 'react-router-dom'; import { useForm } from 'react-hook-form'; // components import DateInput from '../../common/formElements/DateInput'; import Selector from '../../common/formElements/Selector'; import TextInput from '../../common/formElements/TextInput'; import Warning from '../../common/display/Warning'; // hooks import useDeleteLogic from '../../hooks/utilities/useDeleteLogic'; // functions import { validateUUID } from '../../../services/utils'; // constants const nowRunning = 'accounting/transactions/Lines.jsx'; const errorNumber = 581; function EditLine(props) { const { accountNames, credits, debit, debits, line, onDelete, setUpdatedCredits, setUpdatedDebits, theLine } = props; const { control, getValues, register, setValue } = useForm(); const { handleError } = useOutletContext(); const [loaded, setLoaded] = useState(); const { DeleteConfirmationModal, DeleteIcon } = useDeleteLogic(); const key = line - 1; const onChange = async () => { const context = `${nowRunning}.onChange`; try { if (!debit) { const updatedCredits = [...credits]; updatedCredits[key] = getValues(); setUpdatedCredits(updatedCredits); } else { const updatedDebits = [...debits]; updatedDebits[key] = getValues(); setUpdatedDebits(updatedDebits); } } catch (e) { handleError(e, context, errorNumber); } }; useEffect(() => { const context = `${nowRunning}.useEffect`; const runThis = async () => { if (accountNames) { const { account, amount, pointer, statement } = theLine; setValue('account', accountNames[account] ? account : null); setValue('amount', amount); setValue('pointer', pointer); setValue('statement', statement); setLoaded(true); } }; runThis() .catch(e => handleError(e, context, errorNumber)); }, [accountNames, handleError, setValue, theLine]); try { if (!loaded || !accountNames) return null; let lineType = debit ? 'debit' : 'credit'; const line = +key + 1; const deleteMessage = 'Are you sure you want to delete this ' + lineType + ' line?'; const validateAccount = validateUUID(getValues('account')); return ( <>
{lineType} line {line}
{!validateAccount && ()}
); } catch (e) { handleError(e, nowRunning, errorNumber); return null; } } export default EditLine;