|
| 1 | +import { FC, useMemo, useState } from 'react'; |
| 2 | +import { useTranslation } from 'react-i18next'; |
| 3 | +import { StopRegistryTransportModeType } from '../../../../../generated/graphql'; |
| 4 | +import { mapStopRegistryTransportModeTypeToUiName } from '../../../../../i18n/uiNameMappings'; |
| 5 | +import { StopWithDetails } from '../../../../../types'; |
| 6 | +import { |
| 7 | + JoreListbox, |
| 8 | + ListboxOptionItem, |
| 9 | + Modal, |
| 10 | + ModalBody, |
| 11 | + ModalHeader, |
| 12 | + NewModalFooter, |
| 13 | + SimpleButton, |
| 14 | +} from '../../../../../uiComponents'; |
| 15 | +import { parseVehicleMode } from '../../../../../utils'; |
| 16 | +import { |
| 17 | + showDangerToastWithError, |
| 18 | + showSuccessToast, |
| 19 | +} from '../../../../../utils/toastService'; |
| 20 | +import { StopModalStopAreaFormSchema } from '../../../../forms/stop/types'; |
| 21 | +import { StopAreaSearchCombobox } from './StopAreaSearchCombobox'; |
| 22 | +import { useCreateMirrorQuay } from './useCreateMirrorQuay'; |
| 23 | + |
| 24 | +const testIds = { |
| 25 | + modal: 'MakeHybridStopModal', |
| 26 | + transportModeDropdown: 'MakeHybridStopModal::transportMode', |
| 27 | + stopAreaInput: 'MakeHybridStopModal::stopAreaInput', |
| 28 | + stopAreaOption: (code: string) => `MakeHybridStopModal::stopArea::${code}`, |
| 29 | + confirmButton: 'MakeHybridStopModal::confirm', |
| 30 | + cancelButton: 'MakeHybridStopModal::cancel', |
| 31 | +}; |
| 32 | + |
| 33 | +const SUPPORTED_TRANSPORT_MODES = [ |
| 34 | + StopRegistryTransportModeType.Bus, |
| 35 | + StopRegistryTransportModeType.Tram, |
| 36 | +] as const; |
| 37 | + |
| 38 | +type MakeHybridStopModalProps = { |
| 39 | + readonly isOpen: boolean; |
| 40 | + readonly onClose: () => void; |
| 41 | + readonly parentStop: StopWithDetails | null; |
| 42 | +}; |
| 43 | + |
| 44 | +export const MakeHybridStopModal: FC<MakeHybridStopModalProps> = ({ |
| 45 | + isOpen, |
| 46 | + onClose, |
| 47 | + parentStop, |
| 48 | +}) => { |
| 49 | + const { t } = useTranslation(); |
| 50 | + |
| 51 | + const [selectedMode, setSelectedMode] = |
| 52 | + useState<StopRegistryTransportModeType | null>(null); |
| 53 | + const [selectedStopArea, setSelectedStopArea] = |
| 54 | + useState<StopModalStopAreaFormSchema | null>(null); |
| 55 | + |
| 56 | + const { createMirrorQuay, loading: saving } = useCreateMirrorQuay(); |
| 57 | + |
| 58 | + const currentTransportMode = parentStop?.stop_place?.transportMode ?? null; |
| 59 | + |
| 60 | + const availableModes = useMemo( |
| 61 | + () => |
| 62 | + SUPPORTED_TRANSPORT_MODES.filter((mode) => mode !== currentTransportMode), |
| 63 | + [currentTransportMode], |
| 64 | + ); |
| 65 | + |
| 66 | + const transportModeOptions: ReadonlyArray< |
| 67 | + ListboxOptionItem<StopRegistryTransportModeType> |
| 68 | + > = useMemo( |
| 69 | + () => |
| 70 | + availableModes.map((mode) => ({ |
| 71 | + value: mode, |
| 72 | + content: mapStopRegistryTransportModeTypeToUiName(t, mode), |
| 73 | + })), |
| 74 | + [availableModes, t], |
| 75 | + ); |
| 76 | + |
| 77 | + const vehicleMode = selectedMode ? parseVehicleMode(selectedMode) : null; |
| 78 | + |
| 79 | + const handleModeChange = (mode: StopRegistryTransportModeType) => { |
| 80 | + setSelectedMode(mode); |
| 81 | + setSelectedStopArea(null); |
| 82 | + }; |
| 83 | + |
| 84 | + const resetState = () => { |
| 85 | + setSelectedMode(null); |
| 86 | + setSelectedStopArea(null); |
| 87 | + }; |
| 88 | + |
| 89 | + const handleClose = () => { |
| 90 | + resetState(); |
| 91 | + onClose(); |
| 92 | + }; |
| 93 | + |
| 94 | + const handleConfirm = async () => { |
| 95 | + if (!parentStop || !selectedStopArea || !selectedMode || !vehicleMode) { |
| 96 | + return; |
| 97 | + } |
| 98 | + |
| 99 | + try { |
| 100 | + const success = await createMirrorQuay({ |
| 101 | + targetStopPlaceId: selectedStopArea.netexId, |
| 102 | + parentStop, |
| 103 | + vehicleMode, |
| 104 | + }); |
| 105 | + |
| 106 | + if (success) { |
| 107 | + showSuccessToast(t(($) => $.stopDetails.hybrid.success)); |
| 108 | + handleClose(); |
| 109 | + } |
| 110 | + } catch (err) { |
| 111 | + showDangerToastWithError( |
| 112 | + t(($) => $.stopDetails.hybrid.error), |
| 113 | + err, |
| 114 | + ); |
| 115 | + } |
| 116 | + }; |
| 117 | + |
| 118 | + const canConfirm = !!selectedMode && !!selectedStopArea && !saving; |
| 119 | + |
| 120 | + return ( |
| 121 | + <Modal |
| 122 | + isOpen={isOpen} |
| 123 | + onClose={handleClose} |
| 124 | + contentClassName="w-1/3" |
| 125 | + testId={testIds.modal} |
| 126 | + > |
| 127 | + <ModalHeader |
| 128 | + onClose={handleClose} |
| 129 | + heading={t(($) => $.stopDetails.hybrid.title)} |
| 130 | + /> |
| 131 | + <ModalBody className="space-y-4"> |
| 132 | + <div> |
| 133 | + <label className="mb-1 block text-sm font-bold"> |
| 134 | + {t(($) => $.stopDetails.hybrid.transportMode)} |
| 135 | + </label> |
| 136 | + <JoreListbox<StopRegistryTransportModeType> |
| 137 | + buttonContent={ |
| 138 | + selectedMode |
| 139 | + ? mapStopRegistryTransportModeTypeToUiName(t, selectedMode) |
| 140 | + : t(($) => $.stopDetails.hybrid.selectTransportMode) |
| 141 | + } |
| 142 | + options={transportModeOptions} |
| 143 | + value={selectedMode ?? undefined} |
| 144 | + onChange={handleModeChange} |
| 145 | + testId={testIds.transportModeDropdown} |
| 146 | + /> |
| 147 | + </div> |
| 148 | + |
| 149 | + <div> |
| 150 | + <label className="mb-1 block text-sm font-bold"> |
| 151 | + {t(($) => $.stopDetails.hybrid.stopArea)} |
| 152 | + </label> |
| 153 | + <StopAreaSearchCombobox |
| 154 | + vehicleMode={vehicleMode} |
| 155 | + value={selectedStopArea} |
| 156 | + onChange={setSelectedStopArea} |
| 157 | + disabled={!selectedMode} |
| 158 | + inputTestId={testIds.stopAreaInput} |
| 159 | + optionTestId={testIds.stopAreaOption} |
| 160 | + /> |
| 161 | + </div> |
| 162 | + </ModalBody> |
| 163 | + <NewModalFooter> |
| 164 | + <SimpleButton |
| 165 | + inverted |
| 166 | + onClick={handleClose} |
| 167 | + testId={testIds.cancelButton} |
| 168 | + > |
| 169 | + {t(($) => $.stopDetails.hybrid.cancel)} |
| 170 | + </SimpleButton> |
| 171 | + <SimpleButton |
| 172 | + onClick={handleConfirm} |
| 173 | + disabled={!canConfirm} |
| 174 | + testId={testIds.confirmButton} |
| 175 | + > |
| 176 | + {saving ? '...' : t(($) => $.stopDetails.hybrid.confirm)} |
| 177 | + </SimpleButton> |
| 178 | + </NewModalFooter> |
| 179 | + </Modal> |
| 180 | + ); |
| 181 | +}; |
0 commit comments