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"; import userService from "services/user.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 { COMPANY_SIZE } from "constants/workspace"; type Props = { onSubmit: (res: IWorkspace) => void; defaultValues: { name: string; slug: string; company_size: number | null; }; setDefaultValues: Dispatch>; user: ICurrentUserResponse | undefined; }; const restrictedUrls = [ "api", "create-workspace", "error", "installations", "invitations", "magic-sign-in", "onboarding", "reset-password", "signin", "workspace-member-invitation", "404", ]; export const CreateWorkspaceForm: React.FC = ({ onSubmit, defaultValues, setDefaultValues, user, }) => { 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, user) .then((res) => { setToastAlert({ type: "success", title: "Success!", message: "Workspace created successfully.", }); mutate(USER_WORKSPACES, (prevData) => [res, ...(prevData ?? [])]); updateLastWorkspaceIdUnderUSer(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.", }); }); }; // update last_workspace_id const updateLastWorkspaceIdUnderUSer = (workspace: any) => { userService .updateUser({ last_workspace_id: workspace.id }) .then((res) => { onSubmit(workspace); }) .catch((err) => { console.log(err); }); }; 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" className="placeholder:text-brand-secondary" 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"}
); };