-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathselectors.ts
61 lines (55 loc) · 2.96 KB
/
selectors.ts
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
import { compact } from 'lodash';
import { createSelector } from 'reselect';
import type { RootState } from 'reducers';
import buildInfraStateSelectors from 'reducers/infra/selectors';
import type { OperationalStudiesConfSlice } from 'reducers/osrdconf/operationalStudiesConf';
import type { StdcmConfSlice } from 'reducers/osrdconf/stdcmConf';
import { makeSubSelector } from 'utils/selectors';
const buildCommonConfSelectors = (slice: OperationalStudiesConfSlice | StdcmConfSlice) => {
const makeOsrdConfSelector = makeSubSelector((state: RootState) => state[slice.name]);
const getPathSteps = makeOsrdConfSelector('pathSteps');
// If createSelector is not used and we return directly : pathSteps.slice(1, -1), we get this rtk warning :
// Selector getVias returned a different result when called with the same parameters. This can lead to unnecessary rerenders.
// Selectors that return a new reference (such as an object or an array) should be memoized: https://redux.js.org/usage/deriving-data-selectors#optimizing-selectors-with-memoization
const viasSelector = createSelector(
getPathSteps,
(pathSteps) => compact(pathSteps.slice(1, -1)) // a via can't be null
);
return {
...buildInfraStateSelectors(slice),
getConstraintDistribution: makeOsrdConfSelector('constraintDistribution'),
getName: makeOsrdConfSelector('name'),
getTrainCount: makeOsrdConfSelector('trainCount'),
getTrainDelta: makeOsrdConfSelector('trainDelta'),
getTrainStep: makeOsrdConfSelector('trainStep'),
getUsingElectricalProfiles: makeOsrdConfSelector('usingElectricalProfiles'),
getLabels: makeOsrdConfSelector('labels'),
getProjectID: makeOsrdConfSelector('projectID'),
getStudyID: makeOsrdConfSelector('studyID'),
getScenarioID: makeOsrdConfSelector('scenarioID'),
getTimetableID: makeOsrdConfSelector('timetableID'),
getElectricalProfileSetId: makeOsrdConfSelector('electricalProfileSetId'),
getWorkScheduleGroupId: makeOsrdConfSelector('workScheduleGroupId'),
getSearchDatetimeWindow: makeOsrdConfSelector('searchDatetimeWindow'),
getRollingStockID: makeOsrdConfSelector('rollingStockID'),
getSpeedLimitByTag: makeOsrdConfSelector('speedLimitByTag'),
getInitialSpeed: makeOsrdConfSelector('initialSpeed'),
getGridMarginBefore: makeOsrdConfSelector('gridMarginBefore'),
getGridMarginAfter: makeOsrdConfSelector('gridMarginAfter'),
getPowerRestriction: makeOsrdConfSelector('powerRestriction'),
getPathSteps,
getOrigin: (state: RootState) => {
const pathSteps = getPathSteps(state);
return pathSteps[0];
},
getDestination: (state: RootState) => {
const pathSteps = getPathSteps(state);
return pathSteps[pathSteps.length - 1];
},
/** To use this action, do useSelector(getVias()) */
getVias: () => viasSelector,
getRollingStockComfort: makeOsrdConfSelector('rollingStockComfort'),
getStartTime: makeOsrdConfSelector('startTime'),
};
};
export default buildCommonConfSelectors;