import React, { 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 { ICurrentUserResponse, IWorkspace } from "types"; // fetch-keys import { USER_WORKSPACES } from "constants/fetch-keys"; // constants import { ORGANIZATION_SIZE } from "constants/workspace"; type Props = { onSubmit?: (res: IWorkspace) => Promise; defaultValues: { name: string; slug: string; organization_size: string; }; setDefaultValues: Dispatch>; user: ICurrentUserResponse | undefined; secondaryButton?: React.ReactNode; primaryButtonText?: { loading: string; default: string; }; }; const restrictedUrls = [ "api", "installations", "404", "create-workspace", "error", "invitations", "magic-sign-in", "onboarding", "profile", "reset-password", "sign-up", "spaces", "workspace-member-invitation", ]; export const CreateWorkspaceForm: React.FC = ({ onSubmit, defaultValues, setDefaultValues, user, secondaryButton, primaryButtonText = { loading: "Creating...", default: "Create Workspace", }, }) => { const [slugError, setSlugError] = useState(false); const [invalidSlug, setInvalidSlug] = useState(false); const { setToastAlert } = useToast(); const { register, 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 && !restrictedUrls.includes(formData.slug)) { setSlugError(false); await workspaceService .createWorkspace(formData, user) .then(async (res) => { setToastAlert({ type: "success", title: "Success!", message: "Workspace created successfully.", }); mutate( USER_WORKSPACES, (prevData) => [res, ...(prevData ?? [])], false ); if (onSubmit) await onSubmit(res); }) .catch(() => setToastAlert({ type: "error", title: "Error!", message: "Workspace could not be created. Please try again.", }) ); } 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 (
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.`, maxLength: { value: 80, message: "Workspace name should not exceed 80 characters", }, }} placeholder="Enter workspace name..." error={errors.name} />
{window && window.location.host}/ /^[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.`} )}
What size is your organization?
( c === value) ?? ( Select organization size ) } input width="w-full" > {ORGANIZATION_SIZE.map((item) => ( {item} ))} )} /> {errors.organization_size && ( {errors.organization_size.message} )}
{secondaryButton} {isSubmitting ? primaryButtonText.loading : primaryButtonText.default}
); };