Skip to content

feat(site): Read users into basic UsersTable #981

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 26 commits into from
Apr 14, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Start users
  • Loading branch information
presleyp committed Apr 11, 2022
commit fa600a50bc807a8c9e38b22d0fb6ae7a79ea015d
5 changes: 5 additions & 0 deletions site/src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ export const getApiKey = async (): Promise<Types.APIKeyResponse> => {
return response.data
}

export const getUsers = async (): Promise<Types.UserResponse[]> => {
const response = await axios.get<Types.UserResponse[]>("/api/v2/users")
return response.data
}

export const getBuildInfo = async (): Promise<Types.BuildInfoResponse> => {
const response = await axios.get("/api/v2/buildinfo")
return response.data
Expand Down
7 changes: 7 additions & 0 deletions site/src/test_helpers/entities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ export const MockUser: UserResponse = {
created_at: "",
}

export const MockUser2: UserResponse = {
id: "test-user-2",
username: "TestUser2",
email: "test2@coder.com",
created_at: "",
}

export const MockOrganization: Organization = {
id: "test-org",
name: "Test Organization",
Expand Down
3 changes: 3 additions & 0 deletions site/src/test_helpers/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ export const handlers = [
}),

// users
rest.get("/api/v2/users", async (req, res, ctx) => {
return res(ctx.status(200), ctx.json([M.MockUser, M.MockUser2]))
}),
rest.post("/api/v2/users/me/workspaces", async (req, res, ctx) => {
return res(ctx.status(200), ctx.json(M.MockWorkspace))
}),
Expand Down
76 changes: 76 additions & 0 deletions site/src/xServices/users/usersXService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { assign, createMachine } from "xstate"
import * as API from "../../api"
import * as Types from "../../api/types"

export interface UsersContext {
}

export type UsersEvent = { type: 'GET_USERS' }

export const usersMachine =
createMachine(
{
tsTypes: {} as import("./usersXService.typegen").Typegen0,
schema: {
context: {} as UsersContext,
events: {} as UsersEvent,
services: {} as {
getUsers: {
data: Types.UserResponse[]
}
},
},
context: {
},
id: "usersState",
initial: "gettingUsers",
states: {
gettingUsers: {
invoke: {
src: "getUsers",
id: "getUsers",
onDone: [
{
target: "#usersState.ready",
actions: ["assignUsers", "clearGetUsersError"]
},
],
onError: [
{
actions: "assignGetUsersError",
target: "#usersState.error",
},
],
},
tags: "loading",
},
ready: {
on: {
GET_USERS: "gettingUsers"
}
},
error: {
on: {
GET_USERS: "gettingUsers"
}
}
},
},
{
services: {
getUsers: API.getUsers
},
actions: {
assignUsers: assign({
me: (_, event) => event.data,
}),
assignGetUsersError: assign({
getUsersError: (_, event) => event.data,
}),
clearGetUsersError: assign((context: UsersContext) => ({
...context,
getUsersError: undefined,
})),
},
},
)