|
| 1 | +import { useClerk, useOrganizationList } from '@clerk/shared/react'; |
| 2 | +import type { CreateOrganizationParams } from '@clerk/types'; |
| 3 | +import React, { useState } from 'react'; |
| 4 | + |
| 5 | +import { OrganizationAvatarUploader } from '@/ui/common/organizations/OrganizationAvatarUploader'; |
| 6 | +import { useSessionTasksContext } from '@/ui/contexts/components/SessionTasks'; |
| 7 | +import { Col, descriptors, Icon, localizationKeys } from '@/ui/customizables'; |
| 8 | +import { useCardState, withCardStateProvider } from '@/ui/elements/contexts'; |
| 9 | +import { Form } from '@/ui/elements/Form'; |
| 10 | +import { FormButtonContainer } from '@/ui/elements/FormButtons'; |
| 11 | +import { FormContainer } from '@/ui/elements/FormContainer'; |
| 12 | +import { IconButton } from '@/ui/elements/IconButton'; |
| 13 | +import { Organization } from '@/ui/icons'; |
| 14 | +import { createSlug } from '@/ui/utils/createSlug'; |
| 15 | +import { handleError } from '@/ui/utils/errorHandler'; |
| 16 | +import { useFormControl } from '@/ui/utils/useFormControl'; |
| 17 | + |
| 18 | +import { organizationListParams } from '../../../OrganizationSwitcher/utils'; |
| 19 | + |
| 20 | +type CreateOrganizationScreenProps = { |
| 21 | + onCancel?: () => void; |
| 22 | + hideSlug?: boolean; |
| 23 | +}; |
| 24 | + |
| 25 | +export const CreateOrganizationScreen = withCardStateProvider((props: CreateOrganizationScreenProps) => { |
| 26 | + const card = useCardState(); |
| 27 | + const { __internal_navigateToTaskIfAvailable } = useClerk(); |
| 28 | + const { redirectUrlComplete } = useSessionTasksContext(); |
| 29 | + const { createOrganization, isLoaded, setActive } = useOrganizationList({ |
| 30 | + userMemberships: organizationListParams.userMemberships, |
| 31 | + }); |
| 32 | + |
| 33 | + const [file, setFile] = useState<File | null>(); |
| 34 | + const nameField = useFormControl('name', '', { |
| 35 | + type: 'text', |
| 36 | + label: localizationKeys('formFieldLabel__organizationName'), |
| 37 | + placeholder: localizationKeys('formFieldInputPlaceholder__organizationName'), |
| 38 | + }); |
| 39 | + const slugField = useFormControl('slug', '', { |
| 40 | + type: 'text', |
| 41 | + label: localizationKeys('formFieldLabel__organizationSlug'), |
| 42 | + placeholder: localizationKeys('formFieldInputPlaceholder__organizationSlug'), |
| 43 | + }); |
| 44 | + |
| 45 | + const onSubmit = async (e: React.FormEvent) => { |
| 46 | + e.preventDefault(); |
| 47 | + |
| 48 | + if (!isLoaded) { |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + try { |
| 53 | + const createOrgParams: CreateOrganizationParams = { name: nameField.value }; |
| 54 | + |
| 55 | + if (!props.hideSlug) { |
| 56 | + // TODO -> Should we always show the slug |
| 57 | + // should it be exposed as a prop on TaskSelectOrganization |
| 58 | + createOrgParams.slug = slugField.value; |
| 59 | + } |
| 60 | + |
| 61 | + const organization = await createOrganization(createOrgParams); |
| 62 | + |
| 63 | + if (file) { |
| 64 | + await organization.setLogo({ file }); |
| 65 | + } |
| 66 | + |
| 67 | + await setActive({ organization }); |
| 68 | + |
| 69 | + await __internal_navigateToTaskIfAvailable({ redirectUrlComplete }); |
| 70 | + } catch (err) { |
| 71 | + handleError(err, [nameField, slugField], card.setError); |
| 72 | + } |
| 73 | + }; |
| 74 | + |
| 75 | + const onAvatarRemove = () => { |
| 76 | + card.setIdle(); |
| 77 | + return setFile(null); |
| 78 | + }; |
| 79 | + |
| 80 | + const onChangeName = (event: React.ChangeEvent<HTMLInputElement>) => { |
| 81 | + nameField.setValue(event.target.value); |
| 82 | + updateSlugField(createSlug(event.target.value)); |
| 83 | + }; |
| 84 | + |
| 85 | + const updateSlugField = (val: string) => { |
| 86 | + slugField.setValue(val); |
| 87 | + }; |
| 88 | + |
| 89 | + const isSubmitButtonDisabled = !nameField.value || !isLoaded; |
| 90 | + |
| 91 | + return ( |
| 92 | + <FormContainer> |
| 93 | + <Form.Root onSubmit={onSubmit}> |
| 94 | + <Col> |
| 95 | + <OrganizationAvatarUploader |
| 96 | + organization={{ name: nameField.value }} |
| 97 | + onAvatarChange={async file => setFile(file)} |
| 98 | + onAvatarRemove={file ? onAvatarRemove : null} |
| 99 | + actionTitle={localizationKeys('taskSelectOrganization.createOrganizationScreen.action__uploadAvatar')} |
| 100 | + imageTitle={localizationKeys('taskSelectOrganization.createOrganizationScreen.avatarLabel')} |
| 101 | + elementDescriptor={descriptors.organizationAvatarUploaderContainer} |
| 102 | + avatarPreviewPlaceholder={ |
| 103 | + <IconButton |
| 104 | + variant='ghost' |
| 105 | + aria-label='Upload organization logo' |
| 106 | + icon={ |
| 107 | + <Icon |
| 108 | + size='md' |
| 109 | + icon={Organization} |
| 110 | + sx={t => ({ |
| 111 | + color: t.colors.$colorMutedForeground, |
| 112 | + transitionDuration: t.transitionDuration.$controls, |
| 113 | + })} |
| 114 | + /> |
| 115 | + } |
| 116 | + sx={t => ({ |
| 117 | + width: t.sizes.$16, |
| 118 | + height: t.sizes.$16, |
| 119 | + borderRadius: t.radii.$md, |
| 120 | + borderWidth: t.borderWidths.$normal, |
| 121 | + borderStyle: t.borderStyles.$dashed, |
| 122 | + borderColor: t.colors.$borderAlpha200, |
| 123 | + backgroundColor: t.colors.$neutralAlpha50, |
| 124 | + ':hover': { |
| 125 | + backgroundColor: t.colors.$neutralAlpha50, |
| 126 | + svg: { |
| 127 | + transform: 'scale(1.2)', |
| 128 | + }, |
| 129 | + }, |
| 130 | + })} |
| 131 | + /> |
| 132 | + } |
| 133 | + /> |
| 134 | + </Col> |
| 135 | + <Form.ControlRow elementId={nameField.id}> |
| 136 | + <Form.PlainInput |
| 137 | + {...nameField.props} |
| 138 | + onChange={onChangeName} |
| 139 | + isRequired |
| 140 | + // TODO -> Remove auto focus? |
| 141 | + autoFocus |
| 142 | + ignorePasswordManager |
| 143 | + /> |
| 144 | + </Form.ControlRow> |
| 145 | + {!props.hideSlug && ( |
| 146 | + <Form.ControlRow elementId={slugField.id}> |
| 147 | + <Form.PlainInput |
| 148 | + {...slugField.props} |
| 149 | + onChange={event => updateSlugField(event.target.value)} |
| 150 | + isRequired |
| 151 | + pattern='^(?=.*[a-z0-9])[a-z0-9\-]+$' |
| 152 | + ignorePasswordManager |
| 153 | + /> |
| 154 | + </Form.ControlRow> |
| 155 | + )} |
| 156 | + <FormButtonContainer sx={t => ({ marginTop: t.space.$none, flexDirection: 'column' })}> |
| 157 | + <Form.SubmitButton |
| 158 | + block={false} |
| 159 | + sx={() => ({ width: '100%' })} |
| 160 | + isDisabled={isSubmitButtonDisabled} |
| 161 | + localizationKey={localizationKeys('taskSelectOrganization.createOrganizationScreen.formButtonSubmit')} |
| 162 | + /> |
| 163 | + {props.onCancel && ( |
| 164 | + <Form.ResetButton |
| 165 | + localizationKey={localizationKeys('taskSelectOrganization.createOrganizationScreen.formButtonReset')} |
| 166 | + block={false} |
| 167 | + onClick={props.onCancel} |
| 168 | + /> |
| 169 | + )} |
| 170 | + </FormButtonContainer> |
| 171 | + </Form.Root> |
| 172 | + </FormContainer> |
| 173 | + ); |
| 174 | +}); |
0 commit comments