import React, { useEffect, useState } from "react"; // next import type { NextPage } from "next"; import Link from "next/link"; import { useRouter } from "next/router"; // swr import useSWR from "swr"; // headless ui import { Dialog, Transition } from "@headlessui/react"; // 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"; // hoc import withAuthWrapper from "lib/hoc/withAuthWrapper"; // layouts import DefaultLayout from "layouts/DefaultLayout"; // ui import { Button, Spinner } from "ui"; // types import type { IWorkspaceInvitation } from "types"; import { ChartBarIcon, ChevronRightIcon, CubeIcon, PlusIcon } from "@heroicons/react/24/outline"; import { EmptySpace, EmptySpaceItem } from "ui/EmptySpace"; const OnBoard: NextPage = () => { const [canRedirect, setCanRedirect] = useState(true); 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(); }) .catch((err) => { console.log(err); }); }; // useEffect(() => { // if (!invitations) return; // else // invitations.forEach((invite) => { // if (invite.accepted) // setInvitationsRespond((prevData) => { // return [...prevData, invite.workspace.id]; // }); // }); // }, [invitations, router, workspaces]); useEffect(() => { if (workspaces && workspaces.length === 0) { setCanRedirect(false); } }, [workspaces]); return (
{user && (

logged in as {user.email}

)}
{invitations ? ( 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" />
{/*
{ handleInvitation(item, invitationsRespond.includes(item.workspace.id) ? "withdraw" : "accepted"); }} type="checkbox" className="h-4 w-4 rounded border-gray-300 text-indigo-600 focus:ring-indigo-500" />
*/}
))}
) : ( { userService.updateUserOnBoard().then((response) => { console.log(response); }); router.push(canRedirect ? "/" : "/create-workspace"); }} /> ) ) : (
)}
); }; export default withAuthWrapper(OnBoard);