feat: cambiar a un miembro de grupo
- `POST /groups/:gid/attendees/:aid/move` mueve al mismo attendee a otro grupo del profesor: conserva historial, cuotas (siguen del grupo anterior) y acceso a la app de alumnos. Cancela las faltas avisadas y libera lo reservado en clases futuras del grupo anterior. - Cupo y teléfono duplicado se validan con `checkCanJoinGroup`, compartido con el paso desde la lista de espera. - Ficha del miembro: "Cambiar de grupo" abre un diálogo con el selector de grupo destino y navega a la ficha en el grupo nuevo. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -15,7 +15,7 @@ export function AttendeeActionsMenu({
|
||||
* when it closes. */
|
||||
triggerRef: RefObject<HTMLButtonElement | null>
|
||||
}) {
|
||||
const { beginContactEdit, openRemoveDialog } = useAttendeeDetail()
|
||||
const { beginContactEdit, openRemoveDialog, openChangeGroupDialog } = useAttendeeDetail()
|
||||
const toast = useToast()
|
||||
const [isOpen, setIsOpen] = useState(false)
|
||||
const menuRef = useRef<HTMLDivElement>(null)
|
||||
@@ -69,12 +69,6 @@ export function AttendeeActionsMenu({
|
||||
items[next]?.focus()
|
||||
}
|
||||
|
||||
const showPendingChangeGroup = () => {
|
||||
close()
|
||||
// TODO(miembros): navegar al selector cuando se confirmen las reglas y exista el endpoint.
|
||||
toast.info('Cambiar de grupo estará disponible cuando se confirme el flujo de traspaso.')
|
||||
}
|
||||
|
||||
const showPendingPause = () => {
|
||||
close()
|
||||
// TODO(miembros): conectar la pausa cuando se defina su efecto sobre cupos y cobros.
|
||||
@@ -110,7 +104,10 @@ export function AttendeeActionsMenu({
|
||||
type="button"
|
||||
role="menuitem"
|
||||
className={ITEM_CLASS}
|
||||
onClick={showPendingChangeGroup}
|
||||
onClick={() => {
|
||||
close()
|
||||
openChangeGroupDialog()
|
||||
}}
|
||||
>
|
||||
<MoveRight className="size-4 shrink-0 text-secondary" aria-hidden="true" />
|
||||
Cambiar de grupo
|
||||
|
||||
@@ -5,14 +5,19 @@ import { useNavigate } from '@tanstack/react-router'
|
||||
import type {
|
||||
AttendeeDetailDto,
|
||||
AttendeeHistory,
|
||||
GroupListItem,
|
||||
GroupWaitlistEntryDto,
|
||||
UpdateAttendee,
|
||||
} from '@gruperly/shared'
|
||||
import { useToast } from '../../components/ui'
|
||||
import {
|
||||
ApiError,
|
||||
getAttendeeHistory,
|
||||
getGroup,
|
||||
getGroupAttendee,
|
||||
getGroups,
|
||||
getGroupWaitlist,
|
||||
moveGroupAttendee,
|
||||
updateGroupAttendee,
|
||||
} from '../../lib/api'
|
||||
import { useRemoveAttendee } from '../groups/detail/useRemoveAttendee'
|
||||
@@ -39,6 +44,14 @@ type AttendeeDetailContextValue = {
|
||||
closeRemoveDialog: () => void
|
||||
confirmRemoveAttendee: (promoteFromWaitlist: boolean) => void
|
||||
isSubmittingRemoval: boolean
|
||||
isChangeGroupDialogOpen: boolean
|
||||
openChangeGroupDialog: () => void
|
||||
closeChangeGroupDialog: () => void
|
||||
/** Grupos del profe (incluye el actual); se piden al abrir el diálogo. */
|
||||
groups: GroupListItem[]
|
||||
groupsPending: boolean
|
||||
moveToGroup: (targetGroupId: string) => void
|
||||
isMovingAttendee: boolean
|
||||
}
|
||||
|
||||
const AttendeeDetailContext = createContext<AttendeeDetailContextValue | null>(null)
|
||||
@@ -54,7 +67,9 @@ export function AttendeeDetailProvider({
|
||||
}) {
|
||||
const navigate = useNavigate()
|
||||
const queryClient = useQueryClient()
|
||||
const toast = useToast()
|
||||
const [isRemoveDialogOpen, setIsRemoveDialogOpen] = useState(false)
|
||||
const [isChangeGroupDialogOpen, setIsChangeGroupDialogOpen] = useState(false)
|
||||
const [isEditingContact, setIsEditingContact] = useState(false)
|
||||
|
||||
const groupQuery = useQuery({
|
||||
@@ -108,6 +123,46 @@ export function AttendeeDetailProvider({
|
||||
},
|
||||
})
|
||||
|
||||
// Misma key que Grupos y Cobros: el diálogo reusa el caché si ya está.
|
||||
const groupsQuery = useQuery({
|
||||
queryKey: ['groups'],
|
||||
queryFn: getGroups,
|
||||
enabled: isChangeGroupDialogOpen,
|
||||
})
|
||||
const groups = groupsQuery.data?.data ?? []
|
||||
|
||||
const moveMutation = useMutation({
|
||||
mutationFn: (targetGroupId: string) => moveGroupAttendee(groupId, attendeeId, targetGroupId),
|
||||
onSuccess: (moved, targetGroupId) => {
|
||||
const targetName = groups.find((group) => group.id === targetGroupId)?.name ?? 'otro grupo'
|
||||
toast.success(`${moved.fullName} pasó a ${targetName}.`)
|
||||
for (const id of [groupId, targetGroupId]) {
|
||||
void queryClient.invalidateQueries({ queryKey: ['group-attendees', id] })
|
||||
void queryClient.invalidateQueries({ queryKey: ['group', id] })
|
||||
}
|
||||
void queryClient.invalidateQueries({ queryKey: ['groups'] })
|
||||
void queryClient.invalidateQueries({ queryKey: ['attention'] })
|
||||
setIsChangeGroupDialogOpen(false)
|
||||
// La ficha vive bajo el grupo: pasa a la URL del grupo nuevo sin dejar la
|
||||
// vieja en el historial (ya no existe).
|
||||
void navigate({
|
||||
to: '/groups/$groupId/attendees/$attendeeId',
|
||||
params: { groupId: targetGroupId, attendeeId },
|
||||
replace: true,
|
||||
})
|
||||
},
|
||||
onError: (err: Error) => {
|
||||
const code = err instanceof ApiError ? err.problem?.code : undefined
|
||||
if (code === 'group_capacity_reached') {
|
||||
toast.error('Ese grupo alcanzó su cupo. Elegí otro o liberá un lugar primero.')
|
||||
} else if (code === 'attendee_already_registered') {
|
||||
toast.error('En ese grupo ya hay un miembro con el mismo teléfono.')
|
||||
} else {
|
||||
toast.error(err.message || 'No se pudo cambiar de grupo.')
|
||||
}
|
||||
},
|
||||
})
|
||||
|
||||
const value: AttendeeDetailContextValue = {
|
||||
groupId,
|
||||
groupName: groupQuery.data?.name,
|
||||
@@ -135,6 +190,13 @@ export function AttendeeDetailProvider({
|
||||
removeAttendee({ attendee: attendeeQuery.data, promoteFromWaitlist })
|
||||
},
|
||||
isSubmittingRemoval: isRemovingAttendee,
|
||||
isChangeGroupDialogOpen,
|
||||
openChangeGroupDialog: () => setIsChangeGroupDialogOpen(true),
|
||||
closeChangeGroupDialog: () => setIsChangeGroupDialogOpen(false),
|
||||
groups,
|
||||
groupsPending: groupsQuery.isPending,
|
||||
moveToGroup: (targetGroupId) => moveMutation.mutate(targetGroupId),
|
||||
isMovingAttendee: moveMutation.isPending,
|
||||
}
|
||||
|
||||
return <AttendeeDetailContext.Provider value={value}>{children}</AttendeeDetailContext.Provider>
|
||||
|
||||
@@ -11,6 +11,7 @@ import { AttendeeContactSection } from './AttendeeContactSection'
|
||||
import { AttendeeNotesSection } from './AttendeeNotesSection'
|
||||
import { AttendeePaymentsSection } from './AttendeePaymentsSection'
|
||||
import { useAttendeeDetail } from './AttendeeDetailProvider'
|
||||
import { ChangeGroupDialog } from './ChangeGroupDialog'
|
||||
import { ConfirmRemoveAttendeeDialog } from './ConfirmRemoveAttendeeDialog'
|
||||
import { formatMemberSince } from './attendance'
|
||||
import { paymentPresentation } from './detail'
|
||||
@@ -97,6 +98,7 @@ export function AttendeeDetailView() {
|
||||
) : null}
|
||||
|
||||
<ConfirmRemoveAttendeeDialog returnFocusRef={menuTriggerRef} />
|
||||
<ChangeGroupDialog returnFocusRef={menuTriggerRef} />
|
||||
</section>
|
||||
)
|
||||
}
|
||||
|
||||
143
apps/web/src/features/attendee-detail/ChangeGroupDialog.tsx
Normal file
143
apps/web/src/features/attendee-detail/ChangeGroupDialog.tsx
Normal file
@@ -0,0 +1,143 @@
|
||||
import { useEffect, useRef, useState } from 'react'
|
||||
import type { RefObject } from 'react'
|
||||
import type { GroupListItem } from '@gruperly/shared'
|
||||
import type { LucideIcon } from 'lucide-react'
|
||||
import { CalendarX, History, Loader2, MoveRight, Wallet } from 'lucide-react'
|
||||
import { Button, Modal } from '../../components/ui'
|
||||
import { useAttendeeDetail } from './AttendeeDetailProvider'
|
||||
|
||||
type Consequence = { key: string; icon: LucideIcon; text: string }
|
||||
|
||||
function isFull(group: GroupListItem): boolean {
|
||||
return group.capacity != null && group.attendeesCount >= group.capacity
|
||||
}
|
||||
|
||||
export function ChangeGroupDialog({
|
||||
returnFocusRef,
|
||||
}: {
|
||||
/** The element focus returns to when it closes (the "⋯" button). */
|
||||
returnFocusRef: RefObject<HTMLButtonElement | null>
|
||||
}) {
|
||||
const {
|
||||
groupId,
|
||||
attendee,
|
||||
groupName,
|
||||
groups,
|
||||
groupsPending,
|
||||
isChangeGroupDialogOpen,
|
||||
closeChangeGroupDialog,
|
||||
moveToGroup,
|
||||
isMovingAttendee,
|
||||
} = useAttendeeDetail()
|
||||
|
||||
const [targetGroupId, setTargetGroupId] = useState('')
|
||||
const wasOpen = useRef(false)
|
||||
|
||||
// Cada apertura arranca sin destino elegido; al cerrar, el foco vuelve al "⋯".
|
||||
useEffect(() => {
|
||||
if (isChangeGroupDialogOpen) {
|
||||
wasOpen.current = true
|
||||
setTargetGroupId('')
|
||||
return
|
||||
}
|
||||
if (!wasOpen.current) return
|
||||
wasOpen.current = false
|
||||
returnFocusRef.current?.focus()
|
||||
}, [isChangeGroupDialogOpen, returnFocusRef])
|
||||
|
||||
if (!attendee) return null
|
||||
|
||||
const otherGroups = groups.filter((group) => group.id !== groupId)
|
||||
const currentName = groupName ?? 'el grupo actual'
|
||||
|
||||
const consequences: Consequence[] = [
|
||||
{
|
||||
key: 'historial',
|
||||
icon: History,
|
||||
text: 'Conserva su historial, sus cuotas y su acceso a la app de alumnos.',
|
||||
},
|
||||
{
|
||||
key: 'cuotas',
|
||||
icon: Wallet,
|
||||
text: `Las cuotas pendientes de ${currentName} siguen vigentes.`,
|
||||
},
|
||||
{
|
||||
key: 'faltas',
|
||||
icon: CalendarX,
|
||||
text: `Se cancelan las faltas avisadas para clases futuras de ${currentName}.`,
|
||||
},
|
||||
]
|
||||
|
||||
return (
|
||||
<Modal
|
||||
isOpen={isChangeGroupDialogOpen}
|
||||
onClose={closeChangeGroupDialog}
|
||||
title="Cambiar de grupo"
|
||||
description={`${attendee.fullName}${groupName ? ` · ${groupName}` : ''}`}
|
||||
maxWidth="sm"
|
||||
>
|
||||
<div className="space-y-4">
|
||||
{groupsPending ? (
|
||||
<div className="flex justify-center py-4">
|
||||
<Loader2 className="size-5 animate-spin text-foreground/60" aria-label="Cargando grupos" />
|
||||
</div>
|
||||
) : otherGroups.length === 0 ? (
|
||||
<p className="rounded-xl border border-border bg-primary-soft/30 px-4 py-3 text-sm text-foreground/70">
|
||||
No tenés otros grupos. Creá uno para poder mover a este miembro.
|
||||
</p>
|
||||
) : (
|
||||
<label className="block">
|
||||
<span className="mb-1.5 block text-sm font-medium text-primary">Mover a</span>
|
||||
<select
|
||||
autoFocus
|
||||
value={targetGroupId}
|
||||
onChange={(event) => setTargetGroupId(event.target.value)}
|
||||
className="h-10 w-full rounded-xl border border-border bg-surface px-3 text-base text-primary sm:text-sm focus:border-accent focus:outline-none focus:ring-2 focus:ring-accent/30"
|
||||
>
|
||||
<option value="" disabled>
|
||||
Elegí un grupo
|
||||
</option>
|
||||
{otherGroups.map((group) => {
|
||||
const full = isFull(group)
|
||||
return (
|
||||
<option key={group.id} value={group.id} disabled={full}>
|
||||
{group.name}
|
||||
{full ? ' (sin cupo)' : ''}
|
||||
</option>
|
||||
)
|
||||
})}
|
||||
</select>
|
||||
</label>
|
||||
)}
|
||||
|
||||
<ul className="space-y-2.5">
|
||||
{consequences.map(({ icon: Icon, key, text }) => (
|
||||
<li key={key} className="flex items-start gap-2.5 text-sm text-foreground">
|
||||
<Icon className="mt-0.5 size-4 shrink-0" aria-hidden="true" />
|
||||
<span>{text}</span>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
|
||||
<div className="flex flex-col-reverse gap-2.5 border-t border-border pt-3 sm:flex-row sm:justify-end">
|
||||
<Button variant="ghost" onClick={closeChangeGroupDialog}>
|
||||
Cancelar
|
||||
</Button>
|
||||
<Button
|
||||
variant="primary"
|
||||
onClick={() => moveToGroup(targetGroupId)}
|
||||
disabled={!targetGroupId || isMovingAttendee}
|
||||
className="gap-2"
|
||||
>
|
||||
{isMovingAttendee ? (
|
||||
<Loader2 className="size-4 animate-spin" />
|
||||
) : (
|
||||
<MoveRight className="size-4" />
|
||||
)}
|
||||
<span>Cambiar de grupo</span>
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</Modal>
|
||||
)
|
||||
}
|
||||
@@ -316,6 +316,13 @@ export const updateGroupAttendee = (
|
||||
body: JSON.stringify(payload),
|
||||
})
|
||||
|
||||
// Mueve al miembro a otro grupo del profe; devuelve el attendee con su nuevo `groupId`.
|
||||
export const moveGroupAttendee = (groupId: string, attendeeId: string, targetGroupId: string) =>
|
||||
apiFetch<AttendeeDto>(`/api/v1/groups/${groupId}/attendees/${attendeeId}/move`, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({ targetGroupId }),
|
||||
})
|
||||
|
||||
export const getGroupWaitlist = (groupId: string, page = 1, pageSize = 100) =>
|
||||
apiFetch<GroupWaitlistList>(`/api/v1/groups/${groupId}/waitlist?page=${page}&pageSize=${pageSize}`)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user