Skip to content

Commit

Permalink
Add MissionListsContext
Browse files Browse the repository at this point in the history
  • Loading branch information
OleDrange committed Sep 20, 2023
1 parent ce205a5 commit 40022fe
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 134 deletions.
41 changes: 19 additions & 22 deletions frontend/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,28 @@ import { FlotillaSite } from 'components/Pages/FlotillaSite'
import { LanguageProvider } from 'components/Contexts/LanguageContext'
import { MissionControlProvider } from 'components/Contexts/MissionControlContext'
import { MissionFilterProvider } from 'components/Contexts/MissionFilterContext'
import { MissionQueueProvider } from 'components/Contexts/MissionQueueContext'
import { MissionOngoingProvider } from 'components/Contexts/MissionOngoingContext'
import { MissionsProvider } from 'components/Contexts/MissionListsContext'

function App() {
return (
<MissionQueueProvider>
<MissionOngoingProvider>
<LanguageProvider>
<MissionControlProvider>
<>
<UnauthenticatedTemplate>
<div className="sign-in-page">
<SignInPage></SignInPage>
</div>
</UnauthenticatedTemplate>
<AuthenticatedTemplate>
<MissionFilterProvider>
<FlotillaSite />
</MissionFilterProvider>
</AuthenticatedTemplate>
</>
</MissionControlProvider>
</LanguageProvider>
</MissionOngoingProvider>
</MissionQueueProvider>
<MissionsProvider>
<LanguageProvider>
<MissionControlProvider>
<>
<UnauthenticatedTemplate>
<div className="sign-in-page">
<SignInPage></SignInPage>
</div>
</UnauthenticatedTemplate>
<AuthenticatedTemplate>
<MissionFilterProvider>
<FlotillaSite />
</MissionFilterProvider>
</AuthenticatedTemplate>
</>
</MissionControlProvider>
</LanguageProvider>
</MissionsProvider>
)
}

Expand Down
70 changes: 70 additions & 0 deletions frontend/src/components/Contexts/MissionListsContext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { createContext, FC, useContext, useEffect, useState } from 'react'
import { Mission, MissionStatus } from 'models/Mission'
import { refreshInterval } from 'components/Pages/FrontPage/FrontPage'
import { BackendAPICaller } from 'api/ApiCaller'

interface IMissionsContext {
ongoingMissions: Mission[]
missionQueue: Mission[]
}

interface Props {
children: React.ReactNode
}

const defaultMissionsContext: IMissionsContext = {
ongoingMissions: [],
missionQueue: [],
}

export const MissionsContext = createContext<IMissionsContext>(defaultMissionsContext)

interface MissionsResult {
ongoingMissions: Mission[]
missionQueue: Mission[]
}

const fetchMissions = (params: {
statuses: MissionStatus[]
pageSize: number
orderBy: string
}): Promise<Mission[]> => {
return BackendAPICaller.getMissionRuns(params).then((response) => response.content)
}
export const useMissions = (refreshInterval: number): MissionsResult => {
const [ongoingMissions, setOngoingMissions] = useState<Mission[]>([])
const [missionQueue, setMissionQueue] = useState<Mission[]>([])

useEffect(() => {
const fetchAndUpdateMissions = async () => {
const ongoing = await fetchMissions({
statuses: [MissionStatus.Ongoing, MissionStatus.Paused],
pageSize: 100,
orderBy: 'StartTime desc',
})
setOngoingMissions(ongoing)

const queue = await fetchMissions({
statuses: [MissionStatus.Pending],
pageSize: 100,
orderBy: 'DesiredStartTime',
})
setMissionQueue(queue)
}

fetchAndUpdateMissions()

const id = setInterval(fetchAndUpdateMissions, refreshInterval)

return () => clearInterval(id)
}, [refreshInterval])

return { ongoingMissions, missionQueue }
}
export const MissionsProvider: FC<Props> = ({ children }) => {
const { ongoingMissions, missionQueue } = useMissions(refreshInterval)

return <MissionsContext.Provider value={{ ongoingMissions, missionQueue }}>{children}</MissionsContext.Provider>
}

export const useMissionsContext = () => useContext(MissionsContext)
47 changes: 0 additions & 47 deletions frontend/src/components/Contexts/MissionOngoingContext.tsx

This file was deleted.

