mirror of
https://github.com/spliit-app/spliit.git
synced 2026-02-25 08:56:13 +01:00
Use tRPC for recent groups page (#253)
* Use tRPC for recent groups page * Use tRPC for adding group by URL * Use tRPC for saving visited group * Group context
This commit is contained in:
@@ -21,6 +21,8 @@ function getQueryClient() {
|
||||
return (clientQueryClientSingleton ??= makeQueryClient())
|
||||
}
|
||||
|
||||
export const trpcClient = getQueryClient()
|
||||
|
||||
function getUrl() {
|
||||
const base = (() => {
|
||||
if (typeof window !== 'undefined') return ''
|
||||
|
||||
@@ -1,19 +1,10 @@
|
||||
import { getGroup, getGroupExpensesParticipants } from '@/lib/api'
|
||||
import { getGroup } from '@/lib/api'
|
||||
import { baseProcedure } from '@/trpc/init'
|
||||
import { TRPCError } from '@trpc/server'
|
||||
import { z } from 'zod'
|
||||
|
||||
export const getGroupProcedure = baseProcedure
|
||||
.input(z.object({ groupId: z.string().min(1) }))
|
||||
.query(async ({ input: { groupId } }) => {
|
||||
const group = await getGroup(groupId)
|
||||
if (!group) {
|
||||
throw new TRPCError({
|
||||
code: 'NOT_FOUND',
|
||||
message: 'Group not found.',
|
||||
})
|
||||
}
|
||||
|
||||
const participantsWithExpenses = await getGroupExpensesParticipants(groupId)
|
||||
return { group, participantsWithExpenses }
|
||||
return { group }
|
||||
})
|
||||
|
||||
19
src/trpc/routers/groups/getDetails.procedure.ts
Normal file
19
src/trpc/routers/groups/getDetails.procedure.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import { getGroup, getGroupExpensesParticipants } from '@/lib/api'
|
||||
import { baseProcedure } from '@/trpc/init'
|
||||
import { TRPCError } from '@trpc/server'
|
||||
import { z } from 'zod'
|
||||
|
||||
export const getGroupDetailsProcedure = baseProcedure
|
||||
.input(z.object({ groupId: z.string().min(1) }))
|
||||
.query(async ({ input: { groupId } }) => {
|
||||
const group = await getGroup(groupId)
|
||||
if (!group) {
|
||||
throw new TRPCError({
|
||||
code: 'NOT_FOUND',
|
||||
message: 'Group not found.',
|
||||
})
|
||||
}
|
||||
|
||||
const participantsWithExpenses = await getGroupExpensesParticipants(groupId)
|
||||
return { group, participantsWithExpenses }
|
||||
})
|
||||
@@ -6,6 +6,8 @@ import { groupExpensesRouter } from '@/trpc/routers/groups/expenses'
|
||||
import { getGroupProcedure } from '@/trpc/routers/groups/get.procedure'
|
||||
import { groupStatsRouter } from '@/trpc/routers/groups/stats'
|
||||
import { updateGroupProcedure } from '@/trpc/routers/groups/update.procedure'
|
||||
import { getGroupDetailsProcedure } from './getDetails.procedure'
|
||||
import { listGroupsProcedure } from './list.procedure'
|
||||
|
||||
export const groupsRouter = createTRPCRouter({
|
||||
expenses: groupExpensesRouter,
|
||||
@@ -14,6 +16,8 @@ export const groupsRouter = createTRPCRouter({
|
||||
activities: activitiesRouter,
|
||||
|
||||
get: getGroupProcedure,
|
||||
getDetails: getGroupDetailsProcedure,
|
||||
list: listGroupsProcedure,
|
||||
create: createGroupProcedure,
|
||||
update: updateGroupProcedure,
|
||||
})
|
||||
|
||||
14
src/trpc/routers/groups/list.procedure.ts
Normal file
14
src/trpc/routers/groups/list.procedure.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import { getGroups } from '@/lib/api'
|
||||
import { baseProcedure } from '@/trpc/init'
|
||||
import { z } from 'zod'
|
||||
|
||||
export const listGroupsProcedure = baseProcedure
|
||||
.input(
|
||||
z.object({
|
||||
groupIds: z.array(z.string().min(1)),
|
||||
}),
|
||||
)
|
||||
.query(async ({ input: { groupIds } }) => {
|
||||
const groups = await getGroups(groupIds)
|
||||
return { groups }
|
||||
})
|
||||
Reference in New Issue
Block a user