From 18e4b06eed587f8e23c8135f32bc53803eef913b Mon Sep 17 00:00:00 2001 From: Tobias Genannt Date: Thu, 22 Aug 2024 18:50:54 +0200 Subject: [PATCH 1/2] Fix #209: Correctly display loaded expense - Don't load default split options after displaying an existing expense - Re-validate form after changing the "paidFor" selection. This fixes the error message "The expense must be paid for at least one participant." after clicking "Select None" and the selecting one participant. --- src/components/expense-form.tsx | 34 +++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/src/components/expense-form.tsx b/src/components/expense-form.tsx index 1a296dbf..3c6f8e5a 100644 --- a/src/components/expense-form.tsx +++ b/src/components/expense-form.tsx @@ -253,11 +253,6 @@ export function ExpenseForm({ useEffect(() => { setManuallyEditedParticipants(new Set()) - const newPaidFor = defaultSplittingOptions.paidFor.map((participant) => ({ - ...participant, - shares: String(participant.shares) as unknown as number, - })) - form.setValue('paidFor', newPaidFor, { shouldValidate: true }) }, [form.watch('splitMode'), form.watch('amount')]) useEffect(() => { @@ -569,18 +564,29 @@ export function ExpenseForm({ ({ participant }) => participant === id, )} onCheckedChange={(checked) => { - return checked - ? field.onChange([ - ...field.value, - { - participant: id, - shares: '1', - }, - ]) - : field.onChange( + const options = { + shouldDirty: true, + shouldTouch: true, + shouldValidate: true, + } + checked + ? form.setValue( + 'paidFor', + [ + ...field.value, + { + participant: id, + shares: '1' as unknown as number, + }, + ], + options, + ) + : form.setValue( + 'paidFor', field.value?.filter( (value) => value.participant !== id, ), + options, ) }} /> From 33c33f42fa1ececd34f3043ada6e81fa8d9e92b3 Mon Sep 17 00:00:00 2001 From: "partho.kunda" Date: Mon, 2 Sep 2024 23:25:16 +0600 Subject: [PATCH 2/2] Fix Paid For Field reset in Edit Expense Page for split Mode 'Unevenly - By amount' --- src/components/expense-form.tsx | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/components/expense-form.tsx b/src/components/expense-form.tsx index 3c6f8e5a..855041bd 100644 --- a/src/components/expense-form.tsx +++ b/src/components/expense-form.tsx @@ -256,20 +256,18 @@ export function ExpenseForm({ }, [form.watch('splitMode'), form.watch('amount')]) useEffect(() => { - const totalAmount = Number(form.getValues().amount) || 0 - const paidFor = form.getValues().paidFor const splitMode = form.getValues().splitMode - let newPaidFor = [...paidFor] - + // Only auto-balance for split mode 'Unevenly - By amount' if ( - splitMode === 'EVENLY' || - splitMode === 'BY_SHARES' || - splitMode === 'BY_PERCENTAGE' + splitMode === 'BY_AMOUNT' && + (form.getFieldState('paidFor').isDirty || + form.getFieldState('amount').isDirty) ) { - return - } else { - // Only auto-balance for split mode 'Unevenly - By amount' + const totalAmount = Number(form.getValues().amount) || 0 + const paidFor = form.getValues().paidFor + let newPaidFor = [...paidFor] + const editedParticipants = Array.from(manuallyEditedParticipants) let remainingAmount = totalAmount let remainingParticipants = newPaidFor.length - editedParticipants.length