-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
front: integrate api call and refacto component
Signed-off-by: Achraf Mohyeddine <[email protected]>
- Loading branch information
1 parent
bb21d2e
commit faaff8a
Showing
6 changed files
with
162 additions
and
4,798 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import { useEffect, useState } from 'react'; | ||
|
||
import type { TFunction } from 'i18next'; | ||
|
||
import type { ManageTrainSchedulePathProperties } from 'applications/operationalStudies/types'; | ||
import { processConflicts } from 'applications/stdcm/utils/fomatConflicts'; | ||
import type { Conflict } from 'common/api/osrdEditoastApi'; | ||
|
||
const useConflictsMessages = ( | ||
t: TFunction, | ||
conflictData?: Conflict[], | ||
pathProperties?: ManageTrainSchedulePathProperties | ||
) => { | ||
const [trackConflicts, setTrackConflicts] = useState<string[]>([]); | ||
const [workConflicts, setWorkConflicts] = useState<string[]>([]); | ||
|
||
useEffect(() => { | ||
if (!pathProperties || !conflictData) return; | ||
|
||
const generateConflictMessages = () => { | ||
const processedConflictsData = processConflicts(conflictData, pathProperties); | ||
|
||
const trackConflictsData = processedConflictsData.filter( | ||
(conflict) => conflict.conflictType === 'Spacing' | ||
); | ||
|
||
const workConflictsData = processedConflictsData.filter( | ||
(conflict) => conflict.conflictType !== 'Spacing' | ||
); | ||
|
||
const trackMessages: string[] = []; | ||
trackConflictsData.slice(0, 2).forEach((conflict) => { | ||
const { waypointBefore, waypointAfter, startDate, endDate, startTime, endTime } = conflict; | ||
|
||
if (startDate === endDate) { | ||
trackMessages.push( | ||
t('trackConflictSameDay', { | ||
waypointBefore, | ||
waypointAfter, | ||
startTime, | ||
endTime, | ||
startDate, | ||
}) | ||
); | ||
} else { | ||
trackMessages.push( | ||
t('trackConflict', { | ||
waypointBefore, | ||
waypointAfter, | ||
startDate, | ||
endDate, | ||
startTime, | ||
endTime, | ||
}) | ||
); | ||
} | ||
}); | ||
|
||
const remainingTrackConflicts = trackConflictsData.length - 2; | ||
if (remainingTrackConflicts > 0) { | ||
trackMessages.push(t('remainingTrackConflicts', { remainingTrackConflicts })); | ||
} | ||
|
||
const workMessages: string[] = []; | ||
workConflictsData.slice(0, 2).forEach((conflict) => { | ||
const { waypointBefore, waypointAfter, startDate, endDate, startTime, endTime } = conflict; | ||
|
||
if (startDate === endDate) { | ||
workMessages.push( | ||
t('workConflictSameDay', { | ||
waypointBefore, | ||
waypointAfter, | ||
startDate, | ||
startTime, | ||
endTime, | ||
}) | ||
); | ||
} else { | ||
workMessages.push( | ||
t('workConflict', { | ||
waypointBefore, | ||
waypointAfter, | ||
startDate, | ||
startTime, | ||
endDate, | ||
endTime, | ||
}) | ||
); | ||
} | ||
}); | ||
|
||
const remainingWorkConflicts = workConflictsData.length - 2; | ||
if (remainingWorkConflicts > 0) { | ||
workMessages.push(t('remainingWorkConflicts', { remainingWorkConflicts })); | ||
} | ||
|
||
// Update the state with generated messages | ||
setTrackConflicts(trackMessages); | ||
setWorkConflicts(workMessages); | ||
}; | ||
|
||
generateConflictMessages(); | ||
}, [t, pathProperties, conflictData]); | ||
|
||
return { trackConflicts, workConflicts }; | ||
}; | ||
|
||
export default useConflictsMessages; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.