Skip to content

Commit 797bb34

Browse files
committed
Pretend user has org id and make it work
1 parent 62dad5e commit 797bb34

File tree

8 files changed

+31
-10
lines changed

8 files changed

+31
-10
lines changed

site/src/api/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ export const getUsers = async (): Promise<Types.PagedUsers> => {
8585
})
8686
}
8787

88-
export const createUser = async (user: TypesGen.CreateUserRequest): Promise<TypesGen.User> => {
88+
export const createUser = async (user: Types.CreateUserRequest): Promise<TypesGen.User> => {
8989
const response = await axios.post<TypesGen.User>("/api/v2/users", user)
9090
return response.data
9191
}

site/src/api/types.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,20 @@ export interface LoginResponse {
1010
session_token: string
1111
}
1212

13+
export interface CreateUserRequest {
14+
username: string
15+
email: string
16+
password: string
17+
organization_id: string
18+
}
19+
1320
export interface UserResponse {
1421
readonly id: string
1522
readonly username: string
1623
readonly email: string
1724
readonly created_at: string
1825
readonly status: "active" | "suspended"
26+
readonly organization_id: string
1927
}
2028

2129
/**

site/src/components/CreateUserForm/CreateUserForm.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import TextField from "@material-ui/core/TextField"
33
import { FormikContextType, FormikErrors, useFormik } from "formik"
44
import React from "react"
55
import * as Yup from "yup"
6-
import { CreateUserRequest } from "../../api/typesGenerated"
6+
import { CreateUserRequest } from "../../api/types"
77
import { getFormHelpers, onChangeTrimmed } from "../../util/formUtils"
88
import { FormFooter } from "../FormFooter/FormFooter"
99
import { FullPageForm } from "../FullPageForm/FullPageForm"
@@ -26,6 +26,7 @@ export interface CreateUserFormProps {
2626
formErrors?: FormikErrors<CreateUserRequest>
2727
isLoading: boolean
2828
error?: string
29+
myOrgId: string
2930
}
3031

3132
const validationSchema = Yup.object({
@@ -34,12 +35,13 @@ const validationSchema = Yup.object({
3435
username: Yup.string().required(Language.usernameRequired),
3536
})
3637

37-
export const CreateUserForm: React.FC<CreateUserFormProps> = ({ onSubmit, onCancel, formErrors, isLoading, error }) => {
38+
export const CreateUserForm: React.FC<CreateUserFormProps> = ({ onSubmit, onCancel, formErrors, isLoading, error, myOrgId }) => {
3839
const form: FormikContextType<CreateUserRequest> = useFormik<CreateUserRequest>({
3940
initialValues: {
4041
email: "",
4142
password: "",
4243
username: "",
44+
organization_id: myOrgId
4345
},
4446
validationSchema,
4547
onSubmit,

site/src/pages/PreferencesPages/AccountPage/AccountPage.test.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,10 @@ describe("AccountPage", () => {
3434
jest.spyOn(API, "updateProfile").mockImplementationOnce((userId, data) =>
3535
Promise.resolve({
3636
id: userId,
37-
...data,
3837
created_at: new Date().toString(),
39-
status: "active"
38+
status: "active",
39+
organization_id: "123",
40+
...data,
4041
}),
4142
)
4243
const { user } = renderPage()
Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import { useActor } from "@xstate/react"
1+
import { useActor, useSelector } from "@xstate/react"
22
import React, { useContext } from "react"
33
import { useNavigate } from "react-router"
4-
import { CreateUserRequest } from "../../../api/typesGenerated"
4+
import { CreateUserRequest } from "../../../api/types"
55
import { CreateUserForm } from "../../../components/CreateUserForm/CreateUserForm"
6+
import { selectOrgId } from "../../../xServices/auth/authSelectors"
67
import { XServiceContext } from "../../../xServices/StateContext"
78

89
export const Language = {
@@ -11,11 +12,12 @@ export const Language = {
1112

1213
export const CreateUserPage: React.FC = () => {
1314
const xServices = useContext(XServiceContext)
15+
const myOrgId = useSelector(xServices.authXService, selectOrgId)
1416
const [usersState, usersSend] = useActor(xServices.usersXService)
1517
const { createUserError, createUserFormErrors } = usersState.context
1618
const navigate = useNavigate()
1719
// 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
20+
const genericError = (createUserError || createUserFormErrors?.organization_id || !myOrgId) ? Language.unknownError : undefined
1921

2022
return (
2123
<CreateUserForm
@@ -24,6 +26,7 @@ export const CreateUserPage: React.FC = () => {
2426
onCancel={() => navigate("/users")}
2527
isLoading={usersState.hasTag("loading")}
2628
error={genericError}
29+
myOrgId={myOrgId ?? ""}
2730
/>
2831
)
2932
}

site/src/testHelpers/entities.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export const MockUser: UserResponse = {
2626
email: "test@coder.com",
2727
created_at: "",
2828
status: "active",
29+
organization_id: "123"
2930
}
3031

3132
export const MockUser2: UserResponse = {
@@ -34,6 +35,7 @@ export const MockUser2: UserResponse = {
3435
email: "test2@coder.com",
3536
created_at: "",
3637
status: "active",
38+
organization_id: "123"
3739
}
3840

3941
export const MockPager: Pager = {
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { State } from "xstate"
2+
import { AuthContext, AuthEvent } from "./authXService"
3+
4+
export const selectOrgId = (state: State<AuthContext, AuthEvent>) => {
5+
return state.context.me?.organization_id
6+
}

site/src/xServices/users/usersXService.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { NavigateFunction } from "react-router"
21
import { assign, createMachine } from "xstate"
32
import * as API from "../../api"
43
import { ApiError, FieldErrors, isApiError, mapApiErrorToFieldErrors } from "../../api/errors"
@@ -18,7 +17,7 @@ export interface UsersContext {
1817
createUserFormErrors?: FieldErrors
1918
}
2019

21-
export type UsersEvent = { type: "GET_USERS" } | { type: "CREATE"; user: TypesGen.CreateUserRequest }
20+
export type UsersEvent = { type: "GET_USERS" } | { type: "CREATE"; user: Types.CreateUserRequest }
2221

2322
export const usersMachine = createMachine(
2423
{

0 commit comments

Comments
 (0)