- Translated comments from Spanish to English in vite.config.ts, docker-compose.yml, attendance-risk.ts, analytics.ts, attendance.ts, attendees.ts, enums.ts, groups.ts, and onboarding.ts. - Improved descriptions for better understanding of code functionality and purpose. - Ensured uniformity in terminology and phrasing throughout the codebase.
114 lines
3.8 KiB
TypeScript
114 lines
3.8 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, AttendeeHistory, GroupWaitlistEntryDto } from '@gruperly/shared'
|
|
import { getAttendeeHistory, getGroup, getGroupAttendee, getGroupWaitlist } from '../../lib/api'
|
|
import { useRemoveAttendee } from '../groups/detail/useRemoveAttendee'
|
|
|
|
type AttendeeDetailContextValue = {
|
|
groupId: string
|
|
groupName: string | undefined
|
|
attendee: AttendeeDto | undefined
|
|
attendeePending: boolean
|
|
attendeeFailed: boolean
|
|
history: AttendeeHistory | undefined
|
|
historyPending: boolean
|
|
historyFailed: boolean
|
|
firstWaitlistEntry: GroupWaitlistEntryDto | null
|
|
waitlistTotal: number
|
|
waitlistPending: boolean
|
|
isRemoveDialogOpen: boolean
|
|
openRemoveDialog: () => void
|
|
closeRemoveDialog: () => void
|
|
confirmRemoveAttendee: (promoteFromWaitlist: boolean) => void
|
|
isSubmittingRemoval: boolean
|
|
}
|
|
|
|
const AttendeeDetailContext = createContext<AttendeeDetailContextValue | null>(null)
|
|
|
|
export function AttendeeDetailProvider({
|
|
groupId,
|
|
attendeeId,
|
|
children,
|
|
}: {
|
|
groupId: string
|
|
attendeeId: string
|
|
children: ReactNode
|
|
}) {
|
|
const navigate = useNavigate()
|
|
const [isRemoveDialogOpen, setIsRemoveDialogOpen] = 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),
|
|
})
|
|
|
|
// Same key the analytics screen uses: both read the same history and do not
|
|
// clobber each other's cache.
|
|
const historyQuery = useQuery({
|
|
queryKey: ['attendee-history', groupId, attendeeId],
|
|
queryFn: () => getAttendeeHistory(groupId, attendeeId),
|
|
enabled: Boolean(groupId && attendeeId),
|
|
})
|
|
|
|
// The waitlist is only needed for the remove dialog, so it is not fetched
|
|
// until the user opens it.
|
|
const waitlistQuery = useQuery({
|
|
queryKey: ['group-waitlist', groupId],
|
|
queryFn: () => getGroupWaitlist(groupId, 1, 100),
|
|
enabled: Boolean(groupId && isRemoveDialogOpen),
|
|
})
|
|
|
|
const waitlistEntries = waitlistQuery.data?.data ?? []
|
|
const firstWaitlistEntry = waitlistEntries[0] ?? null
|
|
const waitlistTotal = waitlistQuery.data?.pagination?.total ?? waitlistEntries.length
|
|
|
|
const { removeAttendee, isRemovingAttendee } = useRemoveAttendee({
|
|
groupId,
|
|
onRemoved: () => {
|
|
setIsRemoveDialogOpen(false)
|
|
void navigate({ to: '/groups/$groupId', params: { groupId } })
|
|
},
|
|
})
|
|
|
|
const value: AttendeeDetailContextValue = {
|
|
groupId,
|
|
groupName: groupQuery.data?.name,
|
|
attendee: attendeeQuery.data,
|
|
attendeePending: attendeeQuery.isPending,
|
|
attendeeFailed: attendeeQuery.isError,
|
|
history: historyQuery.data,
|
|
historyPending: historyQuery.isPending,
|
|
historyFailed: historyQuery.isError,
|
|
firstWaitlistEntry,
|
|
waitlistTotal,
|
|
waitlistPending: waitlistQuery.isPending,
|
|
isRemoveDialogOpen,
|
|
openRemoveDialog: () => setIsRemoveDialogOpen(true),
|
|
closeRemoveDialog: () => setIsRemoveDialogOpen(false),
|
|
confirmRemoveAttendee: (promoteFromWaitlist) => {
|
|
if (!attendeeQuery.data) return
|
|
removeAttendee({ attendee: attendeeQuery.data, promoteFromWaitlist })
|
|
},
|
|
isSubmittingRemoval: isRemovingAttendee,
|
|
}
|
|
|
|
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
|
|
}
|