import React, { useEffect, useState } from "react"; import { useRouter } from "next/router"; import Image from "next/image"; 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"; // constants import { requiredAuth } from "lib/auth"; // layouts import DefaultLayout from "layouts/default-layout"; // ui import { CustomSelect, Input } from "components/ui"; // images import Logo from "public/onboarding/logo.svg"; // types import type { IWorkspace } from "types"; import type { NextPage, NextPageContext } from "next"; // fetch-keys import { USER_WORKSPACES } from "constants/fetch-keys"; // constants import { COMPANY_SIZE } from "constants/workspace"; const defaultValues = { name: "", slug: "", company_size: null, }; const CreateWorkspace: NextPage = () => { const [slugError, setSlugError] = useState(false); const router = useRouter(); const { setToastAlert } = useToast(); const { register, handleSubmit, control, setError, setValue, reset, formState: { errors, isSubmitting }, } = useForm({ defaultValues }); const onSubmit = async (formData: IWorkspace) => { await workspaceService .workspaceSlugCheck(formData.slug) .then(async (res) => { if (res.status === true) { setSlugError(false); await workspaceService .createWorkspace(formData) .then((res) => { setToastAlert({ type: "success", title: "Success!", message: "Workspace created successfully.", }); mutate(USER_WORKSPACES, (prevData) => [res, ...(prevData ?? [])]); router.push(`/${formData.slug}`); }) .catch((err) => { console.error(err); }); } else setSlugError(true); }) .catch((err) => { Object.keys(err).map((key) => { const errorMessage = err?.[key]; if (!errorMessage) return; setError(key as keyof IWorkspace, { message: Array.isArray(errorMessage) ? errorMessage.join(", ") : errorMessage, }); }); }); }; useEffect(() => { reset(defaultValues); }, [reset]); return (
Plane Logo
setValue("slug", e.target.value.toLocaleLowerCase().replace(/ /g, "-")) } validations={{ required: "Workspace name is required", }} error={errors.name} />
Workspace slug
{"https://app.plane.so/"}
{slugError && ( Workspace URL is already taken! )}
( {COMPANY_SIZE?.map((item) => ( {item.label} ))} )} /> {errors.company_size && ( {errors.company_size.message} )}
); }; export const getServerSideProps = async (ctx: NextPageContext) => { const user = await requiredAuth(ctx.req?.headers.cookie); const redirectAfterSignIn = ctx.req?.url; if (!user) { return { redirect: { destination: `/signin?next=${redirectAfterSignIn}`, permanent: false, }, }; } return { props: { user, }, }; }; export default CreateWorkspace;