Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix: error messages from workspaceScheduleXService #3255

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Jul 28, 2022
Merged
Prev Previous commit
Next Next commit
Fix errors in schedule xservice
  • Loading branch information
presleyp committed Jul 27, 2022
commit 82ee773c56a4ab3c003eeab0192f6faaf9edcca5
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ import utc from "dayjs/plugin/utc"
import { useFormik } from "formik"
import { FC } from "react"
import * as Yup from "yup"
import { FieldErrors } from "../../api/errors"
import { getFormHelpers } from "../../util/formUtils"
import { getFormHelpersWithError } from "../../util/formUtils"
import { FormFooter } from "../FormFooter/FormFooter"
import { FullPageForm } from "../FullPageForm/FullPageForm"
import { Stack } from "../Stack/Stack"
Expand Down Expand Up @@ -54,7 +53,7 @@ export const Language = {
}

export interface WorkspaceScheduleFormProps {
fieldErrors?: FieldErrors
submitScheduleError?: Error | unknown
initialValues?: WorkspaceScheduleFormValues
isLoading: boolean
onCancel: () => void
Expand Down Expand Up @@ -178,7 +177,7 @@ export const defaultWorkspaceSchedule = (
})

export const WorkspaceScheduleForm: FC<WorkspaceScheduleFormProps> = ({
fieldErrors,
submitScheduleError,
initialValues = defaultWorkspaceSchedule(),
isLoading,
onCancel,
Expand All @@ -191,7 +190,7 @@ export const WorkspaceScheduleForm: FC<WorkspaceScheduleFormProps> = ({
onSubmit,
validationSchema,
})
const formHelpers = getFormHelpers<WorkspaceScheduleFormValues>(form, fieldErrors)
const formHelpers = getFormHelpersWithError<WorkspaceScheduleFormValues>(form, submitScheduleError)

const checkboxes: Array<{ value: boolean; name: string; label: string }> = [
{ value: form.values.sunday, name: "sunday", label: Language.daySundayLabel },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ dayjs.extend(timezone)

const Language = {
forbiddenError: "You don't have permissions to update the schedule for this workspace.",
getWorkspaceError: "Failed to fetch workspace.",
checkPermissionsError: "Failed to fetch permissions."
}

export const formValuesToAutoStartRequest = (
Expand Down Expand Up @@ -156,7 +158,7 @@ export const WorkspaceSchedulePage: React.FC = () => {
userId: me?.id,
},
})
const { checkPermissionsError, formErrors, getWorkspaceError, permissions, workspace } =
const { checkPermissionsError, submitScheduleError, getWorkspaceError, permissions, workspace } =
scheduleState.context

// Get workspace on mount and whenever the args for getting a workspace change.
Expand All @@ -183,6 +185,7 @@ export const WorkspaceSchedulePage: React.FC = () => {
return (
<ErrorSummary
error={getWorkspaceError || checkPermissionsError}
defaultMessage={getWorkspaceError ? Language.getWorkspaceError : Language.checkPermissionsError}
retry={() => scheduleSend({ type: "GET_WORKSPACE", username, workspaceName })}
/>
)
Expand All @@ -195,7 +198,7 @@ export const WorkspaceSchedulePage: React.FC = () => {
if (scheduleState.matches("presentForm") || scheduleState.matches("submittingSchedule")) {
return (
<WorkspaceScheduleForm
fieldErrors={formErrors}
submitScheduleError={submitScheduleError}
initialValues={workspaceToInitialValues(workspace, dayjs.tz.guess())}
isLoading={scheduleState.tags.has("loading")}
onCancel={() => {
Expand Down
21 changes: 6 additions & 15 deletions site/src/xServices/workspaceSchedule/workspaceScheduleXService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,16 @@
*/
import { assign, createMachine } from "xstate"
import * as API from "../../api/api"
import { ApiError, FieldErrors, mapApiErrorToFieldErrors } from "../../api/errors"
import * as TypesGen from "../../api/typesGenerated"
import { displayError, displaySuccess } from "../../components/GlobalSnackbar/utils"
import { displaySuccess } from "../../components/GlobalSnackbar/utils"

export const Language = {
errorSubmissionFailed: "Failed to update schedule",
errorWorkspaceFetch: "Failed to fetch workspace",
successMessage: "Successfully updated workspace schedule.",
}

type Permissions = Record<keyof ReturnType<typeof permissionsToCheck>, boolean>

export interface WorkspaceScheduleContext {
formErrors?: FieldErrors
getWorkspaceError?: Error | unknown
/**
* Each workspace has their own schedule (start and ttl). For this reason, we
Expand All @@ -29,6 +25,7 @@ export interface WorkspaceScheduleContext {
userId?: string
permissions?: Permissions
checkPermissionsError?: Error | unknown
submitScheduleError?: Error | unknown
}

export const checks = {
Expand Down Expand Up @@ -86,7 +83,7 @@ export const workspaceSchedule = createMachine(
},
onError: {
target: "error",
actions: ["assignGetWorkspaceError", "displayWorkspaceError"],
actions: ["assignGetWorkspaceError"],
},
},
tags: "loading",
Expand Down Expand Up @@ -125,7 +122,7 @@ export const workspaceSchedule = createMachine(
},
onError: {
target: "presentForm",
actions: ["assignSubmissionError", "displaySubmissionError"],
actions: ["assignSubmissionError"],
},
},
tags: "loading",
Expand All @@ -145,7 +142,7 @@ export const workspaceSchedule = createMachine(
{
actions: {
assignSubmissionError: assign({
formErrors: (_, event) => mapApiErrorToFieldErrors((event.data as ApiError).response.data),
submitScheduleError: (_, event) => event.data
}),
assignWorkspace: assign({
workspace: (_, event) => event.data,
Expand All @@ -170,12 +167,6 @@ export const workspaceSchedule = createMachine(
clearGetWorkspaceError: (context) => {
assign({ ...context, getWorkspaceError: undefined })
},
displayWorkspaceError: () => {
displayError(Language.errorWorkspaceFetch)
},
displaySubmissionError: () => {
displayError(Language.errorSubmissionFailed)
},
displaySuccess: () => {
displaySuccess(Language.successMessage)
},
Expand All @@ -197,7 +188,7 @@ export const workspaceSchedule = createMachine(
submitSchedule: async (context, event) => {
if (!context.workspace?.id) {
// This state is theoretically impossible, but helps TS
throw new Error("failed to load workspace")
throw new Error("Failed to load workspace.")
}

// REMARK: These calls are purposefully synchronous because if one
Expand Down