1
+ import { makeStyles } from "@material-ui/core/styles"
1
2
import { useMachine } from "@xstate/react"
2
3
import { AlertBanner } from "components/AlertBanner/AlertBanner"
4
+ import { ConfirmDialog } from "components/Dialogs/ConfirmDialog/ConfirmDialog"
5
+ import { Margins } from "components/Margins/Margins"
3
6
import { scheduleToAutoStart } from "pages/WorkspaceSchedulePage/schedule"
4
7
import { ttlMsToAutoStop } from "pages/WorkspaceSchedulePage/ttl"
5
- import React , { useEffect , useState } from "react"
8
+ import React , { useEffect } from "react"
9
+ import { useTranslation } from "react-i18next"
6
10
import { Navigate , useNavigate , useParams } from "react-router-dom"
7
11
import { scheduleChanged } from "util/schedule"
8
12
import * as TypesGen from "../../api/typesGenerated"
@@ -15,14 +19,20 @@ import {
15
19
formValuesToTTLRequest ,
16
20
} from "./formToRequest"
17
21
18
- const Language = {
19
- forbiddenError :
20
- "You don't have permissions to update the schedule for this workspace." ,
21
- getWorkspaceError : "Failed to fetch workspace." ,
22
- checkPermissionsError : "Failed to fetch permissions." ,
23
- }
22
+ const getAutoStart = ( workspace ?: TypesGen . Workspace ) =>
23
+ scheduleToAutoStart ( workspace ?. autostart_schedule )
24
+ const getAutoStop = ( workspace ?: TypesGen . Workspace ) =>
25
+ ttlMsToAutoStop ( workspace ?. ttl_ms )
26
+
27
+ const useStyles = makeStyles ( ( theme ) => ( {
28
+ topMargin : {
29
+ marginTop : `${ theme . spacing ( 3 ) } px` ,
30
+ } ,
31
+ } ) )
24
32
25
33
export const WorkspaceSchedulePage : React . FC = ( ) => {
34
+ const { t } = useTranslation ( "workspaceSchedulePage" )
35
+ const styles = useStyles ( )
26
36
const { username : usernameQueryParam , workspace : workspaceQueryParam } =
27
37
useParams ( )
28
38
const navigate = useNavigate ( )
@@ -33,6 +43,7 @@ export const WorkspaceSchedulePage: React.FC = () => {
33
43
checkPermissionsError,
34
44
submitScheduleError,
35
45
getWorkspaceError,
46
+ getTemplateError,
36
47
permissions,
37
48
workspace,
38
49
} = scheduleState . context
@@ -45,52 +56,39 @@ export const WorkspaceSchedulePage: React.FC = () => {
45
56
scheduleSend ( { type : "GET_WORKSPACE" , username, workspaceName } )
46
57
} , [ username , workspaceName , scheduleSend ] )
47
58
48
- const getAutoStart = ( workspace ?: TypesGen . Workspace ) =>
49
- scheduleToAutoStart ( workspace ?. autostart_schedule )
50
- const getAutoStop = ( workspace ?: TypesGen . Workspace ) =>
51
- ttlMsToAutoStop ( workspace ?. ttl_ms )
52
-
53
- const [ autoStart , setAutoStart ] = useState ( getAutoStart ( workspace ) )
54
- const [ autoStop , setAutoStop ] = useState ( getAutoStop ( workspace ) )
55
-
56
- useEffect ( ( ) => {
57
- setAutoStart ( getAutoStart ( workspace ) )
58
- setAutoStop ( getAutoStop ( workspace ) )
59
- } , [ workspace ] )
60
-
61
59
if ( ! username || ! workspaceName ) {
62
60
return < Navigate to = "/workspaces" />
63
61
}
64
62
65
- if (
66
- scheduleState . matches ( "idle" ) ||
67
- scheduleState . matches ( "gettingWorkspace" ) ||
68
- scheduleState . matches ( "gettingPermissions" ) ||
69
- ! workspace
70
- ) {
63
+ if ( scheduleState . hasTag ( "loading" ) ) {
71
64
return < FullScreenLoader />
72
65
}
73
66
74
67
if ( scheduleState . matches ( "error" ) ) {
75
68
return (
76
- < AlertBanner
77
- severity = "error"
78
- error = { getWorkspaceError || checkPermissionsError }
79
- text = {
80
- getWorkspaceError
81
- ? Language . getWorkspaceError
82
- : Language . checkPermissionsError
83
- }
84
- retry = { ( ) =>
85
- scheduleSend ( { type : "GET_WORKSPACE" , username, workspaceName } )
86
- }
87
- />
69
+ < Margins >
70
+ < div className = { styles . topMargin } >
71
+ < AlertBanner
72
+ severity = "error"
73
+ error = {
74
+ getWorkspaceError || checkPermissionsError || getTemplateError
75
+ }
76
+ retry = { ( ) =>
77
+ scheduleSend ( { type : "GET_WORKSPACE" , username, workspaceName } )
78
+ }
79
+ />
80
+ </ div >
81
+ </ Margins >
88
82
)
89
83
}
90
84
91
85
if ( ! permissions ?. updateWorkspace ) {
92
86
return (
93
- < AlertBanner severity = "error" error = { Error ( Language . forbiddenError ) } />
87
+ < Margins >
88
+ < div className = { styles . topMargin } >
89
+ < AlertBanner severity = "error" error = { Error ( t ( "forbiddenError" ) ) } />
90
+ </ div >
91
+ </ Margins >
94
92
)
95
93
}
96
94
@@ -101,7 +99,10 @@ export const WorkspaceSchedulePage: React.FC = () => {
101
99
return (
102
100
< WorkspaceScheduleForm
103
101
submitScheduleError = { submitScheduleError }
104
- initialValues = { { ...autoStart , ...autoStop } }
102
+ initialValues = { {
103
+ ...getAutoStart ( workspace ) ,
104
+ ...getAutoStop ( workspace ) ,
105
+ } }
105
106
isLoading = { scheduleState . tags . has ( "loading" ) }
106
107
onCancel = { ( ) => {
107
108
navigate ( `/@${ username } /${ workspaceName } ` )
@@ -111,15 +112,34 @@ export const WorkspaceSchedulePage: React.FC = () => {
111
112
type : "SUBMIT_SCHEDULE" ,
112
113
autoStart : formValuesToAutoStartRequest ( values ) ,
113
114
ttl : formValuesToTTLRequest ( values ) ,
114
- autoStartChanged : scheduleChanged ( autoStart , values ) ,
115
- autoStopChanged : scheduleChanged ( autoStop , values ) ,
115
+ autoStartChanged : scheduleChanged ( getAutoStart ( workspace ) , values ) ,
116
+ autoStopChanged : scheduleChanged ( getAutoStop ( workspace ) , values ) ,
116
117
} )
117
118
} }
118
119
/>
119
120
)
120
121
}
121
122
122
- if ( scheduleState . matches ( "submitSuccess" ) ) {
123
+ if ( scheduleState . matches ( "showingRestartDialog" ) ) {
124
+ return (
125
+ < ConfirmDialog
126
+ open
127
+ title = { t ( "dialogTitle" ) }
128
+ description = { t ( "dialogDescription" ) }
129
+ confirmText = { t ( "restart" ) }
130
+ cancelText = { t ( "applyLater" ) }
131
+ hideCancel = { false }
132
+ onConfirm = { ( ) => {
133
+ scheduleSend ( "RESTART_WORKSPACE" )
134
+ } }
135
+ onClose = { ( ) => {
136
+ scheduleSend ( "APPLY_LATER" )
137
+ } }
138
+ />
139
+ )
140
+ }
141
+
142
+ if ( scheduleState . matches ( "done" ) ) {
123
143
return < Navigate to = { `/@${ username } /${ workspaceName } ` } />
124
144
}
125
145
0 commit comments