-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathStdcmVias.tsx
185 lines (170 loc) · 6.45 KB
/
StdcmVias.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import { useEffect, useMemo, useState } from 'react';
import { Location } from '@osrd-project/ui-icons';
import { compact } from 'lodash';
import { useTranslation } from 'react-i18next';
import nextId from 'react-id-generator';
import { useSelector } from 'react-redux';
import IntermediatePointIcon from 'assets/pictures/mapMarkers/intermediate-point.svg';
import { useOsrdConfSelectors, useOsrdConfActions } from 'common/osrdContext';
import type { StdcmConfSliceActions } from 'reducers/osrdconf/stdcmConf';
import type { PathStep } from 'reducers/osrdconf/types';
import { useAppDispatch } from 'store';
import { addElementAtIndex, replaceElementAtIndex } from 'utils/array';
import { formatDurationAsISO8601 } from 'utils/timeManipulation';
import StdcmCard from './StdcmCard';
import StdcmDefaultCard from './StdcmDefaultCard';
import StdcmInputVia from './StdcmInputVia';
import StdcmOperationalPoint from './StdcmOperationalPoint';
import StdcmStopType from './StdcmStopType';
import { StdcmStopTypes } from '../../types';
import type { StdcmConfigCardProps } from '../../types';
const generateUniqueId = (idsList: string[]): string => {
let id;
do {
id = nextId();
} while (idsList.includes(id));
return id;
};
const StdcmVias = ({ disabled = false }: StdcmConfigCardProps) => {
const { t } = useTranslation('stdcm');
const dispatch = useAppDispatch();
const { getPathSteps } = useOsrdConfSelectors();
const { deleteVia, updatePathSteps, updateViaStopTime } =
useOsrdConfActions() as StdcmConfSliceActions;
const pathSteps = useSelector(getPathSteps);
const [stopTypes, setStopTypes] = useState<Record<string, StdcmStopTypes>>(
compact(pathSteps).reduce(
(acc, cur) => {
acc[cur.id] = cur.stopType || StdcmStopTypes.PASSAGE_TIME;
return acc;
},
{} as Record<string, StdcmStopTypes>
)
);
const intermediatePoints = useMemo(() => pathSteps.slice(1, -1), [pathSteps]);
const updatePathStepsList = (pathStep: PathStep | null, index: number) => {
if (!pathStep) return;
const newPathSteps = replaceElementAtIndex(pathSteps, index, pathStep);
dispatch(updatePathSteps({ pathSteps: newPathSteps }));
};
const updatePathStepStopTime = (stopTime: string, index: number, pathStepId: string) => {
const pathStepToUpdate = pathSteps[index];
if (!pathStepToUpdate) return;
dispatch(
updateViaStopTime({
via: pathStepToUpdate,
duration: formatDurationAsISO8601(Number(stopTime) * 60),
stopType: stopTypes[pathStepId],
})
);
};
const updateStopType = (newStopType: StdcmStopTypes, index: number, pathStepId: string) => {
setStopTypes((prevStopTypes) => ({
...prevStopTypes,
[pathStepId]: newStopType,
}));
const defaultStopTime = newStopType === StdcmStopTypes.DRIVER_SWITCH ? '3' : '';
updatePathStepStopTime(defaultStopTime, index, pathStepId);
};
const deleteViaOnClick = (index: number, pathStepId: string) => {
setStopTypes((prevStopTypes) => {
delete prevStopTypes[pathStepId];
return prevStopTypes;
});
dispatch(deleteVia(index));
};
const addViaOnClick = (pathStepIndex: number) => {
const newPathStepId = generateUniqueId(pathSteps.map((pathStep) => pathStep?.id || ''));
const newPathSteps = addElementAtIndex(pathSteps, pathStepIndex, {
id: newPathStepId,
uic: -1,
});
setStopTypes((prevStopTypes) => ({
...prevStopTypes,
[newPathStepId]: StdcmStopTypes.PASSAGE_TIME,
}));
dispatch(updatePathSteps({ pathSteps: newPathSteps }));
};
useEffect(() => {
pathSteps.forEach((pathStep, index) => {
if (pathStep) {
const stopType = stopTypes[pathStep.id];
if (stopType && pathStep.stopType !== stopType) {
const updatedPathStep = {
...pathStep,
stopType,
};
updatePathStepsList(updatedPathStep, index);
}
}
});
}, [stopTypes, pathSteps]);
return (
<div className="stdcm-vias-list">
{intermediatePoints.length > 0 &&
compact(intermediatePoints).map((pathStep, index) => {
const pathStepIndex = index + 1;
return (
<div className="stdcm-vias-bundle" key={pathStep.id}>
<StdcmDefaultCard
hasTip
text={t('trainPath.addVia')}
Icon={<Location size="lg" variant="base" />}
onClick={() => addViaOnClick(pathStepIndex)}
disabled={disabled}
/>
<StdcmCard
name={t('trainPath.vias')}
title={
<div className="stdcm-via-icons">
<div className="icon-bundle mt-1">
<img src={IntermediatePointIcon} alt="intermediate-point" />
<span className="icon-index">{pathStepIndex}</span>
</div>
<button type="button" onClick={() => deleteViaOnClick(index, pathStep.id)}>
{t('translation:common.delete')}
</button>
</div>
}
hasTip
disabled={disabled}
className="via"
>
<StdcmOperationalPoint
updatePoint={(e) => updatePathStepsList(e, pathStepIndex)}
point={pathStep}
opPointId={pathStep.id}
disabled={disabled}
/>
{'uic' in pathStep && pathStep.uic !== -1 && (
<>
<StdcmStopType
stopTypes={stopTypes[pathStep.id]}
updatePathStepStopType={(newStopType) =>
updateStopType(newStopType, pathStepIndex, pathStep.id)
}
/>
<StdcmInputVia
stopType={stopTypes[pathStep.id]}
stopDuration={pathStep.stopFor}
updatePathStepStopTime={(e) =>
updatePathStepStopTime(e, pathStepIndex, pathStep.id)
}
/>
</>
)}
</StdcmCard>
</div>
);
})}
<StdcmDefaultCard
hasTip
text={t('trainPath.addVia')}
Icon={<Location size="lg" variant="base" />}
onClick={() => addViaOnClick(pathSteps.length - 1)}
disabled={disabled}
/>
</div>
);
};
export default StdcmVias;