mirror of
https://github.com/spliit-app/spliit.git
synced 2026-02-18 13:36:12 +01:00
Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2fd38aadd9 | ||
|
|
b61d1836ea | ||
|
|
c3903849ec | ||
|
|
b67a0be0dd | ||
|
|
e07d237218 | ||
|
|
cc37083389 |
@@ -0,0 +1,2 @@
|
|||||||
|
-- AlterTable
|
||||||
|
ALTER TABLE "Expense" ADD COLUMN "notes" TEXT;
|
||||||
@@ -52,6 +52,7 @@ model Expense {
|
|||||||
splitMode SplitMode @default(EVENLY)
|
splitMode SplitMode @default(EVENLY)
|
||||||
createdAt DateTime @default(now())
|
createdAt DateTime @default(now())
|
||||||
documents ExpenseDocument[]
|
documents ExpenseDocument[]
|
||||||
|
notes String?
|
||||||
}
|
}
|
||||||
|
|
||||||
model ExpenseDocument {
|
model ExpenseDocument {
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ type Props = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const EXPENSE_GROUPS = {
|
const EXPENSE_GROUPS = {
|
||||||
|
UPCOMING: 'Upcoming',
|
||||||
THIS_WEEK: 'This week',
|
THIS_WEEK: 'This week',
|
||||||
EARLIER_THIS_MONTH: 'Earlier this month',
|
EARLIER_THIS_MONTH: 'Earlier this month',
|
||||||
LAST_MONTH: 'Last month',
|
LAST_MONTH: 'Last month',
|
||||||
@@ -28,7 +29,9 @@ const EXPENSE_GROUPS = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function getExpenseGroup(date: Dayjs, today: Dayjs) {
|
function getExpenseGroup(date: Dayjs, today: Dayjs) {
|
||||||
if (today.isSame(date, 'week')) {
|
if (today.isBefore(date)) {
|
||||||
|
return EXPENSE_GROUPS.UPCOMING
|
||||||
|
} else if (today.isSame(date, 'week')) {
|
||||||
return EXPENSE_GROUPS.THIS_WEEK
|
return EXPENSE_GROUPS.THIS_WEEK
|
||||||
} else if (today.isSame(date, 'month')) {
|
} else if (today.isSame(date, 'month')) {
|
||||||
return EXPENSE_GROUPS.EARLIER_THIS_MONTH
|
return EXPENSE_GROUPS.EARLIER_THIS_MONTH
|
||||||
|
|||||||
@@ -51,6 +51,7 @@ export default async function GroupExpensesPage({
|
|||||||
prefetch={false}
|
prefetch={false}
|
||||||
href={`/groups/${groupId}/expenses/export/json`}
|
href={`/groups/${groupId}/expenses/export/json`}
|
||||||
target="_blank"
|
target="_blank"
|
||||||
|
title="Export to JSON"
|
||||||
>
|
>
|
||||||
<Download className="w-4 h-4" />
|
<Download className="w-4 h-4" />
|
||||||
</Link>
|
</Link>
|
||||||
@@ -63,7 +64,10 @@ export default async function GroupExpensesPage({
|
|||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
<Button asChild size="icon">
|
<Button asChild size="icon">
|
||||||
<Link href={`/groups/${groupId}/expenses/create`}>
|
<Link
|
||||||
|
href={`/groups/${groupId}/expenses/create`}
|
||||||
|
title="Create expense"
|
||||||
|
>
|
||||||
<Plus className="w-4 h-4" />
|
<Plus className="w-4 h-4" />
|
||||||
</Link>
|
</Link>
|
||||||
</Button>
|
</Button>
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ export function ShareButton({ group }: Props) {
|
|||||||
return (
|
return (
|
||||||
<Popover>
|
<Popover>
|
||||||
<PopoverTrigger asChild>
|
<PopoverTrigger asChild>
|
||||||
<Button size="icon">
|
<Button title="Share" size="icon">
|
||||||
<Share className="w-4 h-4" />
|
<Share className="w-4 h-4" />
|
||||||
</Button>
|
</Button>
|
||||||
</PopoverTrigger>
|
</PopoverTrigger>
|
||||||
|
|||||||
47
src/components/delete-popup.tsx
Normal file
47
src/components/delete-popup.tsx
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
'use client'
|
||||||
|
|
||||||
|
import { Trash2 } from 'lucide-react'
|
||||||
|
import { AsyncButton } from './async-button'
|
||||||
|
import { Button } from './ui/button'
|
||||||
|
import {
|
||||||
|
Dialog,
|
||||||
|
DialogClose,
|
||||||
|
DialogContent,
|
||||||
|
DialogDescription,
|
||||||
|
DialogFooter,
|
||||||
|
DialogTitle,
|
||||||
|
DialogTrigger,
|
||||||
|
} from './ui/dialog'
|
||||||
|
|
||||||
|
export function DeletePopup({ onDelete }: { onDelete: () => Promise<void> }) {
|
||||||
|
return (
|
||||||
|
<Dialog>
|
||||||
|
<DialogTrigger asChild>
|
||||||
|
<Button variant="destructive">
|
||||||
|
<Trash2 className="w-4 h-4 mr-2" />
|
||||||
|
Delete
|
||||||
|
</Button>
|
||||||
|
</DialogTrigger>
|
||||||
|
<DialogContent>
|
||||||
|
<DialogTitle>Delete this expense?</DialogTitle>
|
||||||
|
<DialogDescription>
|
||||||
|
Do you really want to delete this expense? This action is
|
||||||
|
irreversible.
|
||||||
|
</DialogDescription>
|
||||||
|
<DialogFooter className="flex flex-col gap-2">
|
||||||
|
<AsyncButton
|
||||||
|
type="button"
|
||||||
|
variant="destructive"
|
||||||
|
loadingContent="Deleting…"
|
||||||
|
action={onDelete}
|
||||||
|
>
|
||||||
|
Yes
|
||||||
|
</AsyncButton>
|
||||||
|
<DialogClose asChild>
|
||||||
|
<Button variant={'secondary'}>Cancel</Button>
|
||||||
|
</DialogClose>
|
||||||
|
</DialogFooter>
|
||||||
|
</DialogContent>
|
||||||
|
</Dialog>
|
||||||
|
)
|
||||||
|
}
|
||||||
@@ -1,5 +1,4 @@
|
|||||||
'use client'
|
'use client'
|
||||||
import { AsyncButton } from '@/components/async-button'
|
|
||||||
import { CategorySelector } from '@/components/category-selector'
|
import { CategorySelector } from '@/components/category-selector'
|
||||||
import { ExpenseDocumentsInput } from '@/components/expense-documents-input'
|
import { ExpenseDocumentsInput } from '@/components/expense-documents-input'
|
||||||
import { SubmitButton } from '@/components/submit-button'
|
import { SubmitButton } from '@/components/submit-button'
|
||||||
@@ -36,16 +35,22 @@ import {
|
|||||||
} from '@/components/ui/select'
|
} from '@/components/ui/select'
|
||||||
import { getCategories, getExpense, getGroup, randomId } from '@/lib/api'
|
import { getCategories, getExpense, getGroup, randomId } from '@/lib/api'
|
||||||
import { RuntimeFeatureFlags } from '@/lib/featureFlags'
|
import { RuntimeFeatureFlags } from '@/lib/featureFlags'
|
||||||
import { ExpenseFormValues, expenseFormSchema } from '@/lib/schemas'
|
import {
|
||||||
|
ExpenseFormValues,
|
||||||
|
SplittingOptions,
|
||||||
|
expenseFormSchema,
|
||||||
|
} from '@/lib/schemas'
|
||||||
import { cn } from '@/lib/utils'
|
import { cn } from '@/lib/utils'
|
||||||
import { zodResolver } from '@hookform/resolvers/zod'
|
import { zodResolver } from '@hookform/resolvers/zod'
|
||||||
import { Save, Trash2 } from 'lucide-react'
|
import { Save } from 'lucide-react'
|
||||||
import Link from 'next/link'
|
import Link from 'next/link'
|
||||||
import { useSearchParams } from 'next/navigation'
|
import { useSearchParams } from 'next/navigation'
|
||||||
import { useState } from 'react'
|
import { useState } from 'react'
|
||||||
import { useForm } from 'react-hook-form'
|
import { useForm } from 'react-hook-form'
|
||||||
import { match } from 'ts-pattern'
|
import { match } from 'ts-pattern'
|
||||||
|
import { DeletePopup } from './delete-popup'
|
||||||
import { extractCategoryFromTitle } from './expense-form-actions'
|
import { extractCategoryFromTitle } from './expense-form-actions'
|
||||||
|
import { Textarea } from './ui/textarea'
|
||||||
|
|
||||||
export type Props = {
|
export type Props = {
|
||||||
group: NonNullable<Awaited<ReturnType<typeof getGroup>>>
|
group: NonNullable<Awaited<ReturnType<typeof getGroup>>>
|
||||||
@@ -67,6 +72,78 @@ const enforceCurrencyPattern = (value: string) =>
|
|||||||
// remove all non-numeric and non-dot characters
|
// remove all non-numeric and non-dot characters
|
||||||
.replace(/[^\d.]/g, '')
|
.replace(/[^\d.]/g, '')
|
||||||
|
|
||||||
|
const getDefaultSplittingOptions = (group: Props['group']) => {
|
||||||
|
const defaultValue = {
|
||||||
|
splitMode: 'EVENLY' as const,
|
||||||
|
paidFor: group.participants.map(({ id }) => ({
|
||||||
|
participant: id,
|
||||||
|
shares: '1' as unknown as number,
|
||||||
|
})),
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof localStorage === 'undefined') return defaultValue
|
||||||
|
const defaultSplitMode = localStorage.getItem(
|
||||||
|
`${group.id}-defaultSplittingOptions`,
|
||||||
|
)
|
||||||
|
if (defaultSplitMode === null) return defaultValue
|
||||||
|
const parsedDefaultSplitMode = JSON.parse(
|
||||||
|
defaultSplitMode,
|
||||||
|
) as SplittingOptions
|
||||||
|
|
||||||
|
if (parsedDefaultSplitMode.paidFor === null) {
|
||||||
|
parsedDefaultSplitMode.paidFor = defaultValue.paidFor
|
||||||
|
}
|
||||||
|
|
||||||
|
// if there is a participant in the default options that does not exist anymore,
|
||||||
|
// remove the stale default splitting options
|
||||||
|
for (const parsedPaidFor of parsedDefaultSplitMode.paidFor) {
|
||||||
|
if (
|
||||||
|
!group.participants.some(({ id }) => id === parsedPaidFor.participant)
|
||||||
|
) {
|
||||||
|
localStorage.removeItem(`${group.id}-defaultSplittingOptions`)
|
||||||
|
return defaultValue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
splitMode: parsedDefaultSplitMode.splitMode,
|
||||||
|
paidFor: parsedDefaultSplitMode.paidFor.map((paidFor) => ({
|
||||||
|
participant: paidFor.participant,
|
||||||
|
shares: String(paidFor.shares / 100) as unknown as number,
|
||||||
|
})),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function persistDefaultSplittingOptions(
|
||||||
|
groupId: string,
|
||||||
|
expenseFormValues: ExpenseFormValues,
|
||||||
|
) {
|
||||||
|
if (localStorage && expenseFormValues.saveDefaultSplittingOptions) {
|
||||||
|
const computePaidFor = (): SplittingOptions['paidFor'] => {
|
||||||
|
if (expenseFormValues.splitMode === 'EVENLY') {
|
||||||
|
return expenseFormValues.paidFor.map(({ participant }) => ({
|
||||||
|
participant,
|
||||||
|
shares: '100' as unknown as number,
|
||||||
|
}))
|
||||||
|
} else if (expenseFormValues.splitMode === 'BY_AMOUNT') {
|
||||||
|
return null
|
||||||
|
} else {
|
||||||
|
return expenseFormValues.paidFor
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const splittingOptions = {
|
||||||
|
splitMode: expenseFormValues.splitMode,
|
||||||
|
paidFor: computePaidFor(),
|
||||||
|
} satisfies SplittingOptions
|
||||||
|
|
||||||
|
localStorage.setItem(
|
||||||
|
`${groupId}-defaultSplittingOptions`,
|
||||||
|
JSON.stringify(splittingOptions),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export function ExpenseForm({
|
export function ExpenseForm({
|
||||||
group,
|
group,
|
||||||
expense,
|
expense,
|
||||||
@@ -80,12 +157,13 @@ export function ExpenseForm({
|
|||||||
const getSelectedPayer = (field?: { value: string }) => {
|
const getSelectedPayer = (field?: { value: string }) => {
|
||||||
if (isCreate && typeof window !== 'undefined') {
|
if (isCreate && typeof window !== 'undefined') {
|
||||||
const activeUser = localStorage.getItem(`${group.id}-activeUser`)
|
const activeUser = localStorage.getItem(`${group.id}-activeUser`)
|
||||||
if (activeUser && activeUser !== 'None') {
|
if (activeUser && activeUser !== 'None' && field?.value === undefined) {
|
||||||
return activeUser
|
return activeUser
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return field?.value
|
return field?.value
|
||||||
}
|
}
|
||||||
|
const defaultSplittingOptions = getDefaultSplittingOptions(group)
|
||||||
const form = useForm<ExpenseFormValues>({
|
const form = useForm<ExpenseFormValues>({
|
||||||
resolver: zodResolver(expenseFormSchema),
|
resolver: zodResolver(expenseFormSchema),
|
||||||
defaultValues: expense
|
defaultValues: expense
|
||||||
@@ -100,8 +178,10 @@ export function ExpenseForm({
|
|||||||
shares: String(shares / 100) as unknown as number,
|
shares: String(shares / 100) as unknown as number,
|
||||||
})),
|
})),
|
||||||
splitMode: expense.splitMode,
|
splitMode: expense.splitMode,
|
||||||
|
saveDefaultSplittingOptions: false,
|
||||||
isReimbursement: expense.isReimbursement,
|
isReimbursement: expense.isReimbursement,
|
||||||
documents: expense.documents,
|
documents: expense.documents,
|
||||||
|
notes: expense.notes ?? '',
|
||||||
}
|
}
|
||||||
: searchParams.get('reimbursement')
|
: searchParams.get('reimbursement')
|
||||||
? {
|
? {
|
||||||
@@ -121,8 +201,10 @@ export function ExpenseForm({
|
|||||||
: undefined,
|
: undefined,
|
||||||
],
|
],
|
||||||
isReimbursement: true,
|
isReimbursement: true,
|
||||||
splitMode: 'EVENLY',
|
splitMode: defaultSplittingOptions.splitMode,
|
||||||
|
saveDefaultSplittingOptions: false,
|
||||||
documents: [],
|
documents: [],
|
||||||
|
notes: '',
|
||||||
}
|
}
|
||||||
: {
|
: {
|
||||||
title: searchParams.get('title') ?? '',
|
title: searchParams.get('title') ?? '',
|
||||||
@@ -134,13 +216,11 @@ export function ExpenseForm({
|
|||||||
? Number(searchParams.get('categoryId'))
|
? Number(searchParams.get('categoryId'))
|
||||||
: 0, // category with Id 0 is General
|
: 0, // category with Id 0 is General
|
||||||
// paid for all, split evenly
|
// paid for all, split evenly
|
||||||
paidFor: group.participants.map(({ id }) => ({
|
paidFor: defaultSplittingOptions.paidFor,
|
||||||
participant: id,
|
|
||||||
shares: '1' as unknown as number,
|
|
||||||
})),
|
|
||||||
paidBy: getSelectedPayer(),
|
paidBy: getSelectedPayer(),
|
||||||
isReimbursement: false,
|
isReimbursement: false,
|
||||||
splitMode: 'EVENLY',
|
splitMode: defaultSplittingOptions.splitMode,
|
||||||
|
saveDefaultSplittingOptions: false,
|
||||||
documents: searchParams.get('imageUrl')
|
documents: searchParams.get('imageUrl')
|
||||||
? [
|
? [
|
||||||
{
|
{
|
||||||
@@ -151,13 +231,19 @@ export function ExpenseForm({
|
|||||||
},
|
},
|
||||||
]
|
]
|
||||||
: [],
|
: [],
|
||||||
|
notes: '',
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
const [isCategoryLoading, setCategoryLoading] = useState(false)
|
const [isCategoryLoading, setCategoryLoading] = useState(false)
|
||||||
|
|
||||||
|
const submit = async (values: ExpenseFormValues) => {
|
||||||
|
await persistDefaultSplittingOptions(group.id, values)
|
||||||
|
return onSubmit(values)
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Form {...form}>
|
<Form {...form}>
|
||||||
<form onSubmit={form.handleSubmit((values) => onSubmit(values))}>
|
<form onSubmit={form.handleSubmit(submit)}>
|
||||||
<Card>
|
<Card>
|
||||||
<CardHeader>
|
<CardHeader>
|
||||||
<CardTitle>
|
<CardTitle>
|
||||||
@@ -318,6 +404,18 @@ export function ExpenseForm({
|
|||||||
</FormItem>
|
</FormItem>
|
||||||
)}
|
)}
|
||||||
/>
|
/>
|
||||||
|
<FormField
|
||||||
|
control={form.control}
|
||||||
|
name="notes"
|
||||||
|
render={({ field }) => (
|
||||||
|
<FormItem className="sm:order-6">
|
||||||
|
<FormLabel>Notes</FormLabel>
|
||||||
|
<FormControl>
|
||||||
|
<Textarea className="text-base" {...field} />
|
||||||
|
</FormControl>
|
||||||
|
</FormItem>
|
||||||
|
)}
|
||||||
|
/>
|
||||||
</CardContent>
|
</CardContent>
|
||||||
</Card>
|
</Card>
|
||||||
|
|
||||||
@@ -511,7 +609,10 @@ export function ExpenseForm({
|
|||||||
)}
|
)}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<Collapsible className="mt-5">
|
<Collapsible
|
||||||
|
className="mt-5"
|
||||||
|
defaultOpen={form.getValues().splitMode !== 'EVENLY'}
|
||||||
|
>
|
||||||
<CollapsibleTrigger asChild>
|
<CollapsibleTrigger asChild>
|
||||||
<Button variant="link" className="-mx-4">
|
<Button variant="link" className="-mx-4">
|
||||||
Advanced splitting options…
|
Advanced splitting options…
|
||||||
@@ -523,7 +624,7 @@ export function ExpenseForm({
|
|||||||
control={form.control}
|
control={form.control}
|
||||||
name="splitMode"
|
name="splitMode"
|
||||||
render={({ field }) => (
|
render={({ field }) => (
|
||||||
<FormItem className="sm:order-2">
|
<FormItem>
|
||||||
<FormLabel>Split mode</FormLabel>
|
<FormLabel>Split mode</FormLabel>
|
||||||
<FormControl>
|
<FormControl>
|
||||||
<Select
|
<Select
|
||||||
@@ -559,6 +660,25 @@ export function ExpenseForm({
|
|||||||
</FormItem>
|
</FormItem>
|
||||||
)}
|
)}
|
||||||
/>
|
/>
|
||||||
|
<FormField
|
||||||
|
control={form.control}
|
||||||
|
name="saveDefaultSplittingOptions"
|
||||||
|
render={({ field }) => (
|
||||||
|
<FormItem className="flex flex-row gap-2 items-center space-y-0 pt-2">
|
||||||
|
<FormControl>
|
||||||
|
<Checkbox
|
||||||
|
checked={field.value}
|
||||||
|
onCheckedChange={field.onChange}
|
||||||
|
/>
|
||||||
|
</FormControl>
|
||||||
|
<div>
|
||||||
|
<FormLabel>
|
||||||
|
Save as default splitting options
|
||||||
|
</FormLabel>
|
||||||
|
</div>
|
||||||
|
</FormItem>
|
||||||
|
)}
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
</CollapsibleContent>
|
</CollapsibleContent>
|
||||||
</Collapsible>
|
</Collapsible>
|
||||||
@@ -598,15 +718,7 @@ export function ExpenseForm({
|
|||||||
{isCreate ? <>Create</> : <>Save</>}
|
{isCreate ? <>Create</> : <>Save</>}
|
||||||
</SubmitButton>
|
</SubmitButton>
|
||||||
{!isCreate && onDelete && (
|
{!isCreate && onDelete && (
|
||||||
<AsyncButton
|
<DeletePopup onDelete={onDelete}></DeletePopup>
|
||||||
type="button"
|
|
||||||
variant="destructive"
|
|
||||||
loadingContent="Deleting…"
|
|
||||||
action={onDelete}
|
|
||||||
>
|
|
||||||
<Trash2 className="w-4 h-4 mr-2" />
|
|
||||||
Delete
|
|
||||||
</AsyncButton>
|
|
||||||
)}
|
)}
|
||||||
<Button variant="ghost" asChild>
|
<Button variant="ghost" asChild>
|
||||||
<Link href={`/groups/${group.id}`}>Cancel</Link>
|
<Link href={`/groups/${group.id}`}>Cancel</Link>
|
||||||
|
|||||||
@@ -72,6 +72,7 @@ export async function createExpense(
|
|||||||
})),
|
})),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
notes: expenseFormValues.notes,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -185,6 +186,7 @@ export async function updateExpense(
|
|||||||
id: doc.id,
|
id: doc.id,
|
||||||
})),
|
})),
|
||||||
},
|
},
|
||||||
|
notes: expenseFormValues.notes,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -105,6 +105,7 @@ export const expenseFormSchema = z
|
|||||||
Object.values(SplitMode) as any,
|
Object.values(SplitMode) as any,
|
||||||
)
|
)
|
||||||
.default('EVENLY'),
|
.default('EVENLY'),
|
||||||
|
saveDefaultSplittingOptions: z.boolean(),
|
||||||
isReimbursement: z.boolean(),
|
isReimbursement: z.boolean(),
|
||||||
documents: z
|
documents: z
|
||||||
.array(
|
.array(
|
||||||
@@ -116,6 +117,7 @@ export const expenseFormSchema = z
|
|||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
.default([]),
|
.default([]),
|
||||||
|
notes: z.string().optional(),
|
||||||
})
|
})
|
||||||
.superRefine((expense, ctx) => {
|
.superRefine((expense, ctx) => {
|
||||||
let sum = 0
|
let sum = 0
|
||||||
@@ -160,3 +162,9 @@ export const expenseFormSchema = z
|
|||||||
})
|
})
|
||||||
|
|
||||||
export type ExpenseFormValues = z.infer<typeof expenseFormSchema>
|
export type ExpenseFormValues = z.infer<typeof expenseFormSchema>
|
||||||
|
|
||||||
|
export type SplittingOptions = {
|
||||||
|
// Used for saving default splitting options in localStorage
|
||||||
|
splitMode: SplitMode
|
||||||
|
paidFor: ExpenseFormValues['paidFor'] | null
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user