|
| 1 | +/* eslint-disable jsx-a11y/no-autofocus */ |
| 2 | +import { |
| 3 | + Path, |
| 4 | + PostSearchApiArg, |
| 5 | + SearchResultItemOperationalPoint, |
| 6 | + osrdEditoastApi, |
| 7 | +} from 'common/api/osrdEditoastApi'; |
| 8 | +import React, { useEffect, useState } from 'react'; |
| 9 | +import { useTranslation } from 'react-i18next'; |
| 10 | +import { useDispatch, useSelector } from 'react-redux'; |
| 11 | +import { getInfraID, getRollingStockID } from 'reducers/osrdconf/selectors'; |
| 12 | +import { useDebounce } from 'utils/helpers'; |
| 13 | +import cx from 'classnames'; |
| 14 | +import { GoAlert, GoTriangleRight } from 'react-icons/go'; |
| 15 | +import { loadPathFinding } from 'modules/trainschedule/components/ManageTrainSchedule/helpers/adjustConfWithTrainToModify'; |
| 16 | +import { setFailure } from 'reducers/main'; |
| 17 | +import bbox from '@turf/bbox'; |
| 18 | +import { Position } from 'geojson'; |
| 19 | + |
| 20 | +type SearchConstraintType = (string | number | string[])[]; |
| 21 | +type PathfindingProps = { |
| 22 | + zoomToFeature: (lngLat: Position, id?: undefined, source?: undefined) => void; |
| 23 | +}; |
| 24 | + |
| 25 | +const monospaceOneCharREMWidth = 0.6225; |
| 26 | + |
| 27 | +function OpTooltips({ opList }: { opList: SearchResultItemOperationalPoint[] }) { |
| 28 | + // Calculation of chars distance from left to put tooltip on center of op name |
| 29 | + const calcLeftMargin = (charsFromLeft: number, length: number) => |
| 30 | + charsFromLeft * monospaceOneCharREMWidth + (length * monospaceOneCharREMWidth) / 2; |
| 31 | + let charsFromLeft = 0; |
| 32 | + return ( |
| 33 | + <div className="op-tooltips"> |
| 34 | + {opList.map((op, idx) => { |
| 35 | + const leftMargin = calcLeftMargin(charsFromLeft, op.trigram.length); |
| 36 | + charsFromLeft = charsFromLeft + op.trigram.length + 1; |
| 37 | + return ( |
| 38 | + op.trigram !== '' && ( |
| 39 | + <div |
| 40 | + className={cx('op', { wrong: !op.name })} |
| 41 | + key={`typeandpath-op-${idx}-${op.trigram}`} |
| 42 | + style={{ left: `${leftMargin}rem` }} |
| 43 | + > |
| 44 | + {op.name ? op.name : <GoAlert />} |
| 45 | + </div> |
| 46 | + ) |
| 47 | + ); |
| 48 | + })} |
| 49 | + </div> |
| 50 | + ); |
| 51 | +} |
| 52 | + |
| 53 | +export default function TypeAndPath({ zoomToFeature }: PathfindingProps) { |
| 54 | + const dispatch = useDispatch(); |
| 55 | + const [inputText, setInputText] = useState<string>(''); |
| 56 | + const [opList, setOpList] = useState<SearchResultItemOperationalPoint[]>([]); |
| 57 | + const infraId = useSelector(getInfraID); |
| 58 | + const rollingStockId = useSelector(getRollingStockID); |
| 59 | + const [postSearch] = osrdEditoastApi.usePostSearchMutation(); |
| 60 | + const [postPathfinding] = osrdEditoastApi.usePostPathfindingMutation(); |
| 61 | + const { t } = useTranslation('operationalStudies/manageTrainSchedule'); |
| 62 | + |
| 63 | + const debouncedInputText = useDebounce(inputText.trimEnd(), 500); |
| 64 | + |
| 65 | + const handleInput = (text: string) => { |
| 66 | + // setInputText(text.trimStart().toUpperCase()); |
| 67 | + setInputText(text.trimStart()); |
| 68 | + }; |
| 69 | + |
| 70 | + function getOpNames() { |
| 71 | + if (infraId !== undefined) { |
| 72 | + const opTrigrams = inputText.toUpperCase().split(' '); |
| 73 | + const constraint = opTrigrams.reduce( |
| 74 | + (res, trigram) => [...res, ['=', ['trigram'], trigram]], |
| 75 | + ['or'] as (string | SearchConstraintType)[] |
| 76 | + ); |
| 77 | + const limitToMainStationConstraint = [ |
| 78 | + 'or', |
| 79 | + ['=', ['ch'], ''], |
| 80 | + ['=', ['ch'], 'BV'], |
| 81 | + ['=', ['ch'], '00'], |
| 82 | + ]; |
| 83 | + const payload: PostSearchApiArg = { |
| 84 | + searchPayload: { |
| 85 | + object: 'operationalpoint', |
| 86 | + query: ['and', constraint, ['=', ['infra_id'], infraId], limitToMainStationConstraint], |
| 87 | + }, |
| 88 | + pageSize: 1000, |
| 89 | + }; |
| 90 | + postSearch(payload) |
| 91 | + .unwrap() |
| 92 | + .then((results) => { |
| 93 | + const operationalPoints = [...results] as SearchResultItemOperationalPoint[]; |
| 94 | + setOpList( |
| 95 | + opTrigrams.map( |
| 96 | + (trigram) => operationalPoints.find((op) => op.trigram === trigram) || { trigram } |
| 97 | + ) as SearchResultItemOperationalPoint[] |
| 98 | + ); |
| 99 | + }); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + const isInvalid = !opList.every((op) => op.name || op.trigram === ''); |
| 104 | + |
| 105 | + function launchPathFinding() { |
| 106 | + if (infraId && rollingStockId && opList.length > 0) { |
| 107 | + const params = { |
| 108 | + infra: infraId, |
| 109 | + rolling_stocks: [rollingStockId], |
| 110 | + steps: opList |
| 111 | + .filter((op) => op.trigram !== '') |
| 112 | + .map((op) => ({ |
| 113 | + duration: 0, |
| 114 | + waypoints: op.track_sections.map((position) => ({ |
| 115 | + track_section: position.track, |
| 116 | + offset: position.position, |
| 117 | + })), |
| 118 | + })), |
| 119 | + }; |
| 120 | + postPathfinding({ pathQuery: params }) |
| 121 | + .unwrap() |
| 122 | + .then((itineraryCreated: Path) => { |
| 123 | + zoomToFeature(bbox(itineraryCreated.geographic)); |
| 124 | + loadPathFinding(itineraryCreated, dispatch); |
| 125 | + }) |
| 126 | + .catch((e) => { |
| 127 | + dispatch( |
| 128 | + setFailure({ |
| 129 | + name: e.data.name, |
| 130 | + message: e.data.message, |
| 131 | + }) |
| 132 | + ); |
| 133 | + }); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + useEffect(() => { |
| 138 | + if (debouncedInputText !== '') { |
| 139 | + getOpNames(); |
| 140 | + } else { |
| 141 | + setOpList([]); |
| 142 | + } |
| 143 | + }, [debouncedInputText]); |
| 144 | + |
| 145 | + return ( |
| 146 | + <div |
| 147 | + className="type-and-path" |
| 148 | + style={{ minWidth: `${monospaceOneCharREMWidth * inputText.length + 5.5}rem` }} // To grow input field & whole div along text size |
| 149 | + > |
| 150 | + <div className="help">{opList.length === 0 && t('inputOPTrigrams')}</div> |
| 151 | + <OpTooltips opList={opList} /> |
| 152 | + <div className="d-flex align-items-center"> |
| 153 | + <div |
| 154 | + className={cx('form-control-container', 'flex-grow-1', 'mr-2', { |
| 155 | + 'is-invalid': isInvalid, |
| 156 | + })} |
| 157 | + > |
| 158 | + <input |
| 159 | + className="form-control form-control-sm text-zone" |
| 160 | + type="text" |
| 161 | + value={inputText} |
| 162 | + onChange={(e) => handleInput(e.target.value)} |
| 163 | + placeholder={t('inputOPTrigramsExample')} |
| 164 | + autoFocus |
| 165 | + /> |
| 166 | + <span className="form-control-state" /> |
| 167 | + </div> |
| 168 | + <button |
| 169 | + className="btn btn-sm btn-success" |
| 170 | + type="button" |
| 171 | + onClick={launchPathFinding} |
| 172 | + disabled={isInvalid} |
| 173 | + > |
| 174 | + <GoTriangleRight /> |
| 175 | + </button> |
| 176 | + </div> |
| 177 | + </div> |
| 178 | + ); |
| 179 | +} |
0 commit comments