|
| 1 | +import TextField from "@mui/material/TextField"; |
| 2 | +import { useFormik } from "formik"; |
| 3 | +import type { FC } from "react"; |
| 4 | +import * as Yup from "yup"; |
| 5 | +import { isApiValidationError } from "api/errors"; |
| 6 | +import type { CreateOrganizationRequest } from "api/typesGenerated"; |
| 7 | +import { ErrorAlert } from "components/Alert/ErrorAlert"; |
| 8 | +import { |
| 9 | + FormFields, |
| 10 | + FormSection, |
| 11 | + HorizontalForm, |
| 12 | + FormFooter, |
| 13 | +} from "components/Form/Form"; |
| 14 | +import { IconField } from "components/IconField/IconField"; |
| 15 | +import { PageHeader, PageHeaderTitle } from "components/PageHeader/PageHeader"; |
| 16 | +import { |
| 17 | + getFormHelpers, |
| 18 | + nameValidator, |
| 19 | + displayNameValidator, |
| 20 | + onChangeTrimmed, |
| 21 | +} from "utils/formUtils"; |
| 22 | + |
| 23 | +const MAX_DESCRIPTION_CHAR_LIMIT = 128; |
| 24 | +const MAX_DESCRIPTION_MESSAGE = `Please enter a description that is no longer than ${MAX_DESCRIPTION_CHAR_LIMIT} characters.`; |
| 25 | + |
| 26 | +const validationSchema = Yup.object({ |
| 27 | + name: nameValidator("Name"), |
| 28 | + display_name: displayNameValidator("Display name"), |
| 29 | + description: Yup.string().max( |
| 30 | + MAX_DESCRIPTION_CHAR_LIMIT, |
| 31 | + MAX_DESCRIPTION_MESSAGE, |
| 32 | + ), |
| 33 | +}); |
| 34 | + |
| 35 | +interface CreateOrganizationPageViewProps { |
| 36 | + error: unknown; |
| 37 | + onSubmit: (values: CreateOrganizationRequest) => Promise<void>; |
| 38 | +} |
| 39 | + |
| 40 | +export const CreateOrganizationPageView: FC< |
| 41 | + CreateOrganizationPageViewProps |
| 42 | +> = ({ error, onSubmit }) => { |
| 43 | + const form = useFormik<CreateOrganizationRequest>({ |
| 44 | + initialValues: { |
| 45 | + name: "", |
| 46 | + display_name: "", |
| 47 | + description: "", |
| 48 | + icon: "", |
| 49 | + }, |
| 50 | + validationSchema, |
| 51 | + onSubmit, |
| 52 | + }); |
| 53 | + const getFieldHelpers = getFormHelpers(form, error); |
| 54 | + |
| 55 | + return ( |
| 56 | + <div> |
| 57 | + <PageHeader> |
| 58 | + <PageHeaderTitle>Organization settings</PageHeaderTitle> |
| 59 | + </PageHeader> |
| 60 | + |
| 61 | + {Boolean(error) && !isApiValidationError(error) && ( |
| 62 | + <div css={{ marginBottom: 32 }}> |
| 63 | + <ErrorAlert error={error} /> |
| 64 | + </div> |
| 65 | + )} |
| 66 | + |
| 67 | + <HorizontalForm |
| 68 | + onSubmit={form.handleSubmit} |
| 69 | + aria-label="Organization settings form" |
| 70 | + > |
| 71 | + <FormSection |
| 72 | + title="General info" |
| 73 | + description="Change the name or description of the organization." |
| 74 | + > |
| 75 | + <fieldset |
| 76 | + disabled={form.isSubmitting} |
| 77 | + css={{ border: "unset", padding: 0, margin: 0, width: "100%" }} |
| 78 | + > |
| 79 | + <FormFields> |
| 80 | + <TextField |
| 81 | + {...getFieldHelpers("name")} |
| 82 | + onChange={onChangeTrimmed(form)} |
| 83 | + autoFocus |
| 84 | + fullWidth |
| 85 | + label="Name" |
| 86 | + /> |
| 87 | + <TextField |
| 88 | + {...getFieldHelpers("display_name")} |
| 89 | + fullWidth |
| 90 | + label="Display name" |
| 91 | + /> |
| 92 | + <TextField |
| 93 | + {...getFieldHelpers("description")} |
| 94 | + multiline |
| 95 | + fullWidth |
| 96 | + label="Description" |
| 97 | + rows={2} |
| 98 | + /> |
| 99 | + <IconField |
| 100 | + {...getFieldHelpers("icon")} |
| 101 | + onChange={onChangeTrimmed(form)} |
| 102 | + fullWidth |
| 103 | + onPickEmoji={(value) => form.setFieldValue("icon", value)} |
| 104 | + /> |
| 105 | + </FormFields> |
| 106 | + </fieldset> |
| 107 | + </FormSection> |
| 108 | + <FormFooter isLoading={form.isSubmitting} /> |
| 109 | + </HorizontalForm> |
| 110 | + </div> |
| 111 | + ); |
| 112 | +}; |
0 commit comments