mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
Add navigate to xservice
Doesn't work in test
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { useInterpret } from "@xstate/react"
|
||||
import React, { createContext } from "react"
|
||||
import { useNavigate } from "react-router"
|
||||
import { ActorRefFrom } from "xstate"
|
||||
import { authMachine } from "./auth/authXService"
|
||||
import { buildInfoMachine } from "./buildInfo/buildInfoXService"
|
||||
@@ -22,12 +23,13 @@ interface XServiceContextType {
|
||||
export const XServiceContext = createContext({} as XServiceContextType)
|
||||
|
||||
export const XServiceProvider: React.FC = ({ children }) => {
|
||||
const navigate = useNavigate()
|
||||
return (
|
||||
<XServiceContext.Provider
|
||||
value={{
|
||||
authXService: useInterpret(authMachine),
|
||||
buildInfoXService: useInterpret(buildInfoMachine),
|
||||
usersXService: useInterpret(usersMachine),
|
||||
usersXService: useInterpret(usersMachine.withContext({ users: [], navigate })),
|
||||
}}
|
||||
>
|
||||
{children}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { NavigateFunction } from "react-router"
|
||||
import { assign, createMachine } from "xstate"
|
||||
import * as API from "../../api"
|
||||
import * as Types from "../../api/types"
|
||||
@@ -13,6 +14,7 @@ export interface UsersContext {
|
||||
pager?: Types.Pager
|
||||
getUsersError?: Error | unknown
|
||||
createUserError?: Error | unknown
|
||||
navigate?: NavigateFunction
|
||||
}
|
||||
|
||||
export type UsersEvent = { type: "GET_USERS" } | { type: "CREATE"; user: TypesGen.CreateUserRequest }
|
||||
@@ -69,7 +71,7 @@ export const usersMachine = createMachine(
|
||||
id: "createUser",
|
||||
onDone: {
|
||||
target: "idle",
|
||||
actions: ["displayCreateUserSuccess", "clearCreateUserError"],
|
||||
actions: ["displayCreateUserSuccess", "redirectToUsersPage", "clearCreateUserError"],
|
||||
},
|
||||
onError: {
|
||||
target: "idle",
|
||||
@@ -112,6 +114,9 @@ export const usersMachine = createMachine(
|
||||
displayCreateUserSuccess: () => {
|
||||
displaySuccess(Language.createUserSuccess)
|
||||
},
|
||||
redirectToUsersPage: (context) => {
|
||||
context.navigate && context.navigate("/users")
|
||||
}
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user