Skip to content

Commit 6a490c2

Browse files
committed
Add navigate to xservice
Doesn't work in test
1 parent 470481c commit 6a490c2

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

site/src/xServices/StateContext.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { useInterpret } from "@xstate/react"
22
import React, { createContext } from "react"
3+
import { useNavigate } from "react-router"
34
import { ActorRefFrom } from "xstate"
45
import { authMachine } from "./auth/authXService"
56
import { buildInfoMachine } from "./buildInfo/buildInfoXService"
@@ -22,12 +23,13 @@ interface XServiceContextType {
2223
export const XServiceContext = createContext({} as XServiceContextType)
2324

2425
export const XServiceProvider: React.FC = ({ children }) => {
26+
const navigate = useNavigate()
2527
return (
2628
<XServiceContext.Provider
2729
value={{
2830
authXService: useInterpret(authMachine),
2931
buildInfoXService: useInterpret(buildInfoMachine),
30-
usersXService: useInterpret(usersMachine),
32+
usersXService: useInterpret(usersMachine.withContext({ users: [], navigate })),
3133
}}
3234
>
3335
{children}

site/src/xServices/users/usersXService.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { NavigateFunction } from "react-router"
12
import { assign, createMachine } from "xstate"
23
import * as API from "../../api"
34
import * as Types from "../../api/types"
@@ -13,6 +14,7 @@ export interface UsersContext {
1314
pager?: Types.Pager
1415
getUsersError?: Error | unknown
1516
createUserError?: Error | unknown
17+
navigate?: NavigateFunction
1618
}
1719

1820
export type UsersEvent = { type: "GET_USERS" } | { type: "CREATE"; user: TypesGen.CreateUserRequest }
@@ -69,7 +71,7 @@ export const usersMachine = createMachine(
6971
id: "createUser",
7072
onDone: {
7173
target: "idle",
72-
actions: ["displayCreateUserSuccess", "clearCreateUserError"],
74+
actions: ["displayCreateUserSuccess", "redirectToUsersPage", "clearCreateUserError"],
7375
},
7476
onError: {
7577
target: "idle",
@@ -112,6 +114,9 @@ export const usersMachine = createMachine(
112114
displayCreateUserSuccess: () => {
113115
displaySuccess(Language.createUserSuccess)
114116
},
117+
redirectToUsersPage: (context) => {
118+
context.navigate && context.navigate("/users")
119+
}
115120
},
116121
},
117122
)

0 commit comments

Comments
 (0)