52 changes: 0 additions & 52 deletions frontend/src/components/Contexts/MissionQueueContext.tsx

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Button, Card, Checkbox, Dialog, Icon, Typography, DotProgress } from '@equinor/eds-core-react'
import { config } from 'config'
import { tokens } from '@equinor/eds-tokens'
import { Mission } from 'models/Mission'
import { Mission, dummyMission } from 'models/Mission'
import styled from 'styled-components'
import { useNavigate } from 'react-router-dom'
import { useState } from 'react'
Expand Down Expand Up @@ -57,7 +57,7 @@ export function MissionQueueCard({ mission, onDeleteMission }: MissionQueueCardP
<HorizontalNonButtonContent onClick={routeChange}>
<Checkbox />

{mission.name === 'dummyMission' ? (
{mission === dummyMission ? (
<DotProgress size={64} color="primary" />
) : (
<Button variant="ghost" fullWidth>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ import { useLanguageContext } from 'components/Contexts/LanguageContext'
import { useInstallationContext } from 'components/Contexts/InstallationContext'
import { CreateMissionButton } from './CreateMissionButton'
import { EchoMissionDefinition } from 'models/MissionDefinition'
import { useMissionQueueContext } from 'components/Contexts/MissionQueueContext'
import { useMissionOngoingContext } from 'components/Contexts/MissionOngoingContext'
import { useMissionsContext } from 'components/Contexts/MissionListsContext'

const StyledMissionView = styled.div`
display: grid;
Expand Down Expand Up @@ -42,8 +41,7 @@ const mapEchoMissionToString = (missions: EchoMissionDefinition[]): Map<string,

export function MissionQueueView({ refreshInterval }: RefreshProps) {
const { TranslateText } = useLanguageContext()
const { missionQueue } = useMissionQueueContext()
const { missionOngoing } = useMissionOngoingContext()
const { missionQueue, ongoingMissions } = useMissionsContext()
const [loadingMissionNames, setLoadingMissionNames] = useState<Set<string>>(new Set())

const [selectedEchoMissions, setSelectedEchoMissions] = useState<EchoMissionDefinition[]>([])
Expand Down Expand Up @@ -129,10 +127,10 @@ export function MissionQueueView({ refreshInterval }: RefreshProps) {
setLoadingMissionNames((currentLoadingNames) => {
const updatedLoadingMissionIds = new Set(currentLoadingNames)
missionQueue.forEach((mission) => updatedLoadingMissionIds.delete(mission.name))
missionOngoing.forEach((mission) => updatedLoadingMissionIds.delete(mission.name))
ongoingMissions.forEach((mission) => updatedLoadingMissionIds.delete(mission.name))
return updatedLoadingMissionIds
})
}, [missionQueue, missionOngoing])
}, [missionQueue, ongoingMissions])

var missionQueueDisplay = missionQueue.map(function (mission, index) {
return <MissionQueueCard key={index} mission={mission} onDeleteMission={onDeleteMission} />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import { useLanguageContext } from 'components/Contexts/LanguageContext'
import { useNavigate } from 'react-router-dom'
import { config } from 'config'
import { Icons } from 'utils/icons'
import { useMissionOngoingContext } from 'components/Contexts/MissionOngoingContext'
/* import { useOngoingMissionsContext } from 'components/Contexts/OngoingMissionsContext' */
import { useMissionsContext } from 'components/Contexts/MissionListsContext'

const StyledOngoingMissionView = styled.div`
display: flex;
Expand All @@ -26,9 +27,9 @@ const ButtonStyle = styled.div`

export function OngoingMissionView({ refreshInterval }: RefreshProps) {
const { TranslateText } = useLanguageContext()
const { missionOngoing } = useMissionOngoingContext()
const { ongoingMissions } = useMissionsContext()

var OngoingMissions = missionOngoing.map(function (mission, index) {
var ongoingMissionscard = ongoingMissions.map(function (mission, index) {
return <OngoingMissionCard key={index} mission={mission} />
})
let navigate = useNavigate()
Expand All @@ -43,8 +44,8 @@ export function OngoingMissionView({ refreshInterval }: RefreshProps) {
{TranslateText('Ongoing Missions')}
</Typography>
<OngoingMissionSection>
{missionOngoing.length > 0 && OngoingMissions}
{missionOngoing.length === 0 && <NoOngoingMissionsPlaceholder />}
{ongoingMissions.length > 0 && ongoingMissionscard}
{ongoingMissions.length === 0 && <NoOngoingMissionsPlaceholder />}
</OngoingMissionSection>
<ButtonStyle>
<Button variant="outlined" onClick={routeChange}>
Expand Down

0 comments on commit 40022fe

Please sign in to comment.