-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathStudy.tsx
332 lines (312 loc) · 11.4 KB
/
Study.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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
import React, { useEffect, useMemo, useState } from 'react';
import { Pencil } from '@osrd-project/ui-icons';
import { useTranslation } from 'react-i18next';
import nextId from 'react-id-generator';
import { useParams } from 'react-router-dom';
import BreadCrumbs from 'applications/operationalStudies/components/BreadCrumbs';
import FilterTextField from 'applications/operationalStudies/components/FilterTextField';
import DateBox from 'applications/operationalStudies/components/Study/DateBox';
import StateStep from 'applications/operationalStudies/components/Study/StateStep';
import { type StudyState, studyStates } from 'applications/operationalStudies/consts';
import {
type PostSearchApiArg,
type ScenarioWithCountTrains,
osrdEditoastApi,
} from 'common/api/osrdEditoastApi';
import { useModal } from 'common/BootstrapSNCF/ModalSNCF';
import NavBarSNCF from 'common/BootstrapSNCF/NavBarSNCF';
import OptionsSNCF from 'common/BootstrapSNCF/OptionsSNCF';
import { Loader, Spinner } from 'common/Loaders';
import ScenarioCard from 'modules/scenario/components/ScenarioCard';
import ScenarioCardEmpty from 'modules/scenario/components/ScenarioCardEmpty';
import AddOrEditStudyModal from 'modules/study/components/AddOrEditStudyModal';
import { budgetFormat } from 'utils/numbers';
type SortOptions =
| 'NameAsc'
| 'NameDesc'
| 'CreationDateAsc'
| 'CreationDateDesc'
| 'LastModifiedAsc'
| 'LastModifiedDesc';
type studyParams = {
projectId: string;
studyId: string;
};
export default function Study() {
const { t } = useTranslation('operationalStudies/study');
const { openModal } = useModal();
const [scenariosList, setScenariosList] = useState<ScenarioWithCountTrains[]>([]);
const [filter, setFilter] = useState('');
const [filterChips, setFilterChips] = useState('');
const [sortOption, setSortOption] = useState<SortOptions>('LastModifiedDesc');
const { projectId: urlProjectId, studyId: urlStudyId } = useParams() as studyParams;
const [isLoading, setIsLoading] = useState(true);
const { projectId, studyId } = useMemo(
() => ({
projectId: !Number.isNaN(+urlProjectId) ? +urlProjectId : undefined,
studyId: !Number.isNaN(+urlStudyId) ? +urlStudyId : undefined,
}),
[urlStudyId, urlProjectId]
);
const {
data: study,
isError: isCurrentStudyError,
error: studyError,
} = osrdEditoastApi.useGetProjectsByProjectIdStudiesAndStudyIdQuery(
{
projectId: +projectId!,
studyId: +studyId!,
},
{
skip: !projectId || !studyId,
}
);
const [postSearch] = osrdEditoastApi.usePostSearchMutation();
const [getScenarios] =
osrdEditoastApi.useLazyGetProjectsByProjectIdStudiesAndStudyIdScenariosQuery();
useEffect(() => {
if (!projectId || !studyId) throw new Error('Missing projectId or studyId in url');
}, [projectId, studyId]);
useEffect(() => {
if (isCurrentStudyError && studyError) throw studyError;
}, [isCurrentStudyError, studyError]);
const sortOptions = [
{
label: t('sortOptions.byName'),
value: 'NameAsc',
},
{
label: t('sortOptions.byRecentDate'),
value: 'LastModifiedDesc',
},
];
const handleSortOptions = (e: React.ChangeEvent<HTMLInputElement>) => {
setSortOption(e.target.value as SortOptions);
};
const getScenarioList = async () => {
setIsLoading(true);
if (projectId && studyId) {
if (filter) {
const payload: PostSearchApiArg = {
pageSize: 1000,
searchPayload: {
object: 'scenario',
query: [
'and',
[
'or',
['search', ['name'], filter],
['search', ['description'], filter],
['search', ['tags'], filter],
],
['=', ['study_id'], studyId],
],
},
};
try {
let filteredScenarios = (await postSearch(payload).unwrap()) as ScenarioWithCountTrains[];
if (sortOption === 'LastModifiedDesc') {
filteredScenarios = [...filteredScenarios].sort((a, b) =>
b.last_modification.localeCompare(a.last_modification)
);
} else if (sortOption === 'NameAsc') {
filteredScenarios = [...filteredScenarios].sort((a, b) => a.name.localeCompare(b.name));
}
setScenariosList(filteredScenarios as ScenarioWithCountTrains[]);
} catch (error) {
console.error(error);
} finally {
setIsLoading(false);
}
} else {
try {
const { data } = await getScenarios({
projectId: +projectId,
studyId: +studyId,
ordering: sortOption,
pageSize: 1000,
});
if (data?.results) setScenariosList(data.results);
} catch (error) {
console.error(error);
} finally {
setIsLoading(false);
}
}
}
};
function displayScenariosList() {
return !isLoading ? (
<div className="row no-gutters">
<div className="col-hdp-3 col-hd-4 col-lg-6">
<ScenarioCardEmpty />
</div>
{scenariosList.map((scenario) => (
<div
className="col-hdp-3 col-hd-4 col-lg-6"
key={`study-displayScenariosList-${scenario.id}`}
>
<ScenarioCard scenario={scenario} setFilterChips={setFilterChips} />
</div>
))}
</div>
) : (
<span className="mt-5 text-center">
<Spinner displayDelay={500} />
</span>
);
}
useEffect(() => {
if (studyId) getScenarioList();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [sortOption, filter]);
return (
<>
<NavBarSNCF appName={<BreadCrumbs project={study?.project} study={study} />} />
<main className="mastcontainer mastcontainer-no-mastnav">
<div className="p-3 study-view">
{study ? (
<div className="study-details">
<div className="study-details-dates">
<DateBox
date={study.creation_date ? new Date(study.creation_date) : null}
className="creation"
translation="creation"
/>
<DateBox
date={study.start_date ? new Date(study.start_date) : null}
className="start"
translation="start"
withoutTime
/>
<DateBox
date={study.expected_end_date ? new Date(study.expected_end_date) : null}
className="estimatedend"
translation="estimatedend"
withoutTime
/>
<DateBox
date={study.actual_end_date ? new Date(study.actual_end_date) : null}
className="realend"
translation="realend"
withoutTime
/>
<DateBox
date={study.last_modification ? new Date(study.last_modification) : null}
className="modified"
translation="modified"
/>
</div>
<div className="d-flex flex-column p-2">
<div className="study-details-name">
<div className="study-name">{study.name}</div>
<button
className="study-details-modify-button"
type="button"
onClick={() =>
openModal(
<AddOrEditStudyModal editionMode study={study} />,
'xl',
'no-close-modal'
)
}
>
<span className="study-details-modify-button-text">{t('modifyStudy')}</span>
<Pencil />
</button>
</div>
{study.study_type && (
<div className="study-details-type">
{t(`studyCategories.${study.study_type}`)}
</div>
)}
<div className="study-details-description">{study.description}</div>
{study.state && (
<div className="study-details-state">
{studyStates.map(
(state, idx) =>
study.project.id &&
study.id &&
study.state && (
<StateStep
key={nextId()}
projectID={study.project.id}
studyID={study.id}
number={idx + 1}
studyName={study.name}
state={state}
done={idx <= studyStates.indexOf(study.state as StudyState)}
tags={study.tags}
/>
)
)}
</div>
)}
</div>
{(study.service_code ||
study.business_code ||
(study.budget !== 0 && study.budget !== null)) && (
<div className="study-details-financials">
<div className="study-details-financials-infos">
{study.service_code && (
<div className="study-details-financials-infos-item">
<h3>{t('geremiCode')}</h3>
<div className="code">{study.service_code}</div>
</div>
)}
{study.business_code && (
<div className="study-details-financials-infos-item">
<h3>{t('affairCode')}</h3>
<div className="code">{study.business_code}</div>
</div>
)}
</div>
{study.budget ? (
<div className="study-details-financials-amount">
<span className="study-details-financials-amount-text">{t('budget')}</span>
{budgetFormat(study.budget)}
</div>
) : null}
</div>
)}
<div className="study-details-footer">
<div className="study-details-tags">
{study.tags?.map((tag) => (
<div className="study-details-tags-tag" key={nextId()}>
{tag}
</div>
))}
</div>
</div>
</div>
) : (
<span className="mt-5">
<Loader position="center" />
</span>
)}
<div className="scenarios-toolbar">
<div>{t('scenariosCount', { count: scenariosList.length })}</div>
<div className="flex-grow-1">
<FilterTextField
setFilter={setFilter}
filterChips={filterChips}
id="scenarios-filter"
sm
/>
</div>
<OptionsSNCF
name="projects-sort-filter"
onChange={handleSortOptions}
selectedValue={sortOption}
options={sortOptions}
sm
/>
</div>
<div className="scenarios-list">
{useMemo(() => displayScenariosList(), [scenariosList])}
</div>
</div>
</main>
</>
);
}