New page for Add group member
This commit is contained in:
@@ -3,21 +3,13 @@ import type { ReactNode } from 'react'
|
||||
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
|
||||
import type {
|
||||
AttendeeDto,
|
||||
CreateAttendee,
|
||||
CreateAttendeeResult,
|
||||
CreateGroupWaitlistEntry,
|
||||
GroupDto,
|
||||
GroupWaitlistEntryDto,
|
||||
GroupWaitlistList,
|
||||
GroupRiskLevel,
|
||||
} from '@gruperly/shared'
|
||||
import { useToast } from '../../../components/ui'
|
||||
import type { ParsedContactRow } from '../../../lib/file-parser'
|
||||
import {
|
||||
addToGroupWaitlist,
|
||||
ApiError,
|
||||
bulkCreateAttendees,
|
||||
createAttendee,
|
||||
getGroup,
|
||||
getGroupAttendees,
|
||||
getGroupsRiskOverview,
|
||||
@@ -28,17 +20,7 @@ import {
|
||||
} from '../../../lib/api'
|
||||
import { useRemoveAttendee } from './useRemoveAttendee'
|
||||
|
||||
export type BulkAttendeeRow = {
|
||||
firstName: string
|
||||
lastName: string
|
||||
fullName: string
|
||||
phone: string
|
||||
email?: string
|
||||
notes?: string
|
||||
}
|
||||
|
||||
export type ListSection = 'members' | 'waitlist'
|
||||
export type AddAttendeeTab = 'quick' | 'bulk'
|
||||
|
||||
type GroupDetailContextValue = {
|
||||
groupId: string
|
||||
@@ -60,49 +42,19 @@ type GroupDetailContextValue = {
|
||||
setSearchFilter: (value: string) => void
|
||||
listSection: ListSection
|
||||
setListSection: (value: ListSection) => void
|
||||
activeTab: AddAttendeeTab
|
||||
setActiveTab: (value: AddAttendeeTab) => void
|
||||
isInviteModalOpen: boolean
|
||||
openInviteModal: () => void
|
||||
closeInviteModal: () => void
|
||||
isAddAttendeeModalOpen: boolean
|
||||
openAddAttendeeModal: () => void
|
||||
closeAddAttendeeModal: () => void
|
||||
attendeeToRemove: AttendeeDto | null
|
||||
requestRemoveAttendee: (attendee: AttendeeDto) => void
|
||||
cancelRemoveAttendee: () => void
|
||||
confirmRemoveAttendee: (promoteFromWaitlist: boolean) => void
|
||||
selectedWaitlistEntry: GroupWaitlistEntryDto | null
|
||||
setSelectedWaitlistEntry: (entry: GroupWaitlistEntryDto | null) => void
|
||||
isCapacityModalOpen: boolean
|
||||
capacityPayload: CreateAttendee | null
|
||||
closeCapacityModal: () => void
|
||||
isBulkCapacityModalOpen: boolean
|
||||
closeBulkCapacityModal: () => void
|
||||
parsedContacts: ParsedContactRow[]
|
||||
setParsedContacts: (contacts: ParsedContactRow[]) => void
|
||||
removeParsedContact: (id: string) => void
|
||||
fileName: string | null
|
||||
setFileName: (name: string | null) => void
|
||||
isParsingFile: boolean
|
||||
setIsParsingFile: (value: boolean) => void
|
||||
isDragging: boolean
|
||||
setIsDragging: (value: boolean) => void
|
||||
validRowsCount: number
|
||||
invalidRowsCount: number
|
||||
isSubmittingAttendee: boolean
|
||||
isSubmittingForcedAttendee: boolean
|
||||
isSubmittingWaitlistEntry: boolean
|
||||
isSubmittingBulkImport: boolean
|
||||
isSubmittingRemoval: boolean
|
||||
isSubmittingPromotion: boolean
|
||||
isSubmittingWaitlistRemoval: boolean
|
||||
isRegeneratingInvite: boolean
|
||||
submitQuickAttendee: (payload: CreateAttendee) => void
|
||||
submitForcedAttendee: () => void
|
||||
submitWaitlistEntry: () => void
|
||||
requestBulkImport: () => void
|
||||
confirmBulkImportOverCapacity: () => void
|
||||
promoteEntry: (entry: GroupWaitlistEntryDto) => void
|
||||
removeEntry: (entry: GroupWaitlistEntryDto) => void
|
||||
regenerateInvite: () => void
|
||||
@@ -118,19 +70,10 @@ export function GroupDetailProvider({ groupId, children }: { groupId: string; ch
|
||||
const toast = useToast()
|
||||
|
||||
const [isInviteModalOpen, setIsInviteModalOpen] = useState(false)
|
||||
const [isAddAttendeeModalOpen, setIsAddAttendeeModalOpen] = useState(false)
|
||||
const [activeTab, setActiveTab] = useState<AddAttendeeTab>('quick')
|
||||
const [listSection, setListSection] = useState<ListSection>('members')
|
||||
const [attendeeToRemove, setAttendeeToRemove] = useState<AttendeeDto | null>(null)
|
||||
const [selectedWaitlistEntry, setSelectedWaitlistEntry] = useState<GroupWaitlistEntryDto | null>(null)
|
||||
const [isCapacityModalOpen, setIsCapacityModalOpen] = useState(false)
|
||||
const [pendingCapacityPayload, setPendingCapacityPayload] = useState<CreateAttendee | null>(null)
|
||||
const [isBulkCapacityModalOpen, setIsBulkCapacityModalOpen] = useState(false)
|
||||
const [searchFilter, setSearchFilter] = useState('')
|
||||
const [parsedContacts, setParsedContacts] = useState<ParsedContactRow[]>([])
|
||||
const [fileName, setFileName] = useState<string | null>(null)
|
||||
const [isParsingFile, setIsParsingFile] = useState(false)
|
||||
const [isDragging, setIsDragging] = useState(false)
|
||||
|
||||
const groupQuery = useQuery({
|
||||
queryKey: ['group', groupId],
|
||||
@@ -170,28 +113,12 @@ export function GroupDetailProvider({ groupId, children }: { groupId: string; ch
|
||||
const firstWaitlistEntry = waitlistEntries[0] ?? null
|
||||
const hasFreeCapacity = group?.capacity == null || attendeesTotal < group.capacity
|
||||
|
||||
const closeAddAttendeeFlow = () => {
|
||||
setPendingCapacityPayload(null)
|
||||
setIsCapacityModalOpen(false)
|
||||
setIsAddAttendeeModalOpen(false)
|
||||
}
|
||||
|
||||
const invalidateGroupQueries = () => {
|
||||
void queryClient.invalidateQueries({ queryKey: ['group-attendees', groupId] })
|
||||
void queryClient.invalidateQueries({ queryKey: ['group-waitlist', groupId] })
|
||||
void queryClient.invalidateQueries({ queryKey: ['group', groupId] })
|
||||
}
|
||||
|
||||
const handleCreateSuccess = (result: CreateAttendeeResult) => {
|
||||
if (result.outcome === 'created') {
|
||||
toast.success(`Miembro ${result.attendee.fullName} agregado con éxito.`)
|
||||
} else {
|
||||
toast.info(result.message)
|
||||
}
|
||||
void queryClient.invalidateQueries({ queryKey: ['group-attendees', groupId] })
|
||||
closeAddAttendeeFlow()
|
||||
}
|
||||
|
||||
const regenerateTokenMutation = useMutation({
|
||||
mutationFn: () => getInviteToken(groupId, true),
|
||||
onSuccess: (data) => {
|
||||
@@ -203,89 +130,6 @@ export function GroupDetailProvider({ groupId, children }: { groupId: string; ch
|
||||
},
|
||||
})
|
||||
|
||||
const createAttendeeMutation = useMutation({
|
||||
mutationFn: (payload: CreateAttendee) => createAttendee(groupId, payload),
|
||||
onMutate: (payload) => setPendingCapacityPayload(payload),
|
||||
onSuccess: handleCreateSuccess,
|
||||
onError: (err: Error) => {
|
||||
if (err instanceof ApiError && err.problem?.code === 'group_capacity_reached') {
|
||||
setIsCapacityModalOpen(true)
|
||||
return
|
||||
}
|
||||
toast.error(err.message || 'Error al agregar miembro.')
|
||||
},
|
||||
})
|
||||
|
||||
const createAttendeeForceMutation = useMutation({
|
||||
mutationFn: (payload: CreateAttendee) => createAttendee(groupId, payload, { allowOverflow: true }),
|
||||
onSuccess: handleCreateSuccess,
|
||||
onError: (err: Error) => {
|
||||
toast.error(err.message || 'Error al agregar miembro.')
|
||||
},
|
||||
})
|
||||
|
||||
const addToWaitlistMutation = useMutation({
|
||||
mutationFn: (payload: CreateGroupWaitlistEntry) => addToGroupWaitlist(groupId, payload),
|
||||
onMutate: async (payload: CreateGroupWaitlistEntry) => {
|
||||
await queryClient.cancelQueries({ queryKey: ['group-waitlist', groupId] })
|
||||
const previousWaitlist = queryClient.getQueryData<GroupWaitlistList>(['group-waitlist', groupId])
|
||||
|
||||
if (previousWaitlist) {
|
||||
const optimisticEntry: GroupWaitlistEntryDto = {
|
||||
id: `temp-${Date.now()}`,
|
||||
groupId,
|
||||
fullName: `${payload.firstName} ${payload.lastName ?? ''}`.trim(),
|
||||
phone: payload.phone,
|
||||
email: payload.email || null,
|
||||
notes: payload.notes ?? null,
|
||||
status: 'PENDING',
|
||||
createdAt: new Date().toISOString(),
|
||||
updatedAt: new Date().toISOString(),
|
||||
}
|
||||
queryClient.setQueryData<GroupWaitlistList>(['group-waitlist', groupId], {
|
||||
data: [optimisticEntry, ...previousWaitlist.data],
|
||||
pagination: {
|
||||
...previousWaitlist.pagination,
|
||||
total: previousWaitlist.pagination.total + 1,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
return { previousWaitlist }
|
||||
},
|
||||
onSuccess: (entry) => {
|
||||
toast.info(`${entry.fullName} fue agregado a la lista de espera del grupo.`)
|
||||
invalidateGroupQueries()
|
||||
closeAddAttendeeFlow()
|
||||
},
|
||||
onError: (err: Error, _payload, context) => {
|
||||
if (context?.previousWaitlist) {
|
||||
queryClient.setQueryData<GroupWaitlistList>(['group-waitlist', groupId], context.previousWaitlist)
|
||||
}
|
||||
if (err instanceof ApiError && err.problem?.code === 'already_waitlisted') {
|
||||
toast.info('Este número ya está en la lista de espera del grupo.')
|
||||
} else {
|
||||
toast.error(err.message || 'Error al agregar a la lista de espera.')
|
||||
}
|
||||
setPendingCapacityPayload(null)
|
||||
setIsCapacityModalOpen(false)
|
||||
},
|
||||
})
|
||||
|
||||
const bulkImportMutation = useMutation({
|
||||
mutationFn: (rows: BulkAttendeeRow[]) => bulkCreateAttendees(groupId, { attendees: rows }),
|
||||
onSuccess: (res) => {
|
||||
toast.success(res.message)
|
||||
setParsedContacts([])
|
||||
setFileName(null)
|
||||
void queryClient.invalidateQueries({ queryKey: ['group-attendees', groupId] })
|
||||
setIsAddAttendeeModalOpen(false)
|
||||
},
|
||||
onError: (err: Error) => {
|
||||
toast.error(err.message || 'Error al importar miembros.')
|
||||
},
|
||||
})
|
||||
|
||||
const removeAttendeeMutation = useRemoveAttendee({
|
||||
groupId,
|
||||
onRemoved: () => setAttendeeToRemove(null),
|
||||
@@ -359,41 +203,6 @@ export function GroupDetailProvider({ groupId, children }: { groupId: string; ch
|
||||
window.open(whatsappShareUrl, '_blank', 'noopener,noreferrer')
|
||||
}
|
||||
|
||||
const validRows: BulkAttendeeRow[] = useMemo(
|
||||
() =>
|
||||
parsedContacts
|
||||
.filter((contact) => contact.isValid)
|
||||
.map((contact) => ({
|
||||
firstName: contact.firstName,
|
||||
lastName: contact.lastName,
|
||||
fullName: contact.fullName,
|
||||
phone: contact.phone,
|
||||
email: contact.email || undefined,
|
||||
notes: contact.notes || undefined,
|
||||
})),
|
||||
[parsedContacts],
|
||||
)
|
||||
|
||||
const runBulkImport = () => {
|
||||
if (validRows.length === 0) {
|
||||
toast.error('No hay miembros válidos para importar.')
|
||||
return
|
||||
}
|
||||
bulkImportMutation.mutate(validRows)
|
||||
}
|
||||
|
||||
const requestBulkImport = () => {
|
||||
if (validRows.length === 0) {
|
||||
toast.error('No hay miembros válidos para importar.')
|
||||
return
|
||||
}
|
||||
if (group?.capacity != null && attendeesTotal + validRows.length > group.capacity) {
|
||||
setIsBulkCapacityModalOpen(true)
|
||||
return
|
||||
}
|
||||
runBulkImport()
|
||||
}
|
||||
|
||||
const value: GroupDetailContextValue = {
|
||||
groupId,
|
||||
group,
|
||||
@@ -414,14 +223,9 @@ export function GroupDetailProvider({ groupId, children }: { groupId: string; ch
|
||||
setSearchFilter,
|
||||
listSection,
|
||||
setListSection,
|
||||
activeTab,
|
||||
setActiveTab,
|
||||
isInviteModalOpen,
|
||||
openInviteModal: () => setIsInviteModalOpen(true),
|
||||
closeInviteModal: () => setIsInviteModalOpen(false),
|
||||
isAddAttendeeModalOpen,
|
||||
openAddAttendeeModal: () => setIsAddAttendeeModalOpen(true),
|
||||
closeAddAttendeeModal: () => setIsAddAttendeeModalOpen(false),
|
||||
attendeeToRemove,
|
||||
requestRemoveAttendee: (attendee) => setAttendeeToRemove(attendee),
|
||||
cancelRemoveAttendee: () => setAttendeeToRemove(null),
|
||||
@@ -431,49 +235,10 @@ export function GroupDetailProvider({ groupId, children }: { groupId: string; ch
|
||||
},
|
||||
selectedWaitlistEntry,
|
||||
setSelectedWaitlistEntry,
|
||||
isCapacityModalOpen,
|
||||
capacityPayload: pendingCapacityPayload,
|
||||
closeCapacityModal: () => {
|
||||
setPendingCapacityPayload(null)
|
||||
setIsCapacityModalOpen(false)
|
||||
},
|
||||
isBulkCapacityModalOpen,
|
||||
closeBulkCapacityModal: () => setIsBulkCapacityModalOpen(false),
|
||||
parsedContacts,
|
||||
setParsedContacts,
|
||||
removeParsedContact: (id) => setParsedContacts((prev) => prev.filter((c) => c.id !== id)),
|
||||
fileName,
|
||||
setFileName,
|
||||
isParsingFile,
|
||||
setIsParsingFile,
|
||||
isDragging,
|
||||
setIsDragging,
|
||||
validRowsCount: validRows.length,
|
||||
invalidRowsCount: parsedContacts.length - validRows.length,
|
||||
isSubmittingAttendee: createAttendeeMutation.isPending,
|
||||
isSubmittingForcedAttendee: createAttendeeForceMutation.isPending,
|
||||
isSubmittingWaitlistEntry: addToWaitlistMutation.isPending,
|
||||
isSubmittingBulkImport: bulkImportMutation.isPending,
|
||||
isSubmittingRemoval: removeAttendeeMutation.isRemovingAttendee,
|
||||
isSubmittingPromotion: promoteWaitlistMutation.isPending,
|
||||
isSubmittingWaitlistRemoval: removeWaitlistEntryMutation.isPending,
|
||||
isRegeneratingInvite: regenerateTokenMutation.isPending,
|
||||
submitQuickAttendee: (payload) => createAttendeeMutation.mutate(payload),
|
||||
submitForcedAttendee: () => {
|
||||
if (pendingCapacityPayload) {
|
||||
createAttendeeForceMutation.mutate({ ...pendingCapacityPayload })
|
||||
}
|
||||
},
|
||||
submitWaitlistEntry: () => {
|
||||
if (pendingCapacityPayload) {
|
||||
addToWaitlistMutation.mutate({ ...pendingCapacityPayload })
|
||||
}
|
||||
},
|
||||
requestBulkImport,
|
||||
confirmBulkImportOverCapacity: () => {
|
||||
setIsBulkCapacityModalOpen(false)
|
||||
runBulkImport()
|
||||
},
|
||||
promoteEntry: (entry) => {
|
||||
setSelectedWaitlistEntry(null)
|
||||
promoteWaitlistMutation.mutate(entry.id)
|
||||
|
||||
Reference in New Issue
Block a user