@@ -7,10 +7,11 @@ import { ErrorAlert } from "components/Alert/ErrorAlert";
77import { Button } from "components/Button/Button" ;
88import { FormFooter } from "components/Form/Form" ;
99import { FullPageForm } from "components/FullPageForm/FullPageForm" ;
10+ import { OrganizationAutocomplete } from "components/OrganizationAutocomplete/OrganizationAutocomplete" ;
1011import { PasswordField } from "components/PasswordField/PasswordField" ;
1112import { Spinner } from "components/Spinner/Spinner" ;
1213import { Stack } from "components/Stack/Stack" ;
13- import { type FormikContextType , useFormik } from "formik" ;
14+ import { useFormik } from "formik" ;
1415import type { FC } from "react" ;
1516import {
1617displayNameValidator ,
@@ -52,14 +53,6 @@ export const authMethodLanguage = {
5253} ,
5354} ;
5455
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-
6356const validationSchema = Yup . object ( {
6457email :Yup . string ( )
6558. trim ( )
@@ -75,27 +68,51 @@ const validationSchema = Yup.object({
7568login_type :Yup . string ( ) . oneOf ( Object . keys ( authMethodLanguage ) ) ,
7669} ) ;
7770
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+
7889export const CreateUserForm :FC <
7990React . 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 ) ;
99116
100117const methods = [
101118authMethods ?. password . enabled && "password" ,
@@ -132,6 +149,20 @@ export const CreateUserForm: FC<
132149fullWidth
133150label = { Language . emailLabel }
134151/>
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+ ) }
135166< TextField
136167{ ...getFieldHelpers ( "login_type" , {
137168helperText :"Authentication method for this user" ,