| 1 | "use client"; |
| 2 | |
| 3 | import { |
| 4 | getAdditionalFieldDefaultValues, |
| 5 | getAdditionalFieldSubmitValues, |
| 6 | validateStringLength, |
| 7 | } from "@better-auth-ui/core"; |
| 8 | import type { OrganizationAuthClient |
| 9 | import { useAuth, useAuthPlugin |
| 10 | import { useCreateOrganization } |
| 11 | import { Briefcase } |
| 12 | import { useEffect, useRef |
| 13 | import { buttonVariants } |
| 14 | import { |
| 15 | Dialog, |
| 16 | DialogClose, |
| 17 | DialogContent, |
| 18 | DialogDescription, |
| 19 | DialogFooter, |
| 20 | DialogHeader, |
| 21 | DialogTitle, |
| 22 | } from "@/components/ui/dialog"; |
| 23 | import { Field, FieldLabel |
| 24 | import { Input } |
| 25 | import { organizationPlugin } |
| 26 | import { |
| 27 | getAuthAdditionalFieldValidators, |
| 28 | isAuthFormFieldInvalid, |
| 29 | useAuthForm, |
| 30 | } from "../auth-form"; |
| 31 | import { SlugField, sanitizeSlug |
| 32 | |
| 33 | export type CreateOrganizationDialogProps |
| 34 | open: boolean |
| 35 | onOpenChange: (open |
| 36 | hideSlug?: boolean |
| 37 | }; |
| 38 | |
| 39 | export function CreateOrganizationDialog({ |
| 40 | open, |
| 41 | onOpenChange, |
| 42 | hideSlug: hideSlugProp, |
| 43 | }: CreateOrganizationDialogProps) |
| 44 | const { authClient, |
| 45 | const { |
| 46 | additionalFields, |
| 47 | localization: organizationLocalization, |
| 48 | hideSlug: pluginHideSlug, |
| 49 | } = useAuthPlugin(organizationPlugin |
| 50 | const hideSlug = |
| 51 | |
| 52 | const [slugEdited, |
| 53 | const submissionGeneration = |
| 54 | const submissionAttemptGeneration = |
| 55 | |
| 56 | const { mutateAsync: |
| 57 | |
| 58 | const form = |
| 59 | defaultValues: { |
| 60 | additionalFields: getAdditionalFieldDefaultValues(additionalFields |
| 61 | name: "", |
| 62 | slug: "", |
| 63 | }, |
| 64 | onSubmit: async ( |
| 65 | const generation = |
| 66 | if (generation !== |
| 67 | await createOrganization({ |
| 68 | ...getAdditionalFieldSubmitValues( |
| 69 | additionalFields, |
| 70 | value.additionalFields, |
| 71 | ), |
| 72 | name: value.name |
| 73 | slug: hideSlug ? |
| 74 | }); |
| 75 | if (generation === |
| 76 | }, |
| 77 | }); |
| 78 | |
| 79 | useEffect(() => |
| 80 | if (!open) |
| 81 | submissionGeneration.current += |
| 82 | form.reset(); |
| 83 | setSlugEdited(false); |
| 84 | } |
| 85 | }, [form, |
| 86 | |
| 87 | return ( |
| 88 | <Dialog open={ |
| 89 | <DialogContent> |
| 90 | <form.AppForm> |
| 91 | <form.AuthFormRoot |
| 92 | className="flex flex-col gap-6" |
| 93 | onBeforeSubmit={() => |
| 94 | submissionAttemptGeneration.current = |
| 95 | submissionGeneration.current; |
| 96 | }} |
| 97 | > |
| 98 | <DialogHeader> |
| 99 | <DialogTitle className=" |
| 100 | <Briefcase /> |
| 101 | {organizationLocalization.createOrganization} |
| 102 | </DialogTitle> |
| 103 | |
| 104 | <DialogDescription> |
| 105 | {organizationLocalization.organizationsDescription} |
| 106 | </DialogDescription> |
| 107 | </DialogHeader> |
| 108 | |
| 109 | <div className="flex flex-col gap-4 |
| 110 | <form.AppField |
| 111 | name="name" |
| 112 | validators={{ |
| 113 | onChange: ({ value |
| 114 | validateStringLength(value, { |
| 115 | requiredMessage: localization.auth |
| 116 | trim: true, |
| 117 | }), |
| 118 | }} |
| 119 | > |
| 120 | {(field) => |
| 121 | const isInvalid = |
| 122 | |
| 123 | return ( |
| 124 | <Field data-invalid={ |
| 125 | <FieldLabel htmlFor=" |
| 126 | {organizationLocalization.name} |
| 127 | </FieldLabel> |
| 128 | |
| 129 | <Input |
| 130 | id="create-organization-name" |
| 131 | name={field.name |
| 132 | autoFocus |
| 133 | placeholder={organizationLocalization.namePlaceholder |
| 134 | value={field.state |
| 135 | onBlur={field.handleBlur |
| 136 | onChange={(event) |
| 137 | const value = |
| 138 | field.handleChange(value |
| 139 | if (!slugEdited) { |
| 140 | form.setFieldValue("slug |
| 141 | } |
| 142 | }} |
| 143 | aria-invalid={isInvalid} |
| 144 | /> |
| 145 | |
| 146 | <field.AuthFormFieldError /> |
| 147 | </Field> |
| 148 | ); |
| 149 | }} |
| 150 | </form.AppField> |
| 151 | |
| 152 | {!hideSlug && ( |
| 153 | <form.AppField name=" |
| 154 | {(field) => |
| 155 | <SlugField |
| 156 | id="create-organization-slug" |
| 157 | value={field.state |
| 158 | onChange={(value) |
| 159 | field.handleChange(value |
| 160 | setSlugEdited(true); |
| 161 | }} |
| 162 | /> |
| 163 | )} |
| 164 | </form.AppField> |
| 165 | )} |
| 166 | |
| 167 | {additionalFields.map(( |
| 168 | <form.AppField |
| 169 | key={configuredField.name |
| 170 | name={`additionalFields.${configuredField |
| 171 | validators={getAuthAdditionalFieldValidators( |
| 172 | configuredField, |
| 173 | localization.auth.fieldRequired |
| 174 | )} |
| 175 | > |
| 176 | {(field) => |
| 177 | <field.AuthFormAdditionalField |
| 178 | field={configuredField} |
| 179 | optionalLabel={localization.settings |
| 180 | /> |
| 181 | )} |
| 182 | </form.AppField> |
| 183 | ))} |
| 184 | </div> |
| 185 | |
| 186 | <DialogFooter> |
| 187 | <DialogClose |
| 188 | className={buttonVariants({ |
| 189 | type="button" |
| 190 | > |
| 191 | {localization.settings.cancel |
| 192 | </DialogClose> |
| 193 | |
| 194 | <form.AuthFormSubmitButton> |
| 195 | {organizationLocalization.createOrganization} |
| 196 | </form.AuthFormSubmitButton> |
| 197 | </DialogFooter> |
| 198 | </form.AuthFormRoot> |
| 199 | </form.AppForm> |
| 200 | </DialogContent> |
| 201 | </Dialog> |
| 202 | ); |
| 203 | } |