import { useEffect, useState } from "react"; import Image from "next/image"; import useSWR from "swr"; import { Controller, useForm } from "react-hook-form"; import { Tab } from "@headlessui/react"; // hooks import useToast from "hooks/use-toast"; // services import workspaceService from "services/workspace.service"; // ui import { CustomSelect, Input } from "components/ui"; // types import { IWorkspace, IWorkspaceMemberInvitation } from "types"; // constants import { companySize } from "constants/"; // fetch-keys import { USER_WORKSPACE_INVITATIONS } from "constants/fetch-keys"; type Props = { setStep: React.Dispatch>; setWorkspace: React.Dispatch>; }; const defaultValues: Partial = { name: "", slug: "", company_size: null, }; const Workspace: React.FC = ({ setStep, setWorkspace }) => { const [slugError, setSlugError] = useState(false); const [isJoiningWorkspaces, setIsJoiningWorkspaces] = useState(false); const [invitationsRespond, setInvitationsRespond] = useState([]); const { setToastAlert } = useToast(); const { data: invitations, mutate } = useSWR(USER_WORKSPACE_INVITATIONS, () => workspaceService.userWorkspaceInvitations() ); const { register, handleSubmit, control, setValue, reset, formState: { errors, isSubmitting }, } = useForm({ defaultValues }); const handleCreateWorkspace = async (formData: IWorkspace) => { await workspaceService .workspaceSlugCheck(formData.slug) .then(async (res) => { if (res.status === true) { setSlugError(false); await workspaceService .createWorkspace(formData) .then((res) => { console.log(res); setToastAlert({ type: "success", title: "Success!", message: "Workspace created successfully.", }); setWorkspace(res); setStep(3); }) .catch((err) => { console.error(err); }); } else setSlugError(true); }) .catch((err) => { console.error(err); }); }; const handleInvitation = ( workspace_invitation: IWorkspaceMemberInvitation, action: "accepted" | "withdraw" ) => { if (action === "accepted") { setInvitationsRespond((prevData) => [...prevData, workspace_invitation.id]); } else if (action === "withdraw") { setInvitationsRespond((prevData) => prevData.filter((item: string) => item !== workspace_invitation.id) ); } }; const submitInvitations = async () => { if (invitationsRespond.length <= 0) return; setIsJoiningWorkspaces(true); await workspaceService .joinWorkspaces({ invitations: invitationsRespond }) .then(async () => { await mutate(); setStep(4); setIsJoiningWorkspaces(false); }) .catch((err) => { console.error(err); setIsJoiningWorkspaces(false); }); }; useEffect(() => { reset(defaultValues); }, [reset]); return (
`rounded-lg px-6 py-2 ${selected ? "bg-gray-300" : "hover:bg-gray-200"}` } > New workspace `rounded-lg px-6 py-2 ${selected ? "bg-gray-300" : "hover:bg-gray-200"}` } > Invited workspaces
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} )}
{invitations && invitations.length > 0 ? ( invitations.map((invitation) => (
)) ) : (

You have no invitations

)}
); }; export default Workspace;