import React, { useState } from "react"; import useSWR, { mutate } from "swr"; // services import workspaceService from "services/workspace.service"; import userService from "services/user.service"; // hooks import useUser from "hooks/use-user"; // ui import { PrimaryButton, SecondaryButton } from "components/ui"; // icons import { CheckCircleIcon } from "@heroicons/react/24/outline"; // helpers import { truncateText } from "helpers/string.helper"; // types import { ICurrentUserResponse, IUser, IWorkspaceMemberInvitation, OnboardingSteps } from "types"; // fetch-keys import { CURRENT_USER, USER_WORKSPACE_INVITATIONS } from "constants/fetch-keys"; // constants import { ROLE } from "constants/workspace"; type Props = { stepChange: (steps: Partial) => Promise; }; export const JoinWorkspaces: React.FC = ({ stepChange }) => { const [isJoiningWorkspaces, setIsJoiningWorkspaces] = useState(false); const [invitationsRespond, setInvitationsRespond] = useState([]); const { user } = useUser(); const { data: invitations, mutate: mutateInvitations } = useSWR(USER_WORKSPACE_INVITATIONS, () => workspaceService.userWorkspaceInvitations() ); 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) ); } }; // complete onboarding const finishOnboarding = async () => { if (!user) return; mutate( CURRENT_USER, (prevData) => { if (!prevData) return prevData; return { ...prevData, is_onboarded: true, }; }, false ); await userService.updateUserOnBoard({ userRole: user.role }, user); await stepChange({ workspace_join: true }); }; const submitInvitations = async () => { if (invitationsRespond.length <= 0) return; setIsJoiningWorkspaces(true); await workspaceService .joinWorkspaces({ invitations: invitationsRespond }) .then(async () => { await mutateInvitations(); await finishOnboarding(); setIsJoiningWorkspaces(false); }) .catch(() => setIsJoiningWorkspaces(false)); }; return (
We see that someone has invited you to

Join a workspace

{invitations && invitations.map((invitation) => { const isSelected = invitationsRespond.includes(invitation.id); return (
handleInvitation(invitation, isSelected ? "withdraw" : "accepted")} >
{invitation.workspace.logo && invitation.workspace.logo !== "" ? ( {invitation.workspace.name} ) : ( {invitation.workspace.name[0]} )}
{truncateText(invitation.workspace.name, 30)}

{ROLE[invitation.role]}

); })}
Accept & Join Skip for now
); };