import React, { useState } from "react"; // next import Link from "next/link"; import type { NextPage } from "next"; import { useRouter } from "next/router"; // services import workspaceService from "lib/services/workspace.service"; // hooks import useUser from "lib/hooks/useUser"; // hoc import withAuthWrapper from "lib/hoc/withAuthWrapper"; // layouts import AdminLayout from "layouts/AdminLayout"; // ui import { Button } from "ui"; // swr import useSWR from "swr"; const MyWorkspacesInvites: NextPage = () => { const router = useRouter(); const [invitationsRespond, setInvitationsRespond] = useState([]); const { workspaces } = useUser(); const { data: workspaceInvitations, isValidating, mutate: mutateInvitations, } = useSWR("WORKSPACE_INVITATIONS", () => workspaceService.userWorkspaceInvitations()); const handleInvitation = (workspace_invitation: any, action: string) => { if (action === "accepted") { setInvitationsRespond((prevData: any) => { return [...prevData, workspace_invitation.workspace.id]; }); } else if (action === "withdraw") { setInvitationsRespond((prevData: any) => { return prevData.filter((item: string) => item !== workspace_invitation.workspace.id); }); } }; const submitInvitations = () => { workspaceService .joinWorkspaces({ workspace_ids: invitationsRespond }) .then(async (res) => { console.log(res); await mutateInvitations(); router.push("/"); }) .catch((err: any) => console.log); }; return (
{(workspaceInvitations as any)?.length > 0 ? ( <>

Workspace Invitations

{workspaceInvitations?.map((item: any) => (
handleInvitation( item, item.accepted ? "withdraw" : 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" />
{invitationsRespond.includes(item.workspace.id) ? (

Accepted

) : ( )}
))}
) : (
No Invitaions Found

Click Here to redirect home

)}
); }; export default withAuthWrapper(MyWorkspacesInvites);