186 lines
6.3 KiB
TypeScript
186 lines
6.3 KiB
TypeScript
import { useEffect, useRef, useState } from 'react'
|
|
import type { RefObject } from 'react'
|
|
import type { GroupListItem } from '@gruperly/shared'
|
|
import type { LucideIcon } from 'lucide-react'
|
|
import { CalendarCheck, CalendarX, History, Loader2, MoveRight, UserPlus, Wallet } from 'lucide-react'
|
|
import { Button, Modal } from '../../components/ui'
|
|
import { useAttendeeDetail } from './AttendeeDetailProvider'
|
|
import { useTranslation } from 'react-i18next'
|
|
|
|
type Consequence = { key: string; icon: LucideIcon; text: string }
|
|
|
|
function isFull(group: GroupListItem): boolean {
|
|
return group.capacity != null && group.attendeesCount >= group.capacity
|
|
}
|
|
|
|
/* "Cambiar de grupo" (sale de este y entra al otro) y "Agregar a otro grupo"
|
|
(queda en los dos) comparten el selector de destino; cambian el texto, las
|
|
consecuencias y la acción. */
|
|
export function ChangeGroupDialog({
|
|
returnFocusRef,
|
|
}: {
|
|
/** The element focus returns to when it closes (the "⋯" button). */
|
|
returnFocusRef: RefObject<HTMLButtonElement | null>
|
|
}) {
|
|
const {
|
|
groupId,
|
|
attendee,
|
|
groupName,
|
|
groups,
|
|
groupsPending,
|
|
groupDialogMode,
|
|
closeGroupDialog,
|
|
moveToGroup,
|
|
isMovingAttendee,
|
|
addToGroup,
|
|
isAddingToGroup,
|
|
} = useAttendeeDetail()
|
|
const isOpen = groupDialogMode !== null
|
|
const isAdd = groupDialogMode === 'add'
|
|
|
|
const [targetGroupId, setTargetGroupId] = useState('')
|
|
const wasOpen = useRef(false)
|
|
const { t } = useTranslation(['attendeeDetail', 'common'])
|
|
|
|
// Cada apertura arranca sin destino elegido; al cerrar, el foco vuelve al "⋯".
|
|
useEffect(() => {
|
|
if (isOpen) {
|
|
wasOpen.current = true
|
|
setTargetGroupId('')
|
|
return
|
|
}
|
|
if (!wasOpen.current) return
|
|
wasOpen.current = false
|
|
returnFocusRef.current?.focus()
|
|
}, [isOpen, returnFocusRef])
|
|
|
|
if (!attendee) return null
|
|
|
|
// Fuera el grupo actual y los grupos donde ya está. Al agregar, una baja
|
|
// anterior (DROPPED) se puede reactivar; al mover, no (sería otra membresía).
|
|
const blocked = new Set(
|
|
attendee.otherGroups
|
|
.filter((membership) => !isAdd || membership.status !== 'DROPPED')
|
|
.map((membership) => membership.groupId),
|
|
)
|
|
const otherGroups = groups.filter((group) => group.id !== groupId && !blocked.has(group.id))
|
|
const currentName = groupName ?? t('changeGroup.currentGroup')
|
|
const isSubmitting = isAdd ? isAddingToGroup : isMovingAttendee
|
|
|
|
const consequences: Consequence[] = isAdd
|
|
? [
|
|
{
|
|
key: 'sigue',
|
|
icon: CalendarCheck,
|
|
text: t('changeGroup.addStays', { group: currentName }),
|
|
},
|
|
{
|
|
key: 'datos',
|
|
icon: History,
|
|
text: t('changeGroup.addShares'),
|
|
},
|
|
{
|
|
key: 'cuotas',
|
|
icon: Wallet,
|
|
text: t('changeGroup.addSeparate'),
|
|
},
|
|
]
|
|
: [
|
|
{
|
|
key: 'historial',
|
|
icon: History,
|
|
text: t('changeGroup.moveKeeps'),
|
|
},
|
|
{
|
|
key: 'cuotas',
|
|
icon: Wallet,
|
|
text: t('changeGroup.moveFees', { group: currentName }),
|
|
},
|
|
{
|
|
key: 'faltas',
|
|
icon: CalendarX,
|
|
text: t('changeGroup.moveNotices', { group: currentName }),
|
|
},
|
|
]
|
|
|
|
return (
|
|
<Modal
|
|
isOpen={isOpen}
|
|
onClose={closeGroupDialog}
|
|
title={isAdd ? t('changeGroup.titleAdd') : t('changeGroup.title')}
|
|
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={t('changeGroup.loadingGroups')} />
|
|
</div>
|
|
) : otherGroups.length === 0 ? (
|
|
<p className="rounded-xl border border-border bg-primary-soft/30 px-4 py-3 text-sm text-foreground/70">
|
|
{groups.length > 1
|
|
? t('changeGroup.allGroups')
|
|
: isAdd
|
|
? t('changeGroup.noGroupsAdd')
|
|
: t('changeGroup.noGroupsMove')}
|
|
</p>
|
|
) : (
|
|
<label className="block">
|
|
<span className="mb-1.5 block text-sm font-medium text-primary">
|
|
{isAdd ? t('changeGroup.addTo') : t('changeGroup.moveTo')}
|
|
</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>
|
|
{t('changeGroup.pick')}
|
|
</option>
|
|
{otherGroups.map((group) => {
|
|
const full = isFull(group)
|
|
return (
|
|
<option key={group.id} value={group.id} disabled={full}>
|
|
{full ? t('changeGroup.full', { name: group.name }) : group.name}
|
|
</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={closeGroupDialog}>
|
|
{t('common:actions.cancel')}
|
|
</Button>
|
|
<Button
|
|
variant="primary"
|
|
onClick={() => (isAdd ? addToGroup(targetGroupId) : moveToGroup(targetGroupId))}
|
|
disabled={!targetGroupId || isSubmitting}
|
|
className="gap-2"
|
|
>
|
|
{isSubmitting ? (
|
|
<Loader2 className="size-4 animate-spin" />
|
|
) : isAdd ? (
|
|
<UserPlus className="size-4" />
|
|
) : (
|
|
<MoveRight className="size-4" />
|
|
)}
|
|
<span>{isAdd ? t('changeGroup.add') : t('changeGroup.move')}</span>
|
|
</Button>
|
|
</div>
|
|
</div>
|
|
</Modal>
|
|
)
|
|
}
|