import React, { useEffect, useState } from "react"; import { useRouter } from "next/router"; import Image from "next/image"; import { mutate } from "swr"; import { Controller, useForm } from "react-hook-form"; // services import type { IWorkspace } from "types"; import type { NextPage, NextPageContext } from "next"; import workspaceService from "services/workspace.service"; // 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"; import { USER_WORKSPACES } from "constants/fetch-keys"; // types // constants import { companySize } from "constants/"; const defaultValues = { name: "", slug: "", company_size: null, }; const CreateWorkspace: NextPage = () => { const [slugError, setSlugError] = useState(false); const { register, handleSubmit, control, setError, setValue, reset, formState: { errors, isSubmitting }, } = useForm({ defaultValues }); const router = useRouter(); const onSubmit = async (formData: IWorkspace) => { await workspaceService .workspaceSlugCheck(formData.slug) .then((res) => { if (res.status === true) { workspaceService .createWorkspace(formData) .then((res) => { router.push("/"); mutate(USER_WORKSPACES, (prevData) => [res, ...(prevData ?? [])]); }) .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! )}
( {companySize?.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;