import React, { useState } from "react"; import Link from "next/link"; import { useRouter } from "next/router"; import useSWR from "swr"; // lib import { requiredAuth } from "lib/auth"; // services import workspaceService from "services/workspace.service"; // hooks import useUser from "hooks/use-user"; import useToast from "hooks/use-toast"; // layouts import DefaultLayout from "layouts/default-layout"; // components import SingleInvitation from "components/workspace/single-invitation"; // ui import { Button, Spinner, EmptySpace, EmptySpaceItem } from "components/ui"; // icons import { CubeIcon, PlusIcon } from "@heroicons/react/24/outline"; // types import type { NextPage, NextPageContext } from "next"; import type { IWorkspaceMemberInvitation } from "types"; const OnBoard: NextPage = () => { const [invitationsRespond, setInvitationsRespond] = useState([]); const { user } = useUser(); const router = useRouter(); const { setToastAlert } = useToast(); 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) => [...prevData, workspace_invitation.id]); } else if (action === "withdraw") { setInvitationsRespond((prevData) => prevData.filter((item: string) => item !== workspace_invitation.id) ); } }; const submitInvitations = () => { // userService.updateUserOnBoard(); if (invitationsRespond.length === 0) { setToastAlert({ type: "error", title: "Error!", message: "Please select atleast one invitation.", }); return; } 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.name}

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