import { useEffect, useMemo } from 'react'; import { TextInput, Button, Group, Stack, Paper, Title, Text, Badge, Select, } from '@mantine/core'; import { useForm } from '@mantine/form'; import { useTranslation } from 'react-i18next'; import { notify } from '@ema-platform/ui'; import type { Location, LocationType } from '../types/location'; interface LocationFormValues { code: string; namesEn: string; namesAm: string; locationTypeId: string; } interface LocationFormProps { locationTypes: LocationType[]; parentLocation?: Location | null; editingLocation?: Location | null; onSubmit: (values: { code: string; names: { en: string; am: string }; locationTypeId: string; parentId?: string | null; }) => void; onCancel: () => void; isSubmitting?: boolean; } export function LocationForm({ locationTypes, parentLocation, editingLocation, onSubmit, onCancel, isSubmitting, }: LocationFormProps) { const { t, i18n } = useTranslation(); const locale = i18n.language as 'en' | 'am'; const isEditing = !!editingLocation; const { type, allAtLevel, typesAvailable } = useMemo(() => { if (isEditing) { const lt = locationTypes.find((t) => t.id === editingLocation.locationTypeId); return { type: lt ?? null, allAtLevel: [lt].filter(Boolean) as LocationType[], typesAvailable: false }; } if (parentLocation) { const parentLevel = locationTypes.find((lt) => lt.id === parentLocation.locationTypeId)?.level ?? 0; const nextLevel = locationTypes .filter((lt) => lt.level === parentLevel + 1) .sort((a, b) => a.level - b.level); return { type: nextLevel[0] ?? null, allAtLevel: nextLevel, typesAvailable: nextLevel.length > 1 }; } const minLevel = Math.min(...locationTypes.map((lt) => lt.level)); const roots = locationTypes .filter((lt) => lt.level === minLevel) .sort((a, b) => a.level - b.level); return { type: roots[0] ?? null, allAtLevel: roots, typesAvailable: roots.length > 1 }; }, [locationTypes, parentLocation, editingLocation, isEditing]); const form = useForm({ initialValues: { code: '', namesEn: '', namesAm: '', locationTypeId: typesAvailable ? '' : (type?.id ?? ''), }, validate: { code: (v) => (!v ? t('location.validation.codeRequired') : null), namesEn: (v) => (!v ? t('location.validation.nameEnRequired') : null), namesAm: (v) => (!v ? t('location.validation.nameAmRequired') : null), locationTypeId: (v) => { if (typesAvailable && !v) return t('location.validation.typeRequired'); return null; }, }, }); useEffect(() => { if (editingLocation) { form.setValues({ code: editingLocation.code, namesEn: editingLocation.names.en, namesAm: editingLocation.names.am, locationTypeId: editingLocation.locationTypeId, }); } }, [editingLocation]); const handleSubmit = form.onSubmit((values) => { const resolvedTypeId = typesAvailable ? values.locationTypeId : type?.id; if (!resolvedTypeId) { notify.error(t('location.noTypesAvailable')); return; } onSubmit({ code: values.code, names: { en: values.namesEn, am: values.namesAm }, locationTypeId: resolvedTypeId, parentId: editingLocation ? editingLocation.parentId : parentLocation?.id ?? null, }); }); const title = isEditing ? t('location.editTitle') : t('location.addTitle'); return ( {title} {parentLocation && !isEditing && ( )}
{typesAvailable ? (