@@ -7,10 +7,11 @@ import { ErrorAlert } from "components/Alert/ErrorAlert";
7
7
import { Button } from "components/Button/Button" ;
8
8
import { FormFooter } from "components/Form/Form" ;
9
9
import { FullPageForm } from "components/FullPageForm/FullPageForm" ;
10
+ import { OrganizationAutocomplete } from "components/OrganizationAutocomplete/OrganizationAutocomplete" ;
10
11
import { PasswordField } from "components/PasswordField/PasswordField" ;
11
12
import { Spinner } from "components/Spinner/Spinner" ;
12
13
import { Stack } from "components/Stack/Stack" ;
13
- import { type FormikContextType , useFormik } from "formik" ;
14
+ import { useFormik } from "formik" ;
14
15
import type { FC } from "react" ;
15
16
import {
16
17
displayNameValidator ,
@@ -52,14 +53,6 @@ export const authMethodLanguage = {
52
53
} ,
53
54
} ;
54
55
55
- export interface CreateUserFormProps {
56
- onSubmit :( user :TypesGen . CreateUserRequestWithOrgs ) => void ;
57
- onCancel :( ) => void ;
58
- error ?:unknown ;
59
- isLoading :boolean ;
60
- authMethods ?:TypesGen . AuthMethods ;
61
- }
62
-
63
56
const validationSchema = Yup . object ( {
64
57
email :Yup . string ( )
65
58
. trim ( )
@@ -75,27 +68,51 @@ const validationSchema = Yup.object({
75
68
login_type :Yup . string ( ) . oneOf ( Object . keys ( authMethodLanguage ) ) ,
76
69
} ) ;
77
70
71
+ type CreateUserFormData = {
72
+ readonly username :string ;
73
+ readonly name :string ;
74
+ readonly email :string ;
75
+ readonly organization :string ;
76
+ readonly login_type :TypesGen . LoginType ;
77
+ readonly password :string ;
78
+ } ;
79
+
80
+ export interface CreateUserFormProps {
81
+ error ?:unknown ;
82
+ isLoading :boolean ;
83
+ onSubmit :( user :CreateUserFormData ) => void ;
84
+ onCancel :( ) => void ;
85
+ authMethods ?:TypesGen . AuthMethods ;
86
+ showOrganizations :boolean ;
87
+ }
88
+
78
89
export const CreateUserForm :FC <
79
90
React . PropsWithChildren < CreateUserFormProps >
80
- > = ( { onSubmit, onCancel, error, isLoading, authMethods} ) => {
81
- const form :FormikContextType < TypesGen . CreateUserRequestWithOrgs > =
82
- useFormik < TypesGen . CreateUserRequestWithOrgs > ( {
83
- initialValues :{
84
- email :"" ,
85
- password :"" ,
86
- username :"" ,
87
- name :"" ,
88
- organization_ids :[ "00000000-0000-0000-0000-000000000000" ] ,
89
- login_type :"" ,
90
- user_status :null ,
91
- } ,
92
- validationSchema,
93
- onSubmit,
94
- } ) ;
95
- const getFieldHelpers = getFormHelpers < TypesGen . CreateUserRequestWithOrgs > (
96
- form ,
97
- error ,
98
- ) ;
91
+ > = ( {
92
+ error,
93
+ isLoading,
94
+ onSubmit,
95
+ onCancel,
96
+ showOrganizations,
97
+ authMethods,
98
+ } ) => {
99
+ const form = useFormik < CreateUserFormData > ( {
100
+ initialValues :{
101
+ email :"" ,
102
+ password :"" ,
103
+ username :"" ,
104
+ name :"" ,
105
+ // If organizations aren't enabled, use the fallback ID to add the user to
106
+ // the default organization.
107
+ organization :showOrganizations
108
+ ?""
109
+ :"00000000-0000-0000-0000-000000000000" ,
110
+ login_type :"" ,
111
+ } ,
112
+ validationSchema,
113
+ onSubmit,
114
+ } ) ;
115
+ const getFieldHelpers = getFormHelpers ( form , error ) ;
99
116
100
117
const methods = [
101
118
authMethods ?. password . enabled && "password" ,
@@ -132,6 +149,20 @@ export const CreateUserForm: FC<
132
149
fullWidth
133
150
label = { Language . emailLabel }
134
151
/>
152
+ { showOrganizations && (
153
+ < OrganizationAutocomplete
154
+ { ...getFieldHelpers ( "organization" ) }
155
+ required
156
+ label = "Organization"
157
+ onChange = { ( newValue ) => {
158
+ void form . setFieldValue ( "organization" , newValue ?. id ?? "" ) ;
159
+ } }
160
+ check = { {
161
+ object :{ resource_type :"organization_member" } ,
162
+ action :"create" ,
163
+ } }
164
+ />
165
+ ) }
135
166
< TextField
136
167
{ ...getFieldHelpers ( "login_type" , {
137
168
helperText :"Authentication method for this user" ,