2023-07-12 14:25:08 +00:00
|
|
|
import React, { useState } from "react";
|
|
|
|
|
|
|
|
import useSWR, { mutate } from "swr";
|
|
|
|
|
|
|
|
// services
|
|
|
|
import workspaceService from "services/workspace.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
|
2023-07-31 11:53:49 +00:00
|
|
|
import { IWorkspaceMemberInvitation, TOnboardingSteps } from "types";
|
2023-07-12 14:25:08 +00:00
|
|
|
// fetch-keys
|
2023-07-31 11:53:49 +00:00
|
|
|
import { USER_WORKSPACES, USER_WORKSPACE_INVITATIONS } from "constants/fetch-keys";
|
2023-07-12 14:25:08 +00:00
|
|
|
// constants
|
|
|
|
import { ROLE } from "constants/workspace";
|
|
|
|
|
|
|
|
type Props = {
|
2023-07-31 11:53:49 +00:00
|
|
|
finishOnboarding: () => Promise<void>;
|
|
|
|
stepChange: (steps: Partial<TOnboardingSteps>) => Promise<void>;
|
|
|
|
updateLastWorkspace: () => Promise<void>;
|
2023-07-12 14:25:08 +00:00
|
|
|
};
|
|
|
|
|
2023-07-31 11:53:49 +00:00
|
|
|
export const JoinWorkspaces: React.FC<Props> = ({
|
|
|
|
finishOnboarding,
|
|
|
|
stepChange,
|
|
|
|
updateLastWorkspace,
|
|
|
|
}) => {
|
2023-07-12 14:25:08 +00:00
|
|
|
const [isJoiningWorkspaces, setIsJoiningWorkspaces] = useState(false);
|
|
|
|
const [invitationsRespond, setInvitationsRespond] = useState<string[]>([]);
|
|
|
|
|
|
|
|
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)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-07-31 11:53:49 +00:00
|
|
|
const handleNextStep = async () => {
|
2023-07-12 14:25:08 +00:00
|
|
|
if (!user) return;
|
|
|
|
|
|
|
|
await stepChange({ workspace_join: true });
|
2023-07-31 11:53:49 +00:00
|
|
|
|
|
|
|
if (user.onboarding_step.workspace_create && user.onboarding_step.workspace_invite)
|
|
|
|
await finishOnboarding();
|
2023-07-12 14:25:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const submitInvitations = async () => {
|
|
|
|
if (invitationsRespond.length <= 0) return;
|
|
|
|
|
|
|
|
setIsJoiningWorkspaces(true);
|
|
|
|
|
|
|
|
await workspaceService
|
|
|
|
.joinWorkspaces({ invitations: invitationsRespond })
|
|
|
|
.then(async () => {
|
|
|
|
await mutateInvitations();
|
2023-07-31 11:53:49 +00:00
|
|
|
await mutate(USER_WORKSPACES);
|
|
|
|
await updateLastWorkspace();
|
2023-07-12 14:25:08 +00:00
|
|
|
|
2023-07-31 11:53:49 +00:00
|
|
|
await handleNextStep();
|
2023-07-12 14:25:08 +00:00
|
|
|
})
|
2023-07-31 11:53:49 +00:00
|
|
|
.finally(() => setIsJoiningWorkspaces(false));
|
2023-07-12 14:25:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="w-full space-y-7 sm:space-y-10">
|
|
|
|
<h5 className="sm:text-lg">We see that someone has invited you to</h5>
|
|
|
|
<h4 className="text-xl sm:text-2xl font-semibold">Join a workspace</h4>
|
2023-07-20 09:44:57 +00:00
|
|
|
<div className="max-h-[37vh] overflow-y-auto md:w-3/5 space-y-4">
|
2023-07-12 14:25:08 +00:00
|
|
|
{invitations &&
|
|
|
|
invitations.map((invitation) => {
|
|
|
|
const isSelected = invitationsRespond.includes(invitation.id);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
key={invitation.id}
|
|
|
|
className={`flex cursor-pointer items-center gap-2 border py-5 px-3.5 rounded ${
|
|
|
|
isSelected
|
|
|
|
? "border-custom-primary-100"
|
2023-07-17 10:58:23 +00:00
|
|
|
: "border-custom-border-200 hover:bg-custom-background-80"
|
2023-07-12 14:25:08 +00:00
|
|
|
}`}
|
|
|
|
onClick={() => handleInvitation(invitation, isSelected ? "withdraw" : "accepted")}
|
|
|
|
>
|
|
|
|
<div className="flex-shrink-0">
|
|
|
|
<div className="grid place-items-center h-9 w-9 rounded">
|
|
|
|
{invitation.workspace.logo && invitation.workspace.logo !== "" ? (
|
|
|
|
<img
|
|
|
|
src={invitation.workspace.logo}
|
|
|
|
height="100%"
|
|
|
|
width="100%"
|
|
|
|
className="rounded"
|
|
|
|
alt={invitation.workspace.name}
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<span className="grid place-items-center h-9 w-9 py-1.5 px-3 rounded bg-gray-700 uppercase text-white">
|
|
|
|
{invitation.workspace.name[0]}
|
|
|
|
</span>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div className="min-w-0 flex-1">
|
|
|
|
<div className="text-sm font-medium">
|
|
|
|
{truncateText(invitation.workspace.name, 30)}
|
|
|
|
</div>
|
|
|
|
<p className="text-xs text-custom-text-200">{ROLE[invitation.role]}</p>
|
|
|
|
</div>
|
|
|
|
<span
|
|
|
|
className={`flex-shrink-0 ${
|
|
|
|
isSelected ? "text-custom-primary-100" : "text-custom-text-200"
|
|
|
|
}`}
|
|
|
|
>
|
|
|
|
<CheckCircleIcon className="h-5 w-5" />
|
|
|
|
</span>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
</div>
|
|
|
|
<div className="flex items-center gap-3">
|
|
|
|
<PrimaryButton
|
|
|
|
type="submit"
|
|
|
|
size="md"
|
|
|
|
onClick={submitInvitations}
|
2023-07-31 11:53:49 +00:00
|
|
|
disabled={invitationsRespond.length === 0}
|
|
|
|
loading={isJoiningWorkspaces}
|
2023-07-12 14:25:08 +00:00
|
|
|
>
|
|
|
|
Accept & Join
|
|
|
|
</PrimaryButton>
|
2023-07-20 09:44:57 +00:00
|
|
|
<SecondaryButton
|
|
|
|
className="border border-none bg-transparent"
|
|
|
|
size="md"
|
2023-07-31 11:53:49 +00:00
|
|
|
onClick={handleNextStep}
|
2023-07-20 09:44:57 +00:00
|
|
|
>
|
2023-07-12 14:25:08 +00:00
|
|
|
Skip for now
|
|
|
|
</SecondaryButton>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|