Files
gruperly/apps/web/src/features/groups/detail/AttendeeDetailProvider.tsx
Jose Selesan 02af5e3c11 feat: add attendee detail and stats features
- Implemented API endpoints to fetch attendee details and statistics.
- Created new route for fetching attendee by ID and their attendance stats.
- Added schemas for attendee statistics.
- Developed frontend components to display attendee details and attendance statistics.
- Integrated attendee detail provider to manage state and data fetching.
- Added tests for attendee retrieval and statistics calculation.
2026-09-28 16:16:21 -03:00

120 lines
3.9 KiB
TypeScript

import { createContext, useContext, useState } from 'react'
import type { ReactNode } from 'react'
import { useQuery } from '@tanstack/react-query'
import { useNavigate } from '@tanstack/react-router'
import type { AttendeeDto, AttendeeStats, GroupWaitlistEntryDto } from '@gruperly/shared'
import { useToast } from '../../../components/ui'
import { getAttendeeStats, getGroup, getGroupAttendee, getGroupWaitlist } from '../../../lib/api'
import { useRemoveAttendee } from './useRemoveAttendee'
type AttendeeDetailContextValue = {
groupId: string
groupName: string | undefined
attendee: AttendeeDto | undefined
attendeePending: boolean
attendeeFailed: boolean
attendeeStats: AttendeeStats | undefined
attendeeStatsPending: boolean
attendeeStatsFailed: boolean
firstWaitlistEntry: GroupWaitlistEntryDto | null
waitlistTotal: number
waitlistPending: boolean
isRemoveModalOpen: boolean
openRemoveModal: () => void
closeRemoveModal: () => void
confirmRemoveAttendee: (promoteFromWaitlist: boolean) => void
isSubmittingRemoval: boolean
copyAbsenceNotifyUrl: (url: string) => void
}
const AttendeeDetailContext = createContext<AttendeeDetailContextValue | null>(null)
export function AttendeeDetailProvider({
groupId,
attendeeId,
children,
}: {
groupId: string
attendeeId: string
children: ReactNode
}) {
const toast = useToast()
const navigate = useNavigate()
const [isRemoveModalOpen, setIsRemoveModalOpen] = useState(false)
const groupQuery = useQuery({
queryKey: ['group', groupId],
queryFn: () => getGroup(groupId),
enabled: Boolean(groupId),
})
const attendeeQuery = useQuery({
queryKey: ['group-attendee', groupId, attendeeId],
queryFn: () => getGroupAttendee(groupId, attendeeId),
enabled: Boolean(groupId && attendeeId),
})
const attendeeStatsQuery = useQuery({
queryKey: ['attendee-stats', groupId, attendeeId],
queryFn: () => getAttendeeStats(groupId, attendeeId),
enabled: Boolean(groupId && attendeeId),
})
const waitlistQuery = useQuery({
queryKey: ['group-waitlist', groupId],
queryFn: () => getGroupWaitlist(groupId, 1, 100),
enabled: Boolean(groupId && isRemoveModalOpen),
})
const waitlistEntries = waitlistQuery.data?.data ?? []
const firstWaitlistEntry = waitlistEntries[0] ?? null
const waitlistTotal = waitlistQuery.data?.pagination?.total ?? waitlistEntries.length
const { removeAttendee, isRemovingAttendee } = useRemoveAttendee({
groupId,
onRemoved: () => {
setIsRemoveModalOpen(false)
void navigate({ to: '/groups/$groupId', params: { groupId } })
},
})
const copyAbsenceNotifyUrl = (url: string) => {
void navigator.clipboard.writeText(url).then(() => {
toast.success('Link copiado al portapapeles.')
})
}
const value: AttendeeDetailContextValue = {
groupId,
groupName: groupQuery.data?.name,
attendee: attendeeQuery.data,
attendeePending: attendeeQuery.isPending,
attendeeFailed: attendeeQuery.isError,
attendeeStats: attendeeStatsQuery.data,
attendeeStatsPending: attendeeStatsQuery.isPending,
attendeeStatsFailed: attendeeStatsQuery.isError,
firstWaitlistEntry,
waitlistTotal,
waitlistPending: waitlistQuery.isPending,
isRemoveModalOpen,
openRemoveModal: () => setIsRemoveModalOpen(true),
closeRemoveModal: () => setIsRemoveModalOpen(false),
confirmRemoveAttendee: (promoteFromWaitlist) => {
if (!attendeeQuery.data) return
removeAttendee({ attendee: attendeeQuery.data, promoteFromWaitlist })
},
isSubmittingRemoval: isRemovingAttendee,
copyAbsenceNotifyUrl,
}
return <AttendeeDetailContext.Provider value={value}>{children}</AttendeeDetailContext.Provider>
}
export function useAttendeeDetail() {
const context = useContext(AttendeeDetailContext)
if (!context) {
throw new Error('useAttendeeDetail debe usarse dentro de <AttendeeDetailProvider>')
}
return context
}