import { Dispatch, SetStateAction, useEffect, useState } from "react"; import { mutate } from "swr"; // react-hook-form import { Controller, useForm } from "react-hook-form"; // services import workspaceService from "services/workspace.service"; // hooks import useToast from "hooks/use-toast"; // ui import { CustomSelect, Input, PrimaryButton } from "components/ui"; // types import { IWorkspace } from "types"; // fetch-keys import { USER_WORKSPACES } from "constants/fetch-keys"; // constants import { COMPANY_SIZE } from "constants/workspace"; type Props = { onSubmit: (res: IWorkspace) => void; defaultValues: { name: string; slug: string; company_size: number | null; }; setDefaultValues: Dispatch>; }; const restrictedUrls = [ "api", "create-workspace", "error", "installations", "invitations", "magic-sign-in", "onboarding", "signin", "workspace-member-invitation", "404", ]; export const CreateWorkspaceForm: React.FC = ({ onSubmit, defaultValues, setDefaultValues, }) => { const [slugError, setSlugError] = useState(false); const [invalidSlug, setInvalidSlug] = useState(false); const { setToastAlert } = useToast(); const { register, handleSubmit, control, setValue, getValues, formState: { errors, isSubmitting }, } = useForm({ defaultValues }); const handleCreateWorkspace = async (formData: IWorkspace) => { await workspaceService .workspaceSlugCheck(formData.slug) .then(async (res) => { if (res.status === true && !restrictedUrls.includes(formData.slug)) { setSlugError(false); await workspaceService .createWorkspace(formData) .then((res) => { setToastAlert({ type: "success", title: "Success!", message: "Workspace created successfully.", }); mutate(USER_WORKSPACES, (prevData) => [res, ...(prevData ?? [])]); onSubmit(res); }) .catch((err) => { console.error(err); }); } else setSlugError(true); }) .catch(() => { setToastAlert({ 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 (
Workspace name setValue("slug", e.target.value.toLocaleLowerCase().trim().replace(/ /g, "-")) } validations={{ required: "Workspace name is required", validate: (value) => /^[\w\s-]*$/.test(value) || `Name can only contain (" "), ( - ), ( _ ) & Alphanumeric characters.`, }} placeholder="e.g. My Workspace" error={errors.name} />
Workspace URL
{typeof window !== "undefined" && window.location.origin}/ /^[a-zA-Z0-9_-]+$/.test(e.target.value) ? setInvalidSlug(false) : setInvalidSlug(true) } />
{slugError && ( Workspace URL is already taken! )} {invalidSlug && ( {`URL can only contain ( - ), ( _ ) & Alphanumeric characters.`} )}
How large is your company?
( Select company size ) } input width="w-full" > {COMPANY_SIZE?.map((item) => ( {item.label} ))} )} /> {errors.company_size && ( {errors.company_size.message} )}
{isSubmitting ? "Creating..." : "Create Workspace"}
); };