-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathStdcmVias.tsx
123 lines (111 loc) · 4.32 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
import { useMemo } from 'react';
import { Location } from '@osrd-project/ui-icons';
import { useTranslation } from 'react-i18next';
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 { StdcmConfSelectors } from 'reducers/osrdconf/stdcmConf/selectors';
import type { StdcmPathStep } from 'reducers/osrdconf/types';
import { useAppDispatch } from 'store';
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 StdcmVias = ({ disabled = false }: StdcmConfigCardProps) => {
const { t } = useTranslation('stdcm');
const dispatch = useAppDispatch();
const { getStdcmPathSteps } = useOsrdConfSelectors() as StdcmConfSelectors;
const { updateStdcmPathStep, addStdcmVia, deleteStdcmVia } =
useOsrdConfActions() as StdcmConfSliceActions;
const pathSteps = useSelector(getStdcmPathSteps);
const intermediatePoints = useMemo(() => pathSteps.slice(1, -1), [pathSteps]);
const updateStopType = (newStopType: StdcmStopTypes, pathStep: StdcmPathStep) => {
const defaultStopTime = newStopType === StdcmStopTypes.DRIVER_SWITCH ? 3 : undefined;
dispatch(
updateStdcmPathStep({
id: pathStep.id,
updates: { stopType: newStopType, stopFor: defaultStopTime },
})
);
};
const updateStopDuration = (stopTime: string, pathStep: StdcmPathStep) => {
const stopFor = stopTime ? Number(stopTime) : undefined;
dispatch(
updateStdcmPathStep({
id: pathStep.id,
updates: { stopFor },
})
);
};
const deleteViaOnClick = (pathStepId: string) => {
dispatch(deleteStdcmVia(pathStepId));
};
const addViaOnClick = (pathStepIndex: number) => {
dispatch(addStdcmVia(pathStepIndex));
};
return (
<div className="stdcm-vias-list">
{intermediatePoints.map((pathStep, index) => {
if (!pathStep.isVia) return null;
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(pathStep.id)}>
{t('translation:common.delete')}
</button>
</div>
}
hasTip
disabled={disabled}
className="via"
>
{(!pathStep.location || 'uic' in pathStep.location) && (
<StdcmOperationalPoint
location={pathStep.location}
pathStepId={pathStep.id}
disabled={disabled}
/>
)}
<StdcmStopType
stopTypes={pathStep.stopType}
updatePathStepStopType={(newStopType) => updateStopType(newStopType, pathStep)}
/>
<StdcmInputVia
stopType={pathStep.stopType}
stopDuration={pathStep.stopFor}
updatePathStepStopTime={(e) => updateStopDuration(e, pathStep)}
/>
</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;