1
- import { useMachine } from "@xstate/react"
1
+ import { useMachine , useSelector } from "@xstate/react"
2
2
import * as cronParser from "cron-parser"
3
3
import dayjs from "dayjs"
4
4
import timezone from "dayjs/plugin/timezone"
5
5
import utc from "dayjs/plugin/utc"
6
- import React , { useEffect } from "react"
6
+ import React , { useContext , useEffect } from "react"
7
7
import { useNavigate , useParams } from "react-router-dom"
8
8
import * as TypesGen from "../../api/typesGenerated"
9
9
import { ErrorSummary } from "../../components/ErrorSummary/ErrorSummary"
@@ -16,6 +16,8 @@ import {
16
16
} from "../../components/WorkspaceScheduleForm/WorkspaceScheduleForm"
17
17
import { firstOrItem } from "../../util/array"
18
18
import { extractTimezone , stripTimezone } from "../../util/schedule"
19
+ import { selectUser } from "../../xServices/auth/authSelectors"
20
+ import { XServiceContext } from "../../xServices/StateContext"
19
21
import { workspaceSchedule } from "../../xServices/workspaceSchedule/workspaceScheduleXService"
20
22
21
23
// REMARK: timezone plugin depends on UTC
@@ -24,6 +26,10 @@ import { workspaceSchedule } from "../../xServices/workspaceSchedule/workspaceSc
24
26
dayjs . extend ( utc )
25
27
dayjs . extend ( timezone )
26
28
29
+ const Language = {
30
+ forbiddenError : "You don't have permissions to update the schedule for this workspace." ,
31
+ }
32
+
27
33
export const formValuesToAutoStartRequest = (
28
34
values : WorkspaceScheduleFormValues ,
29
35
) : TypesGen . UpdateWorkspaceAutostartRequest => {
@@ -141,8 +147,17 @@ export const WorkspaceSchedulePage: React.FC = () => {
141
147
const navigate = useNavigate ( )
142
148
const username = firstOrItem ( usernameQueryParam , null )
143
149
const workspaceName = firstOrItem ( workspaceQueryParam , null )
144
- const [ scheduleState , scheduleSend ] = useMachine ( workspaceSchedule )
145
- const { formErrors, getWorkspaceError, workspace } = scheduleState . context
150
+
151
+ const xServices = useContext ( XServiceContext )
152
+ const me = useSelector ( xServices . authXService , selectUser )
153
+
154
+ const [ scheduleState , scheduleSend ] = useMachine ( workspaceSchedule , {
155
+ context : {
156
+ userId : me ?. id ,
157
+ } ,
158
+ } )
159
+ const { checkPermissionsError, formErrors, getWorkspaceError, permissions, workspace } =
160
+ scheduleState . context
146
161
147
162
// Get workspace on mount and whenever the args for getting a workspace change.
148
163
// scheduleSend should not change.
@@ -153,20 +168,31 @@ export const WorkspaceSchedulePage: React.FC = () => {
153
168
if ( ! username || ! workspaceName ) {
154
169
navigate ( "/workspaces" )
155
170
return null
156
- } else if (
171
+ }
172
+
173
+ if (
157
174
scheduleState . matches ( "idle" ) ||
158
175
scheduleState . matches ( "gettingWorkspace" ) ||
176
+ scheduleState . matches ( "gettingPermissions" ) ||
159
177
! workspace
160
178
) {
161
179
return < FullScreenLoader />
162
- } else if ( scheduleState . matches ( "error" ) ) {
180
+ }
181
+
182
+ if ( scheduleState . matches ( "error" ) ) {
163
183
return (
164
184
< ErrorSummary
165
- error = { getWorkspaceError }
185
+ error = { getWorkspaceError || checkPermissionsError }
166
186
retry = { ( ) => scheduleSend ( { type : "GET_WORKSPACE" , username, workspaceName } ) }
167
187
/>
168
188
)
169
- } else if ( scheduleState . matches ( "presentForm" ) || scheduleState . matches ( "submittingSchedule" ) ) {
189
+ }
190
+
191
+ if ( ! permissions ?. updateWorkspace ) {
192
+ return < ErrorSummary error = { Error ( Language . forbiddenError ) } />
193
+ }
194
+
195
+ if ( scheduleState . matches ( "presentForm" ) || scheduleState . matches ( "submittingSchedule" ) ) {
170
196
return (
171
197
< WorkspaceScheduleForm
172
198
fieldErrors = { formErrors }
@@ -184,13 +210,15 @@ export const WorkspaceSchedulePage: React.FC = () => {
184
210
} }
185
211
/>
186
212
)
187
- } else if ( scheduleState . matches ( "submitSuccess" ) ) {
213
+ }
214
+
215
+ if ( scheduleState . matches ( "submitSuccess" ) ) {
188
216
navigate ( `/@${ username } /${ workspaceName } ` )
189
217
return < FullScreenLoader />
190
- } else {
191
- // Theoretically impossible - log and bail
192
- console . error ( "WorkspaceSchedulePage: unknown state :: " , scheduleState )
193
- navigate ( "/" )
194
- return null
195
218
}
219
+
220
+ // Theoretically impossible - log and bail
221
+ console . error ( "WorkspaceSchedulePage: unknown state :: " , scheduleState )
222
+ navigate ( "/" )
223
+ return null
196
224
}
0 commit comments