Skip to content

Commit

Permalink
Use tRPC in group modals
Browse files Browse the repository at this point in the history
  • Loading branch information
scastiel committed Oct 19, 2024
1 parent 3b039ca commit 30c28e2
Show file tree
Hide file tree
Showing 9 changed files with 286 additions and 252 deletions.
5 changes: 0 additions & 5 deletions src/app/groups/[groupId]/edit/page.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { cached } from '@/app/cached-functions'
import { EditGroup } from '@/app/groups/[groupId]/edit/edit-group'
import { Metadata } from 'next'
import { notFound } from 'next/navigation'

export const metadata: Metadata = {
title: 'Settings',
Expand All @@ -12,8 +10,5 @@ export default async function EditGroupPage({
}: {
params: { groupId: string }
}) {
const group = await cached.getGroup(groupId)
if (!group) notFound()

return <EditGroup groupId={groupId} />
}
25 changes: 17 additions & 8 deletions src/app/groups/[groupId]/expenses/active-user-modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,24 @@ import {
} from '@/components/ui/drawer'
import { Label } from '@/components/ui/label'
import { RadioGroup, RadioGroupItem } from '@/components/ui/radio-group'
import { getGroup } from '@/lib/api'
import { useMediaQuery } from '@/lib/hooks'
import { cn } from '@/lib/utils'
import { trpc } from '@/trpc/client'
import { AppRouterOutput } from '@/trpc/routers/_app'
import { useTranslations } from 'next-intl'
import { ComponentProps, useEffect, useState } from 'react'

type Props = {
group: NonNullable<Awaited<ReturnType<typeof getGroup>>>
}

export function ActiveUserModal({ group }: Props) {
export function ActiveUserModal({ groupId }: { groupId: string }) {
const t = useTranslations('Expenses.ActiveUserModal')
const [open, setOpen] = useState(false)
const isDesktop = useMediaQuery('(min-width: 768px)')
const { data: groupData } = trpc.groups.get.useQuery({ groupId })

const group = groupData?.group

useEffect(() => {
if (!group) return

const tempUser = localStorage.getItem(`newGroup-activeUser`)
const activeUser = localStorage.getItem(`${group.id}-activeUser`)
if (!tempUser && !activeUser) {
Expand All @@ -42,6 +44,8 @@ export function ActiveUserModal({ group }: Props) {
}, [group])

function updateOpen(open: boolean) {
if (!group) return

if (!open && !localStorage.getItem(`${group.id}-activeUser`)) {
localStorage.setItem(`${group.id}-activeUser`, 'None')
}
Expand Down Expand Up @@ -93,14 +97,19 @@ function ActiveUserForm({
group,
close,
className,
}: ComponentProps<'form'> & { group: Props['group']; close: () => void }) {
}: ComponentProps<'form'> & {
group?: AppRouterOutput['groups']['get']['group']
close: () => void
}) {
const t = useTranslations('Expenses.ActiveUserModal')
const [selected, setSelected] = useState('None')

return (
<form
className={cn('grid items-start gap-4', className)}
onSubmit={(event) => {
if (!group) return

event.preventDefault()
localStorage.setItem(`${group.id}-activeUser`, selected)
close()
Expand All @@ -114,7 +123,7 @@ function ActiveUserForm({
{t('nobody')}
</Label>
</div>
{group.participants.map((participant) => (
{group?.participants.map((participant) => (
<div key={participant.id} className="flex items-center space-x-2">
<RadioGroupItem value={participant.id} id={participant.id} />
<Label htmlFor={participant.id} className="flex-1">
Expand Down
Loading

0 comments on commit 30c28e2

Please sign in to comment.