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