-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathAddRollingstockParam.tsx
105 lines (100 loc) · 3.16 KB
/
AddRollingstockParam.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
import SelectImprovedSNCF from 'common/BootstrapSNCF/SelectImprovedSNCF';
import { RollingStockComfortType } from 'common/api/osrdEditoastApi';
import { RollingStockSelectorParams } from 'modules/rollingStock/consts';
import React, { useState } from 'react';
import cx from 'classnames';
import { useTranslation } from 'react-i18next';
import { RiAddFill } from 'react-icons/ri';
import { compact } from 'lodash';
import { useModal } from 'common/BootstrapSNCF/ModalSNCF';
import PowerRestrictionGridModal from './PowerRestrictionGridModal';
export default function AddRollingstockParam({
listName,
allOptionsList,
displayedLists,
disabled,
updateDisplayedLists,
}: {
listName: string;
allOptionsList: string[] | (string | null)[];
displayedLists: RollingStockSelectorParams;
disabled?: boolean;
updateDisplayedLists: (arg: string) => void;
}) {
const COMFORT_LEVELS_KEY: keyof RollingStockSelectorParams = 'comfortLevels';
const TRACTION_MODES_KEY: keyof RollingStockSelectorParams = 'tractionModes';
const POWER_RESTRICTIONS_KEY: keyof RollingStockSelectorParams = 'powerRestrictions';
const { t } = useTranslation('rollingstock');
const { openModal } = useModal();
const [isSelectVisible, setIsSelectVisible] = useState(false);
const isTractionModes = listName === TRACTION_MODES_KEY;
const optionsList = compact(allOptionsList)
.filter(
(option) =>
!displayedLists[listName as keyof RollingStockSelectorParams].includes(
option as RollingStockComfortType
)
)
.map((option) => {
if (listName === COMFORT_LEVELS_KEY) {
return {
id: option,
label: t(`comfortTypes.${option}`),
};
}
return {
id: option,
label: t(option),
};
});
return listName !== POWER_RESTRICTIONS_KEY ? (
<div>
<button
type="button"
className={cx('rollingstock-selector-buttons', 'mb-2', {
disabled: disabled || (optionsList.length < 1 && listName !== 'tractionModes'),
})}
onClick={() => setIsSelectVisible(true)}
>
<RiAddFill />
</button>
{isSelectVisible && (
<div className="selector-select">
<SelectImprovedSNCF
options={optionsList}
onChange={(e) => {
if (e) {
updateDisplayedLists(e.id);
setIsSelectVisible(false);
}
}}
withSearch
withNewValueInput={isTractionModes}
addButtonTitle={isTractionModes ? t('addNewTractionMode') : undefined}
bgWhite
isOpened
setSelectVisibility={setIsSelectVisible}
noTogglingHeader
/>
</div>
)}
</div>
) : (
<button
type="button"
className="rollingstock-selector-buttons mb-2"
onClick={() =>
openModal(
<PowerRestrictionGridModal
powerRestrictionsList={allOptionsList as string[]}
updatePowerRestrictions={updateDisplayedLists}
currentPowerRestrictions={displayedLists.powerRestrictions}
/>,
'lg'
)
}
>
<RiAddFill />
</button>
);
}