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:
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>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user