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

Use tRPC for group create form #250

Merged
merged 1 commit into from
Oct 20, 2024
Merged
Show file tree
Hide file tree
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
9 changes: 1 addition & 8 deletions src/app/groups/[groupId]/edit/edit-group.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,9 @@ import { trpc } from '@/trpc/client'

export const EditGroup = ({ groupId }: { groupId: string }) => {
const { data, isLoading } = trpc.groups.get.useQuery({ groupId })
const { mutateAsync, isPending } = trpc.groups.update.useMutation()
const { mutateAsync } = trpc.groups.update.useMutation()
const utils = trpc.useUtils()

// async function updateGroupAction(values: unknown, participantId?: string) {
// 'use server'
// const groupFormValues = groupFormSchema.parse(values)
// const group = await updateGroup(groupId, groupFormValues, participantId)
// redirect(`/groups/${group.id}`)
// }

if (isLoading) return <></>

return (
Expand Down
6 changes: 3 additions & 3 deletions src/app/groups/[groupId]/information/group-information.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ export default function GroupInformation({ groupId }: { groupId: string }) {
<Skeleton className="h-3 w-3/4" />
<Skeleton className="h-3 w-1/2" />
</div>
) : data.group.information ? (
<p className="text-foreground">{data.group.information}</p>
) : (
data.group.information || (
<p className="text-muted-foreground text-sm">{t('empty')}</p>
)
<p className="text-muted-foreground text-sm">{t('empty')}</p>
)}
</CardContent>
</Card>
Expand Down
21 changes: 21 additions & 0 deletions src/app/groups/create/create-group.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use client'

import { GroupForm } from '@/components/group-form'
import { trpc } from '@/trpc/client'
import { useRouter } from 'next/navigation'

export const CreateGroup = () => {
const { mutateAsync } = trpc.groups.create.useMutation()
const utils = trpc.useUtils()
const router = useRouter()

return (
<GroupForm
onSubmit={async (groupFormValues) => {
const { groupId } = await mutateAsync({ groupFormValues })
await utils.groups.invalidate()
router.push(`/groups/${groupId}`)
}}
/>
)
}
19 changes: 7 additions & 12 deletions src/app/groups/create/page.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
import { GroupForm } from '@/components/group-form'
import { createGroup } from '@/lib/api'
import { groupFormSchema } from '@/lib/schemas'
import { redirect } from 'next/navigation'
import { CreateGroup } from '@/app/groups/create/create-group'
import { Metadata } from 'next'

export default function CreateGroupPage() {
async function createGroupAction(values: unknown) {
'use server'
const groupFormValues = groupFormSchema.parse(values)
const group = await createGroup(groupFormValues)
redirect(`/groups/${group.id}`)
}
export const metadata: Metadata = {
title: 'Create Group',
}

return <GroupForm onSubmit={createGroupAction} />
export default function CreateGroupPage() {
return <CreateGroup />
}
15 changes: 15 additions & 0 deletions src/trpc/routers/groups/create.procedure.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { createGroup } from '@/lib/api'
import { groupFormSchema } from '@/lib/schemas'
import { baseProcedure } from '@/trpc/init'
import { z } from 'zod'

export const createGroupProcedure = baseProcedure
.input(
z.object({
groupFormValues: groupFormSchema,
}),
)
.mutation(async ({ input: { groupFormValues } }) => {
const group = await createGroup(groupFormValues)
return { groupId: group.id }
})
2 changes: 2 additions & 0 deletions src/trpc/routers/groups/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createTRPCRouter } from '@/trpc/init'
import { activitiesRouter } from '@/trpc/routers/groups/activities'
import { groupBalancesRouter } from '@/trpc/routers/groups/balances'
import { createGroupProcedure } from '@/trpc/routers/groups/create.procedure'
import { groupExpensesRouter } from '@/trpc/routers/groups/expenses'
import { getGroupProcedure } from '@/trpc/routers/groups/get.procedure'
import { groupStatsRouter } from '@/trpc/routers/groups/stats'
Expand All @@ -13,5 +14,6 @@ export const groupsRouter = createTRPCRouter({
activities: activitiesRouter,

get: getGroupProcedure,
create: createGroupProcedure,
update: updateGroupProcedure,
})
Loading