import React, { useEffect, useState } from "react"; // next import type { NextPage } from "next"; import { useRouter } from "next/router"; // swr import useSWR from "swr"; // services import workspaceService from "lib/services/workspace.service"; import userService from "lib/services/user.service"; // hooks import useUser from "lib/hooks/useUser"; // constants import { USER_WORKSPACE_INVITATIONS } from "constants/api-routes"; // layouts import DefaultLayout from "layouts/DefaultLayout"; // components import SingleInvitation from "components/workspace/SingleInvitation"; // ui import { Button, Spinner, EmptySpace, EmptySpaceItem } from "ui"; // icons import { CubeIcon, PlusIcon } from "@heroicons/react/24/outline"; // types import type { IWorkspaceInvitation } from "types"; import Link from "next/link"; const OnBoard: NextPage = () => { const router = useRouter(); const { workspaces, mutateWorkspaces, user } = useUser(); const [invitationsRespond, setInvitationsRespond] = useState([]); const { data: invitations, mutate } = useSWR( USER_WORKSPACE_INVITATIONS, () => workspaceService.userWorkspaceInvitations() ); const handleInvitation = ( workspace_invitation: IWorkspaceInvitation, action: "accepted" | "withdraw" ) => { if (action === "accepted") { setInvitationsRespond((prevData) => { return [...prevData, workspace_invitation.id]; }); } else if (action === "withdraw") { setInvitationsRespond((prevData) => { return prevData.filter((item: string) => item !== workspace_invitation.id); }); } }; const submitInvitations = () => { userService.updateUserOnBoard().then((response) => { console.log(response); }); workspaceService .joinWorkspaces({ invitations: invitationsRespond }) .then(async (res: any) => { console.log(res); await mutate(); await mutateWorkspaces(); router.push("/workspace"); }) .catch((err) => { console.log(err); }); }; // useEffect(() => { // if (workspaces && workspaces.length === 0) setCanRedirect(false); // }, [workspaces]); return (
{user && (

logged in as {user.email}

)}
{invitations && workspaces ? ( invitations.length > 0 ? (

Join your workspaces

{invitations.map((item) => (

{item.workspace.name}

invited by {item.workspace.owner.first_name}

{ handleInvitation( item, invitationsRespond.includes(item.id) ? "withdraw" : "accepted" ); }} type="checkbox" className="h-4 w-4 rounded border-gray-300 text-indigo-600 focus:ring-indigo-500" />
))}

Workspace Invitations

Select invites that you want to accept.

    {invitations.map((invitation) => ( ))}
) : workspaces && workspaces.length > 0 ? (

Your workspaces

{workspaces.map((workspace) => (

{workspace.owner.first_name}

))}
) : ( invitations.length === 0 && workspaces.length === 0 && ( { router.push("/create-workspace"); }} /> ) ) ) : (
)}
); }; export default OnBoard;