-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathutils.ts
174 lines (164 loc) · 4.92 KB
/
utils.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
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
import { isNil, omit } from 'lodash';
import {
osrdEditoastApi,
type MacroNodeResponse,
type PathItemLocation,
type SearchResultItemOperationalPoint,
} from 'common/api/osrdEditoastApi';
import type { AppDispatch } from 'store';
import { DEFAULT_TRAINRUN_FREQUENCIES } from './consts';
import type MacroEditorState from './MacroEditorState';
import type { NodeIndexed } from './MacroEditorState';
export const findOpFromPathItem = (
pathItem: PathItemLocation,
searchResults: SearchResultItemOperationalPoint[]
) => {
// When a path item doesn't specify a secondary code, mimick what editoast
// does: pick 'BV', '00' or an OP without a ch.
let chs: (string | null)[] = [];
if ('uic' in pathItem || 'trigram' in pathItem) {
if (pathItem.secondary_code) {
chs = [pathItem.secondary_code];
} else {
chs = ['BV', '00', null];
}
}
return searchResults.find((searchResult) => {
if ('uic' in pathItem) {
return searchResult.uic === pathItem.uic && chs.includes(searchResult.ch);
}
if ('trigram' in pathItem) {
return searchResult.trigram === pathItem.trigram && chs.includes(searchResult.ch);
}
if ('operational_point' in pathItem) {
return searchResult.obj_id === pathItem.operational_point;
}
return false;
});
};
export const createMacroNode = async (
state: MacroEditorState,
dispatch: AppDispatch,
node: Omit<MacroNodeResponse, 'id'>,
ngeNodeId: number
) => {
try {
const createPromise = dispatch(
osrdEditoastApi.endpoints.postProjectsByProjectIdStudiesAndStudyIdScenariosScenarioIdMacroNodes.initiate(
{
projectId: state.scenario.project.id,
studyId: state.scenario.study_id,
scenarioId: state.scenario.id,
macroNodeForm: node,
}
)
);
const newNode = await createPromise.unwrap();
state.indexNodeByKey(newNode.path_item_key, {
...omit(newNode, ['id']),
ngeId: ngeNodeId,
dbId: newNode.id,
});
} catch (e) {
console.error(e);
}
};
export const updateMacroNode = async (
state: MacroEditorState,
dispatch: AppDispatch,
node: NodeIndexed
) => {
try {
const indexedNode = state.getNodeByNgeId(node.ngeId);
if (!indexedNode) throw new Error(`Node ${node.ngeId} not found`);
if (!indexedNode.dbId) throw new Error(`Node ${node.ngeId} is not saved in the DB`);
await dispatch(
osrdEditoastApi.endpoints.putProjectsByProjectIdStudiesAndStudyIdScenariosScenarioIdMacroNodesNodeId.initiate(
{
projectId: state.scenario.project.id,
studyId: state.scenario.study_id,
scenarioId: state.scenario.id,
nodeId: indexedNode.dbId,
macroNodeForm: node,
}
)
);
state.indexNodeByKey(indexedNode.path_item_key, node);
} catch (e) {
console.error(e);
}
};
export const deleteMacroNodeByDbId = async (
state: MacroEditorState,
dispatch: AppDispatch,
dbId: number
) => {
try {
await dispatch(
osrdEditoastApi.endpoints.deleteProjectsByProjectIdStudiesAndStudyIdScenariosScenarioIdMacroNodesNodeId.initiate(
{
projectId: state.scenario.project.id,
studyId: state.scenario.study_id,
scenarioId: state.scenario.id,
nodeId: dbId,
}
)
);
} catch (e) {
console.error(e);
}
};
export const deleteMacroNodeByNgeId = async (
state: MacroEditorState,
dispatch: AppDispatch,
ngeId: number
) => {
try {
const indexedNode = state.getNodeByNgeId(ngeId);
if (indexedNode?.dbId) await deleteMacroNodeByDbId(state, dispatch, indexedNode.dbId);
state.deleteNodeByNgeId(ngeId);
} catch (e) {
console.error(e);
}
};
/**
* Get nodes of the scenario that are saved in the DB.
*/
export const getSavedMacroNodes = async (
state: MacroEditorState,
dispatch: AppDispatch
): Promise<MacroNodeResponse[]> => {
const pageSize = 100;
let page = 1;
let reachEnd = false;
const result: MacroNodeResponse[] = [];
while (!reachEnd) {
const promise = dispatch(
osrdEditoastApi.endpoints.getProjectsByProjectIdStudiesAndStudyIdScenariosScenarioIdMacroNodes.initiate(
{
projectId: state.scenario.project.id,
studyId: state.scenario.study_id,
scenarioId: state.scenario.id,
pageSize,
page,
},
{ forceRefetch: true, subscribe: false }
)
);
// need to unsubscribe on get call to avoid cache issue
const { data } = await promise;
if (data) result.push(...data.results);
reachEnd = isNil(data?.next);
page += 1;
}
return result;
};
/**
* Match a frequency label to a NGE TrainrunFrequency, or `null` if not handled.
*/
export const trainrunFrequencyFromLabel = (label: string) => {
if (!label.startsWith('frequency::')) return null;
const n = parseInt(label.split('::', 2)[1], 10);
const frequency = DEFAULT_TRAINRUN_FREQUENCIES.find((freq) => freq.frequency === n);
return frequency ?? null;
};