-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathMissionRestartButton.tsx
More file actions
144 lines (133 loc) · 5.47 KB
/
Copy pathMissionRestartButton.tsx
File metadata and controls
144 lines (133 loc) · 5.47 KB
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
import { Button, EdsProvider, Icon, Menu } from '@equinor/eds-core-react'
import { Icons } from 'utils/icons'
import { useNavigate } from 'react-router'
import { useLanguageContext } from 'components/Contexts/LanguageContext'
import styled from 'styled-components'
import { useContext, useState } from 'react'
import { AlertType, useAlertContext } from 'components/Contexts/AlertContext'
import { FailedRequestAlertContent, FailedRequestAlertListContent } from 'components/Alerts/FailedRequestAlert'
import { Mission } from 'models/Mission'
import { AlertCategory } from 'components/Alerts/AlertsBanner'
import { InspectionAreaVerificationDialog } from '../InspectionAreaVerificationDialogs/InspectionAreaVerificationDialog'
import {
getInspectionAreaDialogType,
InspectionAreaDialogType,
} from '../InspectionAreaVerificationDialogs/getInspectionAreaDialogType'
import { useBackendApi } from 'api/UseBackendApi'
import { InstallationContext } from 'components/Contexts/InstallationContext'
import { useAssetContext } from 'components/Contexts/AssetContext'
const Centered = styled.div`
display: flex;
align-content: center;
align-items: center;
justify-content: center;
`
const StyledButton = styled(Button)`
background-color: none;
height: 36px;
`
interface MissionProps {
mission: Mission
hasFailedTasks: boolean
smallButton: boolean
}
enum ReRunOptions {
ReRun,
ReRunFailed,
}
export const MissionRestartButton = ({ mission, hasFailedTasks, smallButton }: MissionProps) => {
const { TranslateText } = useLanguageContext()
const { installation } = useContext(InstallationContext)
const { enabledRobots } = useAssetContext()
const { setAlert, setListAlert } = useAlertContext()
const [isOpen, setIsOpen] = useState<boolean>(false)
const [isLocationVerificationOpen, setIsLocationVerificationOpen] = useState<boolean>(false)
const [verificationDialogType, setVerificationDialogType] = useState<InspectionAreaDialogType | null>(null)
const [anchorEl, setAnchorEl] = useState<HTMLButtonElement | null>(null)
const liveRobot = enabledRobots.find((robot) => robot.id === mission.robot.id)
const navigate = useNavigate()
const navigateToHome = () => {
const path = `/${installation.installationCode}`
navigate(path)
}
const backendApi = useBackendApi()
const startReRun = (option: ReRunOptions) => {
backendApi
.reRunMission(mission.id, option === ReRunOptions.ReRunFailed)
.then(() => navigateToHome())
.catch(() => {
setAlert(
AlertType.RequestFail,
<FailedRequestAlertContent translatedMessage={TranslateText('Failed to rerun mission')} />,
AlertCategory.ERROR
)
setListAlert(
AlertType.RequestFail,
<FailedRequestAlertListContent translatedMessage={TranslateText('Failed to rerun mission')} />,
AlertCategory.ERROR
)
})
setIsLocationVerificationOpen(false)
}
const selectRerunOption = (rerunOption: ReRunOptions) => {
const dialogType = getInspectionAreaDialogType(liveRobot, [mission.inspectionArea])
if (dialogType === null) {
startReRun(rerunOption)
return
}
setVerificationDialogType(dialogType)
setIsLocationVerificationOpen(true)
}
return (
<Centered>
<StyledButton
variant={smallButton ? 'ghost_icon' : 'outlined'}
ref={setAnchorEl}
id="anchor-default"
aria-haspopup="true"
aria-expanded={isOpen}
aria-controls="menu-default"
onClick={() => {
return hasFailedTasks ? setIsOpen(!isOpen) : selectRerunOption(ReRunOptions.ReRun)
}}
>
<Icon name={Icons.Add} size={24} />
{!smallButton && TranslateText('Queue mission')}
</StyledButton>
<EdsProvider density="compact">
<Menu
open={isOpen}
id="menu-default"
aria-labelledby="anchor-default"
onClose={() => setIsOpen(false)}
anchorEl={anchorEl}
>
<Menu.Item
onClick={() => {
selectRerunOption(ReRunOptions.ReRun)
}}
>
{TranslateText('Rerun full mission')}
</Menu.Item>
{hasFailedTasks && (
<Menu.Item
onClick={() => {
selectRerunOption(ReRunOptions.ReRunFailed)
}}
>
{TranslateText('Rerun failed and cancelled tasks in the mission')}
</Menu.Item>
)}
</Menu>
</EdsProvider>
{isLocationVerificationOpen && verificationDialogType !== null && (
<InspectionAreaVerificationDialog
dialogType={verificationDialogType}
closeDialog={() => setIsLocationVerificationOpen(false)}
robot={liveRobot}
missionInspectionAreas={[mission.inspectionArea]}
/>
)}
</Centered>
)
}