Skip to content

Commit 7d98c13

Browse files
committed
Move error type predicate to xservice
1 parent 6a490c2 commit 7d98c13

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed
Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { useActor } from "@xstate/react"
22
import React, { useContext } from "react"
33
import { useNavigate } from "react-router"
4-
import { isApiError, mapApiErrorToFieldErrors } from "../../../api/errors"
54
import { CreateUserRequest } from "../../../api/typesGenerated"
65
import { CreateUserForm } from "../../../components/CreateUserForm/CreateUserForm"
76
import { XServiceContext } from "../../../xServices/StateContext"
@@ -13,19 +12,18 @@ export const Language = {
1312
export const CreateUserPage = () => {
1413
const xServices = useContext(XServiceContext)
1514
const [usersState, usersSend] = useActor(xServices.usersXService)
16-
const { createUserError } = usersState.context
17-
const apiError = isApiError(createUserError)
18-
const formErrors = apiError ? mapApiErrorToFieldErrors(createUserError.response.data) : undefined
19-
const hasUnknownError = createUserError && !apiError
15+
const { createUserError, createUserFormErrors } = usersState.context
2016
const navigate = useNavigate()
17+
// There is no field for organization id in Community Edition, so handle its field error like a generic error
18+
const genericError = (createUserError || createUserFormErrors?.organization_id) ? Language.unknownError : undefined
2119

2220
return (
2321
<CreateUserForm
24-
formErrors={formErrors}
22+
formErrors={createUserFormErrors}
2523
onSubmit={(user: CreateUserRequest) => usersSend({ type: "CREATE", user })}
2624
onCancel={() => navigate("/users")}
2725
isLoading={usersState.hasTag("loading")}
28-
error={hasUnknownError ? Language.unknownError : undefined}
26+
error={genericError}
2927
/>
3028
)
3129
}

site/src/xServices/users/usersXService.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { NavigateFunction } from "react-router"
22
import { assign, createMachine } from "xstate"
33
import * as API from "../../api"
4+
import { ApiError, FieldErrors, isApiError, mapApiErrorToFieldErrors } from "../../api/errors"
45
import * as Types from "../../api/types"
56
import * as TypesGen from "../../api/typesGenerated"
67
import { displaySuccess } from "../../components/GlobalSnackbar/utils"
@@ -14,6 +15,7 @@ export interface UsersContext {
1415
pager?: Types.Pager
1516
getUsersError?: Error | unknown
1617
createUserError?: Error | unknown
18+
createUserFormErrors?: FieldErrors
1719
navigate?: NavigateFunction
1820
}
1921

@@ -73,10 +75,17 @@ export const usersMachine = createMachine(
7375
target: "idle",
7476
actions: ["displayCreateUserSuccess", "redirectToUsersPage", "clearCreateUserError"],
7577
},
76-
onError: {
77-
target: "idle",
78-
actions: ["assignCreateUserError"],
79-
},
78+
onError: [
79+
{
80+
target: "idle",
81+
cond: "isFormError",
82+
actions: ["assignCreateUserFormErrors"],
83+
},
84+
{
85+
target: "idle",
86+
actions: ["assignCreateUserError"],
87+
},
88+
],
8089
},
8190
tags: "loading",
8291
},
@@ -92,6 +101,9 @@ export const usersMachine = createMachine(
92101
getUsers: API.getUsers,
93102
createUser: (_, event) => API.createUser(event.user),
94103
},
104+
guards: {
105+
isFormError: (_, event) => isApiError(event.data)
106+
},
95107
actions: {
96108
assignUsers: assign({
97109
users: (_, event) => event.data.page,
@@ -107,6 +119,10 @@ export const usersMachine = createMachine(
107119
assignCreateUserError: assign({
108120
createUserError: (_, event) => event.data,
109121
}),
122+
assignCreateUserFormErrors: assign({
123+
// the guard ensures it is ApiError
124+
createUserFormErrors: (_, event) => mapApiErrorToFieldErrors((event.data as ApiError).response.data)
125+
}),
110126
clearCreateUserError: assign((context: UsersContext) => ({
111127
...context,
112128
createUserError: undefined,
@@ -116,7 +132,7 @@ export const usersMachine = createMachine(
116132
},
117133
redirectToUsersPage: (context) => {
118134
context.navigate && context.navigate("/users")
119-
}
135+
},
120136
},
121137
},
122138
)

0 commit comments

Comments
 (0)