-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathRollingStockCardButtons.tsx
91 lines (80 loc) · 2.95 KB
/
RollingStockCardButtons.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
import React, { useContext, useEffect, useMemo, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { useSelector } from 'react-redux';
import type { RollingStockComfortType } from 'common/api/osrdEditoastApi';
import { ModalContext } from 'common/BootstrapSNCF/ModalSNCF/ModalProvider';
import OptionsSNCF from 'common/BootstrapSNCF/OptionsSNCF';
import type { Option } from 'common/BootstrapSNCF/OptionsSNCF';
import { useOsrdConfActions, useOsrdConfSelectors } from 'common/osrdContext';
import { comfort2pictogram } from 'modules/rollingStock/components/RollingStockSelector/RollingStockHelpers';
import { useAppDispatch } from 'store';
interface RollingStockCardButtonsProps {
id: number;
curvesComfortList: string[];
setOpenedRollingStockCardId: (openCardId: number | undefined) => void;
}
const RollingStockCardButtons = ({
id,
curvesComfortList,
setOpenedRollingStockCardId,
}: RollingStockCardButtonsProps) => {
const dispatch = useAppDispatch();
const { t } = useTranslation(['rollingstock']);
const { closeModal } = useContext(ModalContext);
const { getRollingStockComfort } = useOsrdConfSelectors();
const rollingStockComfort = useSelector(getRollingStockComfort);
const [comfort, setComfort] = useState('STANDARD');
const { updatePathfindingID, updateRollingStockComfort, updateRollingStockID } =
useOsrdConfActions();
const selectRollingStock = () => {
setOpenedRollingStockCardId(undefined);
dispatch(updateRollingStockComfort(comfort as RollingStockComfortType));
dispatch(updateRollingStockID(id));
dispatch(updatePathfindingID(undefined));
closeModal();
};
const comfortOptions = useMemo(() => {
const options: Option[] = [{ value: 'STANDARD', label: t('comfortTypes.STANDARD') }];
if (curvesComfortList.includes('HEATING')) {
options.push({
value: 'HEATING',
label: (
<span className="rollingstock-footer-button-with-picto">
{comfort2pictogram('HEATING')} {t('comfortTypes.HEATING')}
</span>
),
});
}
if (curvesComfortList.includes('AC')) {
options.push({
value: 'AC',
label: (
<span className="rollingstock-footer-button-with-picto">
{comfort2pictogram('AC')} {t('comfortTypes.AC')}
</span>
),
});
}
return options;
}, [curvesComfortList]);
useEffect(() => {
setComfort(rollingStockComfort);
}, [rollingStockComfort]);
return (
<div className="rollingstock-footer-buttons">
{curvesComfortList.length > 1 && (
<OptionsSNCF
onChange={(e) => setComfort(e.target.value)}
options={comfortOptions}
name="comfortChoice"
selectedValue={comfort}
sm
/>
)}
<button type="button" className="ml-2 btn btn-primary btn-sm" onClick={selectRollingStock}>
{t('selectRollingStock')}
</button>
</div>
);
};
export default RollingStockCardButtons;