// 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 { Controller, useForm } from 'react-hook-form'; // components import DatePicker from 'react-datepicker'; import 'react-datepicker/dist/react-datepicker.css'; import Selector from '../../common/formElements/Selector'; import TextInput from '../../common/formElements/TextInput'; // hooks import useDeleteLogic from '../../hooks/useDeleteLogic'; // functions import { convertToServerTimezone } from '../../../services/utils'; // constants const nowRunning = 'accounting/transactions/Lines.jsx'; const errorNumber = 581; function EditLine(props) { const { accountNames, credits, debit, debits, line, onDelete, setTrigger, setUpdatedCredits, setUpdatedDebits, theLine, trigger } = 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) { credits[key] = getValues(); setUpdatedCredits(credits); } else { debits[key] = getValues(); setUpdatedDebits(debits); } setTrigger(trigger + 1); // This is used to force an immediate new rendering on Editor (see useEffect). } catch (e) { handleError(e, context, errorNumber); } }; useEffect(() => { const context = `${nowRunning}.useEffect`; const runThis = async () => { if (accountNames) { const { account, amount, pointer, statement } = theLine; setValue('account', account); setValue('amount', amount); setValue('pointer', pointer); setValue('statement', statement); setLoaded(true); } }; runThis() .catch(e => handleError(e, context, errorNumber)); }, [accountNames, handleError, setValue, theLine, trigger]); 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?'; return ( <>