import React, { useState } from "react"; import Link from "next/link"; import { useRouter } from "next/router"; import type { NextPage, NextPageContext } from "next"; import useSWR from "swr"; // services import workspaceService from "lib/services/workspace.service"; // hooks import useUser from "lib/hooks/useUser"; // constants import { requiredAuth } from "lib/auth"; import { USER_WORKSPACES } from "constants/fetch-keys"; import { USER_WORKSPACE_INVITATIONS } from "constants/api-routes"; // layouts import DefaultLayout from "layouts/default-layout"; // 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 { IWorkspaceMemberInvitation } from "types"; const OnBoard: NextPage = () => { const [invitationsRespond, setInvitationsRespond] = useState([]); const { user } = useUser(); const router = useRouter(); const { data: invitations, mutate: mutateInvitations } = useSWR(USER_WORKSPACE_INVITATIONS, () => workspaceService.userWorkspaceInvitations() ); const { data: workspaces, mutate: mutateWorkspaces } = useSWR(USER_WORKSPACES, () => workspaceService.userWorkspaces() ); const handleInvitation = ( workspace_invitation: IWorkspaceMemberInvitation, 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(); workspaceService .joinWorkspaces({ invitations: invitationsRespond }) .then(() => { mutateInvitations(); mutateWorkspaces(); }) .catch((err) => { console.log(err); }); }; return (
{user && (

logged in as {user.email}

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

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 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 OnBoard;