Files
gruperly/apps/web/src/features/groups/EditGroupView.tsx
Jose Selesan 58c776e18c feat(groups): kids groups with an adult in charge
- Add the `kidsGroups` plan feature (Pro) and `Group.audience` (ADULTS | KIDS);
  creating or switching a group to KIDS requires the feature, existing KIDS
  groups keep working after a downgrade.
- Model the adult in charge as a Person linked through `person_guardians`;
  migrate the old guardian_name/guardian_phone columns and drop them.
- Kids quick add: kid + guardian form, guardian lookup by phone to pick an
  existing kid or add a sibling; backend rejects KIDS adds without guardian.
- Attendee page: "Adulto a cargo" section with siblings, editable via
  PATCH /groups/:gid/attendees/:aid/guardian.
- Contact phone (WhatsApp jobs, attention, student access invite, students at
  risk, attendee actions) prefers the guardian's phone.
- Edit group screen (PATCH /groups/:gid, owner only); switching back to ADULTS
  is blocked while kids have no phone of their own.
- Hide bulk import in KIDS groups until it carries guardian data.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 13:27:13 -03:00

79 lines
2.8 KiB
TypeScript

import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
import { Link, useNavigate } from '@tanstack/react-router'
import type { CreateFirstGroup, GroupDto } from '@gruperly/shared'
import { ChevronLeft, Loader2 } from 'lucide-react'
import { useTranslation } from 'react-i18next'
import { errorMessage } from '../../i18n'
import { getGroup, updateGroup } from '../../lib/api'
import { GroupForm } from './GroupForm'
/* "Editar grupo": el mismo formulario que el alta, con los datos del grupo. */
export function EditGroupView({ groupId }: { groupId: string }) {
const navigate = useNavigate()
const { t } = useTranslation('groups')
const queryClient = useQueryClient()
const groupQuery = useQuery({
queryKey: ['group', groupId],
queryFn: () => getGroup(groupId),
})
const update = useMutation({
// El país vive en el usuario, no en el grupo: el formulario lo trae pero no se manda.
mutationFn: ({ country: _country, ...values }: CreateFirstGroup) => updateGroup(groupId, values),
onSuccess: (group) => {
queryClient.setQueryData<GroupDto>(['group', groupId], (current) => (current ? { ...current, ...group } : group))
void queryClient.invalidateQueries({ queryKey: ['groups'] })
void navigate({ to: '/groups/$groupId', params: { groupId } })
},
})
const group = groupQuery.data
return (
<section className="mx-auto w-full max-w-md">
<Link
to="/groups/$groupId"
params={{ groupId }}
className="mb-6 hidden items-center gap-1 text-sm font-medium text-foreground/70 transition-colors hover:text-primary lg:inline-flex"
>
<ChevronLeft className="size-4" />
{group?.name ?? t('title')}
</Link>
{group ? (
<GroupForm
heading={t('edit.heading')}
description={t('edit.description')}
submitLabel={t('edit.submit')}
isSubmitting={update.isPending}
errorMessage={update.isError ? errorMessage(update.error, t('edit.error')) : null}
initialValues={toFormValues(group)}
onSubmit={(values) => update.mutate(values)}
/>
) : groupQuery.isError ? (
<p role="alert" className="text-sm text-danger-fg">
{errorMessage(groupQuery.error, t('edit.loadError'))}
</p>
) : (
<div className="flex justify-center py-20">
<Loader2 className="size-8 animate-spin text-accent" />
</div>
)}
</section>
)
}
function toFormValues(group: GroupDto): Partial<CreateFirstGroup> {
return {
name: group.name,
days: group.days ?? [],
time: group.time ?? '09:00',
capacity: group.capacity ?? undefined,
price: group.price ?? undefined,
billingType: group.billingType ?? 'MONTHLY',
dueDay: group.dueDay ?? 1,
audience: group.audience,
}
}