Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Correctly display loaded expense #210

Merged
merged 2 commits into from
Sep 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 28 additions & 24 deletions src/components/expense-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -253,28 +253,21 @@ 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(() => {
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
Expand Down Expand Up @@ -569,18 +562,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,
)
}}
/>
Expand Down
Loading