import { Dispatch, SetStateAction, useEffect, useState, FC } from "react"; import { observer } from "mobx-react-lite"; import { useRouter } from "next/router"; import { Controller, useForm } from "react-hook-form"; import { IWorkspace } from "@plane/types"; // ui import { Button, CustomSelect, Input, TOAST_TYPE, setToast } from "@plane/ui"; // constants import { WORKSPACE_CREATED } from "@/constants/event-tracker"; import { ORGANIZATION_SIZE, RESTRICTED_URLS } from "@/constants/workspace"; // hooks import { useEventTracker, useWorkspace } from "@/hooks/store"; // ui // types import { WorkspaceService } from "@/services/workspace.service"; type Props = { onSubmit?: (res: IWorkspace) => Promise; defaultValues: { name: string; slug: string; organization_size: string; }; setDefaultValues: Dispatch>; secondaryButton?: React.ReactNode; primaryButtonText?: { loading: string; default: string; }; }; const workspaceService = new WorkspaceService(); export const CreateWorkspaceForm: FC = observer((props) => { const { onSubmit, defaultValues, setDefaultValues, secondaryButton, primaryButtonText = { loading: "Creating...", default: "Create Workspace", }, } = props; // states const [slugError, setSlugError] = useState(false); const [invalidSlug, setInvalidSlug] = useState(false); // router const router = useRouter(); // store hooks const { captureWorkspaceEvent } = useEventTracker(); const { createWorkspace } = useWorkspace(); // form info const { handleSubmit, control, setValue, getValues, formState: { errors, isSubmitting, isValid }, } = useForm({ defaultValues, mode: "onChange" }); const handleCreateWorkspace = async (formData: IWorkspace) => { await workspaceService .workspaceSlugCheck(formData.slug) .then(async (res) => { if (res.status === true && !RESTRICTED_URLS.includes(formData.slug)) { setSlugError(false); await createWorkspace(formData) .then(async (res) => { captureWorkspaceEvent({ eventName: WORKSPACE_CREATED, payload: { ...res, state: "SUCCESS", element: "Create workspace page", }, }); setToast({ type: TOAST_TYPE.SUCCESS, title: "Success!", message: "Workspace created successfully.", }); if (onSubmit) await onSubmit(res); }) .catch(() => { captureWorkspaceEvent({ eventName: WORKSPACE_CREATED, payload: { state: "FAILED", element: "Create workspace page", }, }); setToast({ type: TOAST_TYPE.ERROR, title: "Error!", message: "Workspace could not be created. Please try again.", }); }); } else setSlugError(true); }) .catch(() => { setToast({ type: TOAST_TYPE.ERROR, title: "Error!", message: "Some error occurred while creating workspace. Please try again.", }); }); }; useEffect( () => () => { // when the component unmounts set the default values to whatever user typed in setDefaultValues(getValues()); }, [getValues, setDefaultValues] ); return (
/^[\w\s-]*$/.test(value) || `Name can only contain (" "), ( - ), ( _ ) & alphanumeric characters.`, maxLength: { value: 80, message: "Workspace name should not exceed 80 characters", }, }} render={({ field: { value, ref, onChange } }) => ( { onChange(e.target.value); setValue("name", e.target.value); setValue("slug", e.target.value.toLocaleLowerCase().trim().replace(/ /g, "-")); }} ref={ref} hasError={Boolean(errors.name)} placeholder="Enter workspace name..." className="w-full" /> )} />
{window && window.location.host}/ ( { /^[a-zA-Z0-9_-]+$/.test(e.target.value) ? setInvalidSlug(false) : setInvalidSlug(true); onChange(e.target.value.toLowerCase()); }} ref={ref} hasError={Boolean(errors.slug)} placeholder="Enter workspace url..." className="block w-full rounded-md border-none bg-transparent !px-0 py-2 text-sm" /> )} />
{slugError && Workspace URL is already taken!} {invalidSlug && ( {`URL can only contain ( - ), ( _ ) & alphanumeric characters.`} )}
What size is your organization?*
( c === value) ?? ( Select organization size ) } buttonClassName="!border-[0.5px] !border-custom-border-200 !shadow-none" input optionsClassName="w-full" > {ORGANIZATION_SIZE.map((item) => ( {item} ))} )} /> {errors.organization_size && ( {errors.organization_size.message} )}
{secondaryButton} {!secondaryButton && ( )}
); });