Select all/no participant for expenses (Fix #3)

This commit is contained in:
Sebastien Castiel
2023-12-12 09:55:00 -05:00
parent 1ed533d01f
commit af3ed68f1c
3 changed files with 34 additions and 5 deletions

View File

@@ -9,10 +9,10 @@ Spliit is a free and open source alternative to Splitwise. I created it back in
- [x] Display group balances - [x] Display group balances
- [x] Create reimbursement expenses - [x] Create reimbursement expenses
- [x] Progressive Web App - [x] Progressive Web App
- [x] Select all/no participant for expenses
### Work in progress ### Work in progress
- [ ] Select all/no participant for expenses
- [ ] Tell the application who you are when opening a group - [ ] Tell the application who you are when opening a group
## Stack ## Stack

View File

@@ -1,6 +1,7 @@
'use client' 'use client'
import { AsyncButton } from '@/components/async-button' import { AsyncButton } from '@/components/async-button'
import { SubmitButton } from '@/components/submit-button' import { SubmitButton } from '@/components/submit-button'
import { Button } from '@/components/ui/button'
import { import {
Card, Card,
CardContent, CardContent,
@@ -138,7 +139,6 @@ export function ExpenseForm({ group, expense, onSubmit, onDelete }: Props) {
className="text-base max-w-[120px]" className="text-base max-w-[120px]"
type="number" type="number"
inputMode="decimal" inputMode="decimal"
min={0.01}
step={0.01} step={0.01}
placeholder="0.00" placeholder="0.00"
{...field} {...field}
@@ -174,7 +174,34 @@ export function ExpenseForm({ group, expense, onSubmit, onDelete }: Props) {
render={() => ( render={() => (
<FormItem className="order-5"> <FormItem className="order-5">
<div className="mb-4"> <div className="mb-4">
<FormLabel>Paid for</FormLabel> <FormLabel>
Paid for
<Button
variant="link"
type="button"
className="-m-2"
onClick={() => {
const paidFor = form.getValues().paidFor
const allSelected =
paidFor.length === group.participants.length
const newPairFor = allSelected
? []
: group.participants.map((p) => p.id)
form.setValue('paidFor', newPairFor, {
shouldDirty: true,
shouldTouch: true,
shouldValidate: true,
})
}}
>
{form.getValues().paidFor.length ===
group.participants.length ? (
<>Select none</>
) : (
<>Select all</>
)}
</Button>
</FormLabel>
<FormDescription> <FormDescription>
Select who the expense was paid for. Select who the expense was paid for.
</FormDescription> </FormDescription>
@@ -219,7 +246,9 @@ export function ExpenseForm({ group, expense, onSubmit, onDelete }: Props) {
</CardContent> </CardContent>
<CardFooter className="gap-2"> <CardFooter className="gap-2">
<SubmitButton loadingContent="Submitting…"> <SubmitButton
loadingContent={isCreate ? <>Creating</> : <>Saving</>}
>
{isCreate ? <>Create</> : <>Save</>} {isCreate ? <>Create</> : <>Save</>}
</SubmitButton> </SubmitButton>
{!isCreate && onDelete && ( {!isCreate && onDelete && (

View File

@@ -66,7 +66,7 @@ export const expenseFormSchema = z.object({
paidBy: z.string({ required_error: 'You must select a participant.' }), paidBy: z.string({ required_error: 'You must select a participant.' }),
paidFor: z paidFor: z
.array(z.string()) .array(z.string())
.min(1, 'The expense must be paid for at least 1 participant.'), .min(1, 'The expense must be paid for at least one participant.'),
isReimbursement: z.boolean(), isReimbursement: z.boolean(),
}) })