-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #500 from MTES-MCT/499-front-completeforstats-and-…
…status feat(mission): compute status and complete for stats (front)
- Loading branch information
Showing
25 changed files
with
265 additions
and
106 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
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
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,52 @@ | ||
import {} from '@common/types/env-mission-types' | ||
import { CompletenessForStats, MissionStatusEnum } from '@common/types/mission-types' | ||
import { useStore } from '@tanstack/react-store' | ||
import { isAfter, isBefore, isSameDay, parseISO } from 'date-fns' | ||
import { isNil } from 'lodash' | ||
import { useEffect, useState } from 'react' | ||
import { store } from '../../../store' | ||
import { setMissionStatus } from '../../../store/slices/mission-reducer' | ||
import { CompletenessForStatsStatusEnum, Mission } from '../types/mission-types' | ||
|
||
interface MissionHook { | ||
status?: MissionStatusEnum | ||
exportRapportEnabled: boolean | ||
completeForStats?: CompletenessForStats | ||
} | ||
|
||
export function useMission(mission?: Mission): MissionHook { | ||
const [status, setStatus] = useState<MissionStatusEnum>() | ||
const [completeForStats, setCompleteForStats] = useState<CompletenessForStats>() | ||
const timelieCompleteForStats = useStore(store, state => state.timeline.completnessForStats) | ||
const exportRapportEnabled = completeForStats?.status === CompletenessForStatsStatusEnum.COMPLETE | ||
|
||
useEffect(() => { | ||
if (!mission) return | ||
const status = computeMissionStatus(mission.startDateTimeUtc, mission.endDateTimeUtc) | ||
setStatus(status) | ||
setMissionStatus(status) | ||
}, [mission]) | ||
|
||
useEffect(() => { | ||
setCompleteForStats({ | ||
status: timelieCompleteForStats?.status, //TODO: compute with generalIformation | ||
sources: timelieCompleteForStats?.sources ////TODO: compute with generalIformation | ||
}) | ||
}, [timelieCompleteForStats]) | ||
|
||
const computeMissionStatus = (startDate: string, endDate?: string) => { | ||
const today = new Date() | ||
if (isNil(endDate) || isNil(startDate)) return MissionStatusEnum.UNAVAILABLE | ||
try { | ||
if (isAfter(parseISO(endDate), today)) return MissionStatusEnum.IN_PROGRESS | ||
if (isBefore(parseISO(endDate), today) || isSameDay(parseISO(endDate), today)) return MissionStatusEnum.ENDED | ||
if (isAfter(parseISO(startDate), today) || isSameDay(parseISO(startDate), today)) | ||
return MissionStatusEnum.UPCOMING | ||
} catch (e) { | ||
console.error(`calculateMissionStatus - error with startDate: ${startDate}, endDate: ${endDate}`, e) | ||
} | ||
return MissionStatusEnum.UNAVAILABLE | ||
} | ||
|
||
return { status, completeForStats, exportRapportEnabled } | ||
} |
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,15 @@ | ||
import { skipToken, useQuery } from '@tanstack/react-query' | ||
import axios from '../../../../query-client/axios' | ||
import { Mission } from '../types/mission-types' | ||
|
||
const useGetMissionQuery = (missionId?: string) => { | ||
const fetchMission = (): Promise<Mission> => axios.get(`missions/${missionId}`).then(response => response.data) | ||
|
||
const query = useQuery<Mission>({ | ||
queryKey: ['mission', missionId], | ||
queryFn: missionId ? fetchMission : skipToken | ||
}) | ||
return query | ||
} | ||
|
||
export default useGetMissionQuery |
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
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
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
17 changes: 17 additions & 0 deletions
17
frontend/src/v2/features/mission-timeline/hooks/use-timeline-complete-for-stats.tsx
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,17 @@ | ||
import { CompletenessForStatsStatusEnum, MissionSourceEnum } from '../../common/types/mission-types' | ||
import { MissionTimelineAction } from '../types/mission-timeline-output' | ||
|
||
export function useTimelineCompleteForStats() { | ||
const computeCompleteForStats = (actions: MissionTimelineAction[]) => { | ||
const sources = new Set<MissionSourceEnum>() | ||
|
||
actions.forEach(action => { | ||
if (!action.isCompleteForStats && action.source) sources.add(action.source) | ||
}) | ||
return { | ||
sources: Array.from(sources), | ||
status: sources.size === 0 ? CompletenessForStatsStatusEnum.COMPLETE : CompletenessForStatsStatusEnum.INCOMPLETE | ||
} | ||
} | ||
return { computeCompleteForStats } | ||
} |
Oops, something went wrong.