|
| 1 | +import { FC, useRef } from 'react'; |
| 2 | +import { useTranslation } from 'react-i18next'; |
| 3 | +import { submitFormByRef } from '../../../utils'; |
| 4 | +import { |
| 5 | + StopFormState as FormState, |
| 6 | + StopForm, |
| 7 | + StopFormState, |
| 8 | +} from '../../forms/stop'; |
| 9 | +import { CustomOverlay } from '../CustomOverlay'; |
| 10 | +import { Modal } from '../modal'; |
| 11 | +import { CreateChanges, EditChanges, isEditChanges } from './hooks'; |
| 12 | + |
| 13 | +const testIds = { |
| 14 | + modal: 'CopyStopModal', |
| 15 | +}; |
| 16 | + |
| 17 | +type CopyStopModalProps = { |
| 18 | + readonly defaultValues: Partial<FormState>; |
| 19 | + readonly onCancel: () => void; |
| 20 | + readonly onClose: () => void; |
| 21 | + readonly onSubmit: (changes: CreateChanges, state: StopFormState) => void; |
| 22 | +}; |
| 23 | + |
| 24 | +export const CopyStopModal: FC<CopyStopModalProps> = ({ |
| 25 | + defaultValues, |
| 26 | + onCancel, |
| 27 | + onClose, |
| 28 | + onSubmit, |
| 29 | +}) => { |
| 30 | + const { t } = useTranslation(); |
| 31 | + |
| 32 | + const formRef = useRef<ExplicitAny>(null); |
| 33 | + const onSave = () => submitFormByRef(formRef); |
| 34 | + |
| 35 | + const onStopFormSubmit = ( |
| 36 | + changes: CreateChanges | EditChanges, |
| 37 | + state: StopFormState, |
| 38 | + ) => { |
| 39 | + if (!isEditChanges(changes)) { |
| 40 | + onSubmit(changes, state); |
| 41 | + } |
| 42 | + }; |
| 43 | + |
| 44 | + return ( |
| 45 | + <CustomOverlay |
| 46 | + className="min-h-full w-[calc(450px+(2*1.25rem))]" |
| 47 | + position="top-left" |
| 48 | + > |
| 49 | + <Modal |
| 50 | + className="pointer-events-auto flex max-h-full flex-col" |
| 51 | + headerClassName="*:text-xl px-4 py-4 items-center" |
| 52 | + bodyClassName="mx-0 my-0" |
| 53 | + footerClassName="px-4 py-2" |
| 54 | + testId={testIds.modal} |
| 55 | + onSave={onSave} |
| 56 | + onCancel={onCancel} |
| 57 | + onClose={onClose} |
| 58 | + heading={t('stops.createStopCopy', { |
| 59 | + stopLabel: defaultValues.publicCode?.value, |
| 60 | + })} |
| 61 | + navigationContext="StopForm" |
| 62 | + > |
| 63 | + <StopForm |
| 64 | + defaultValues={defaultValues} |
| 65 | + editing |
| 66 | + submitState |
| 67 | + onSubmit={onStopFormSubmit} |
| 68 | + ref={formRef} |
| 69 | + /> |
| 70 | + </Modal> |
| 71 | + </CustomOverlay> |
| 72 | + ); |
| 73 | +}; |
0 commit comments