refactor: polishing of admin dashboard teams module (#64)

Co-authored-by: Anwarul Islam <anwaarulislaam@gmail.com>
This commit is contained in:
Joel Jacob Stephen
2023-04-04 13:48:02 +05:30
committed by GitHub
parent ea847d7d32
commit e27dc1f7a2
20 changed files with 1097 additions and 1447 deletions

View File

@@ -1,66 +0,0 @@
<template>
<div class="flex justify-between">
<h3 class="mt-10 text-2xl justify-start font-medium text-gray-200">
Members
</h3>
<div class="mt-8 ml-2">
<button
@click="addNewMember"
class="px-4 py-2 justify-right text-gray-200 bg-emerald-900 rounded-md hover:bg-emerald-700 focus:outline-none focus:bg-emerald-800"
>
Add New
</button>
</div>
</div>
<div>
<div
v-for="(member, i) in members"
:key="i"
class="mt-3 flex justify-center"
>
<input
type="text"
placeholder="Enter email"
v-model="member.name"
class="bg-primaryDark p-2 border-primaryLight w-90 focus:border-emerald-600 focus:ring focus:ring-opacity-40 focus:ring-emerald-500"
/>
<select
v-model="member.role"
class="bg-primaryDark ml-0.5 p-2 pr-3 focus:border-emerald-600 focus:ring focus:ring-opacity-40 focus:ring-emerald-500"
>
<option value="owner">Owner</option>
<option value="editor">Editor</option>
<option value="viewer">Viewer</option>
</select>
<div
class="bg-primaryDark flex items-center ml-0.5 border-primaryLight px-2 text-red-600 focus:border-emerald-600 focus:ring focus:ring-opacity-40 focus:ring-emerald-500"
>
<span class="flex" @click="deleteMemberRow(i)">
<icon-lucide-trash />
</span>
</div>
</div>
</div>
</template>
<script setup lang="ts">
import { reactive } from 'vue';
const members = reactive([
{
name: '',
role: 'owner',
},
]);
const addNewMember = () => {
members.push({
name: '',
role: 'admin',
});
};
const deleteMemberRow = (index: number) => {
members.splice(index, 1);
};
</script>

View File

@@ -1,483 +0,0 @@
<template>
<div class="sm:px-6 p-4" v-if="team">
<h3 class="text-3xl font-medium text-zinc-800 dark:text-gray-200">
Team Details
</h3>
<div class="mt-5">
<div class="grid gap-4">
<div class="w-full">
<div
class="p-4 bg-zinc-200 dark:bg-zinc-800 rounded-md shadow-sm flex flex-col gap-1"
>
<div class="flex mb-6">
<icon-lucide-user class="text-emerald-400 text-3xl" />
<h4
class="text-3xl ml-2 font-semibold text-gray-700 dark:text-gray-200"
>
Team Info
</h4>
</div>
<div class="flex text-xl">
<h4 class="font-semibold text-gray-700 dark:text-gray-400">
Team ID:
</h4>
<div class="text-gray-600 dark:text-gray-200 ml-2">
{{ team.id }}
</div>
</div>
<div class="flex text-xl items-center">
<h4 class="font-semibold text-gray-700 dark:text-gray-400">
Team Name:
</h4>
<div class="text-gray-600 dark:text-gray-200 ml-2">
<div v-if="showRenameInput" class="flex gap-4 items-center">
<input
class="w-full py-1 px-3 dark:bg-zinc-800 border-gray-200 dark:border-gray-600 rounded-md focus:border-emerald-600 focus:ring focus:ring-opacity-40 focus:ring-emerald-500 border"
type="text"
v-model="teamName"
placeholder="Team Name"
autofocus
v-focus
/>
<div>
<HoppButtonPrimary
class="py-1"
filled
label="Rename"
@click="renameTeamName()"
/>
</div>
</div>
<div v-else class="flex gap-2">
<span>
{{ teamName }}
</span>
<icon-lucide-edit
class="cursor-pointer"
@click="showRenameInput = true"
/>
</div>
</div>
</div>
<div class="flex text-xl">
<h4 class="font-semibold text-gray-700 dark:text-gray-400">
Number of members:
</h4>
<div class="text-gray-600 dark:text-gray-200 ml-2">
{{ team.teamMembers.length }}
</div>
</div>
<div class="flex justify-start mt-6">
<HoppButtonSecondary
class="mr-4"
filled
label="Delete"
@click="team && deleteTeam(team.id)"
/>
</div>
</div>
</div>
</div>
</div>
<div class="my-6">
<div class="flex flex-col mb-6">
<div class="flex items-center justify-between flex-1 pt-4 mb-4">
<label for="memberList"> Members </label>
<div class="flex">
<HoppButtonSecondary
:icon="IconUserPlus"
label="Add Members"
filled
@click="showInvite = !showInvite"
/>
</div>
</div>
<div class="border rounded border-divider">
<div
v-if="team?.teamMembers?.length === 0"
class="flex flex-col items-center justify-center p-4 text-secondaryLight"
>
<img
:src="`/images/states/dark/add_group.svg`"
loading="lazy"
class="inline-flex flex-col object-contain object-center w-16 h-16 my-4"
/>
<span class="pb-4 text-center">
No members in this team. Add members to this team to collaborate
</span>
<HoppButtonSecondary
:icon="IconUserPlus"
label="Add Members"
@click="
() => {
showInvite = !showInvite;
}
"
/>
</div>
<div v-else class="divide-y divide-dividerLight">
<div
v-for="(member, index) in membersList"
:key="`member-${index}`"
class="flex divide-x divide-dividerLight"
>
<input
class="flex flex-1 px-4 py-2 bg-transparent"
placeholder="Email"
:name="'param' + index"
:value="member.email"
readonly
/>
<span>
<tippy
interactive
trigger="click"
theme="popover"
:on-shown="() => tippyActions![index].focus()"
>
<span class="select-wrapper">
<input
class="flex flex-1 px-4 py-2 bg-transparent cursor-pointer"
placeholder="Permissions"
:name="'value' + index"
:value="member.role"
readonly
/>
</span>
<template #content="{ hide }">
<div
ref="tippyActions"
class="flex flex-col focus:outline-none"
tabindex="0"
@keyup.escape="hide()"
>
<HoppSmartItem
label="OWNER"
:icon="
member.role === 'OWNER' ? IconCircleDot : IconCircle
"
:active="member.role === 'OWNER'"
@click="
() => {
updateMemberRole(
member.userID,
TeamMemberRole.Owner
);
hide();
}
"
/>
<HoppSmartItem
label="EDITOR"
:icon="
member.role === 'EDITOR' ? IconCircleDot : IconCircle
"
:active="member.role === 'EDITOR'"
@click="
() => {
updateMemberRole(
member.userID,
TeamMemberRole.Editor
);
hide();
}
"
/>
<HoppSmartItem
label="VIEWER"
:icon="
member.role === 'VIEWER' ? IconCircleDot : IconCircle
"
:active="member.role === 'VIEWER'"
@click="
() => {
updateMemberRole(
member.userID,
TeamMemberRole.Viewer
);
hide();
}
"
/>
</div>
</template>
</tippy>
</span>
<div class="flex">
<HoppButtonSecondary
id="member"
v-tippy="{ theme: 'tooltip' }"
title="Remove"
:icon="IconUserMinus"
color="red"
:loading="isLoadingIndex === index"
@click="removeExistingTeamMember(member.userID, index)"
/>
</div>
</div>
</div>
</div>
<div v-if="!fetching && !team" class="flex flex-col items-center">
<component :is="IconHelpCircle" class="mb-4 svg-icons" />
Something went wrong. Please try again later.
</div>
</div>
<HoppButtonPrimary
label="Save"
:loading="isLoading"
outline
@click="saveTeam"
/>
</div>
<TeamsPendingInvites :editingTeamID="team.id" />
<HoppSmartConfirmModal
:show="confirmDeletion"
:title="`Confirm Deletion of ${team.name} team?`"
@hide-modal="confirmDeletion = false"
@resolve="deleteTeamMutation(deleteTeamUID)"
/>
<TeamsInvite
:show="showInvite"
:editingTeamID="team.id"
@hide-modal="
() => {
showInvite = false;
}
"
/>
</div>
</template>
<script setup lang="ts">
import IconCircleDot from '~icons/lucide/circle-dot';
import IconCircle from '~icons/lucide/circle';
import IconUserPlus from '~icons/lucide/user-plus';
import IconUserMinus from '~icons/lucide/user-minus';
import IconHelpCircle from '~icons/lucide/help-circle';
import { useClientHandle, useMutation } from '@urql/vue';
import { computed, onMounted, ref, watch } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { useToast } from '../../composables/toast';
import {
RemoveTeamDocument,
RenameTeamDocument,
TeamInfoDocument,
TeamMemberRole,
UpdateTeamMemberRoleDocument,
RemoveTeamMemberDocument,
TeamInfoQuery,
} from '../../helpers/backend/graphql';
const route = useRoute();
const router = useRouter();
const toast = useToast();
const team = ref<TeamInfoQuery['team'] | null>(null);
const showRenameInput = ref(false);
const showInvite = ref(false);
const teamName = ref('');
const { client } = useClientHandle();
const fetching = ref(true);
const teamDeletion = useMutation(RemoveTeamDocument);
const teamRename = useMutation(RenameTeamDocument);
const confirmDeletion = ref(false);
const deleteTeamUID = ref<string | null>(null);
onMounted(async () => {
await getTeamInfo();
});
const getTeamInfo = async () => {
fetching.value = true;
const result = await client
.query(TeamInfoDocument, { uid: route.params.id.toString() })
.toPromise();
if (result.error) {
return toast.error('Unable to Load Team Info..');
}
if (result.data?.team) {
team.value = result.data.team;
teamName.value = team.value.name;
}
fetching.value = false;
};
const renameTeamName = async () => {
if (!team.value) return;
if (team.value.name === teamName.value) {
showRenameInput.value = false;
return;
}
const variables = { uid: team.value.id, name: teamName.value };
await teamRename.executeMutation(variables).then((result) => {
if (result.error) {
toast.error('Renaming team Failed');
} else {
showRenameInput.value = false;
toast.success('Team Renamed Successfully');
}
});
};
const deleteTeam = (id: string) => {
confirmDeletion.value = true;
deleteTeamUID.value = id;
};
const deleteTeamMutation = async (id: string | null) => {
if (!id) {
confirmDeletion.value = false;
toast.error('User Deletion Failed');
return;
}
const variables = { uid: id };
await teamDeletion.executeMutation(variables).then((result) => {
if (result.error) {
toast.error('User Deletion Failed');
} else {
toast.success('User Deleted Successfully');
}
});
confirmDeletion.value = false;
deleteTeamUID.value = null;
router.push('/teams');
};
const emit = defineEmits<{
(e: 'refetch-teams'): void;
(e: 'invite-team'): void;
}>();
// Template refs
const tippyActions = ref<any | null>(null);
const roleUpdates = ref<
{
userID: string;
role: TeamMemberRole;
}[]
>([]);
watch(
() => team.value,
(teamDetails) => {
const members = teamDetails?.teamMembers ?? [];
// Remove deleted members
roleUpdates.value = roleUpdates.value.filter(
(update) => members.findIndex((y) => y.user.uid === update.userID) !== -1
);
}
);
const updateMemberRole = (userID: string, role: TeamMemberRole) => {
const updateIndex = roleUpdates.value.findIndex(
(item) => item.userID === userID
);
if (updateIndex !== -1) {
// Role Update exists
roleUpdates.value[updateIndex].role = role;
} else {
// Role Update does not exist
roleUpdates.value.push({
userID,
role,
});
}
};
const membersList = computed(() => {
if (!team.value) return [];
const members = (team.value.teamMembers ?? []).map((member) => {
const updatedRole = roleUpdates.value.find(
(update) => update.userID === member.user.uid
);
return {
userID: member.user.uid,
email: member.user.email!,
role: updatedRole?.role ?? member.role,
};
});
return members;
});
const isLoadingIndex = ref<null | number>(null);
const removeExistingTeamMember = async (userID: string, index: number) => {
if (!team.value) return;
isLoadingIndex.value = index;
const removeTeamMemberResult = await removeTeamMember(
userID,
team.value.id
)();
if (removeTeamMemberResult.error) {
toast.error(`Something went wrong`);
} else {
toast.success(`Member removed successfully`);
emit('refetch-teams');
getTeamInfo();
}
isLoadingIndex.value = null;
};
const isLoading = ref(false);
const saveTeam = async () => {
isLoading.value = true;
roleUpdates.value.forEach(async (update) => {
if (!team.value) return;
const updateMemberRoleResult = await updateTeamMemberRole(
update.userID,
team.value.id,
update.role
);
if (updateMemberRoleResult.error) {
toast.error(`Something went wrong`);
} else {
toast.success(`Roles updated successfully`);
}
isLoading.value = false;
});
};
const updateTeamMemberRoleMutation = useMutation(UpdateTeamMemberRoleDocument);
const updateTeamMemberRole = (
userUid: string,
teamID: string,
newRole: TeamMemberRole
) => {
return updateTeamMemberRoleMutation.executeMutation({
userUid,
teamID,
newRole,
});
};
const removeTeamMemberMutation = useMutation(RemoveTeamMemberDocument);
const removeTeamMember = (userUid: string, teamID: string) => {
return () =>
removeTeamMemberMutation.executeMutation({
userUid,
teamID,
});
};
</script>

View File

@@ -1,45 +1,7 @@
<template>
<HoppSmartModal v-if="show" dialog title="Add Member" @close="hideModal">
<template #body>
<div v-if="sendInvitesResult.length" class="flex flex-col px-4">
<div class="flex flex-col items-center justify-center max-w-md">
<icon-lucide-users class="w-6 h-6 text-accent" />
<h3 class="my-2 text-lg text-center">
We have sent you an invite link
</h3>
<p class="text-center">
You can use this link to invite your team members to join your team.
</p>
</div>
<div
class="flex flex-col p-4 mt-8 border rounded space-y-6 border-dividerLight"
>
<div
v-for="(invitee, index) in sendInvitesResult"
:key="`invitee-${index}`"
>
<p class="flex items-center">
<component
:is="
invitee.status === 'error' ? IconAlertTriangle : IconMailCheck
"
class="mr-4 svg-icons"
:class="
invitee.status === 'error' ? 'text-red-500' : 'text-green-500'
"
/>
<span class="truncate">{{ invitee.email }}</span>
</p>
<p v-if="invitee.status === 'error'" class="mt-2 ml-8 text-red-500">
{{ getErrorMessage(invitee.error) }}
</p>
</div>
</div>
</div>
<div
v-else-if="sendingInvites"
class="flex items-center justify-center p-4"
>
<div v-if="addingUserToTeam" class="flex items-center justify-center p-4">
<HoppSmartSpinner />
</div>
<div v-else class="flex flex-col">
@@ -50,21 +12,21 @@
:icon="IconPlus"
label="Add new"
filled
@click="addNewInvitee"
@click="addNewMember"
/>
</div>
</div>
<div class="border rounded divide-y divide-dividerLight border-divider">
<div
v-for="(invitee, index) in newInvites"
:key="`new-invitee-${index}`"
v-for="(member, index) in newMembersList"
:key="`new-member-${index}`"
class="flex divide-x divide-dividerLight"
>
<input
v-model="invitee.key"
v-model="member.key"
class="flex flex-1 px-4 py-2 bg-transparent"
placeholder="Email"
:name="'invitee' + index"
:name="'member' + index"
autofocus
/>
<span>
@@ -79,7 +41,7 @@
class="flex flex-1 px-4 py-2 bg-transparent cursor-pointer"
placeholder="Permissions"
:name="'value' + index"
:value="invitee.value"
:value="member.value"
readonly
/>
</span>
@@ -93,12 +55,12 @@
<HoppSmartItem
label="OWNER"
:icon="
invitee.value === 'OWNER' ? IconCircleDot : IconCircle
member.value === 'OWNER' ? IconCircleDot : IconCircle
"
:active="invitee.value === 'OWNER'"
:active="member.value === 'OWNER'"
@click="
() => {
updateNewInviteeRole(index, TeamMemberRole.Owner);
updateNewMemberRole(index, TeamMemberRole.Owner);
hide();
}
"
@@ -106,12 +68,12 @@
<HoppSmartItem
label="EDITOR"
:icon="
invitee.value === 'EDITOR' ? IconCircleDot : IconCircle
member.value === 'EDITOR' ? IconCircleDot : IconCircle
"
:active="invitee.value === 'EDITOR'"
:active="member.value === 'EDITOR'"
@click="
() => {
updateNewInviteeRole(index, TeamMemberRole.Editor);
updateNewMemberRole(index, TeamMemberRole.Editor);
hide();
}
"
@@ -119,12 +81,12 @@
<HoppSmartItem
label="VIEWER"
:icon="
invitee.value === 'VIEWER' ? IconCircleDot : IconCircle
member.value === 'VIEWER' ? IconCircleDot : IconCircle
"
:active="invitee.value === 'VIEWER'"
:active="member.value === 'VIEWER'"
@click="
() => {
updateNewInviteeRole(index, TeamMemberRole.Viewer);
updateNewMemberRole(index, TeamMemberRole.Viewer);
hide();
}
"
@@ -140,12 +102,12 @@
title="Remove"
:icon="IconTrash"
color="red"
@click="removeNewInvitee(index)"
@click="removeNewMember(index)"
/>
</div>
</div>
<div
v-if="newInvites.length === 0"
v-if="newMembersList.length === 0"
class="flex flex-col items-center justify-center p-4 text-secondaryLight"
>
<img
@@ -154,15 +116,11 @@
class="inline-flex flex-col object-contain object-center w-16 h-16 mb-4"
/>
<span class="pb-4 text-center"> No invites </span>
<HoppButtonSecondary
label="Add new"
filled
@click="addNewInvitee"
/>
<HoppButtonSecondary label="Add new" filled @click="addNewMember" />
</div>
</div>
<div
v-if="newInvites.length"
v-if="newMembersList.length"
class="flex flex-col items-start px-4 py-4 mt-4 border rounded border-dividerLight"
>
<span
@@ -217,34 +175,12 @@
</template>
<template #footer>
<p
v-if="sendInvitesResult.length"
class="flex justify-between flex-1 text-secondaryLight"
>
<HoppButtonSecondary
class="link !p-0"
label="Invite more"
:icon="IconArrowLeft"
@click="
() => {
sendInvitesResult = [];
newInvites = [
{
key: '',
value: TeamMemberRole.Viewer,
},
];
}
"
<span class="flex space-x-2">
<HoppButtonPrimary
label="Add Member"
outline
@click="addUserasTeamMember"
/>
<HoppButtonSecondary
class="link !p-0"
label="Dismiss"
@click="hideModal"
/>
</p>
<span v-else class="flex space-x-2">
<HoppButtonPrimary label="Invite" outline @click="sendInvites" />
<HoppButtonSecondary label="Cancel" outline filled @click="hideModal" />
</span>
</template>
@@ -257,22 +193,17 @@ import * as A from 'fp-ts/Array';
import * as O from 'fp-ts/Option';
import { pipe } from 'fp-ts/function';
import {
AddUserToTeamByAdminDocument,
TeamMemberRole,
CreateTeamInvitationDocument,
} from '../../helpers/backend/graphql';
import { useToast } from '~/composables/toast';
import { useMutation } from '@urql/vue';
import { Email, EmailCodec } from '~/helpers/backend/Email';
import IconTrash from '~icons/lucide/trash';
import IconPlus from '~icons/lucide/plus';
import IconHelpCircle from '~icons/lucide/help-circle';
import IconAlertTriangle from '~icons/lucide/alert-triangle';
import IconMailCheck from '~icons/lucide/mail-check';
import IconCircleDot from '~icons/lucide/circle-dot';
import IconCircle from '~icons/lucide/circle';
import IconArrowLeft from '~icons/lucide/arrow-left';
import { useMutation } from '@urql/vue';
import { Email, EmailCodec } from '~/helpers/backend/Email';
const toast = useToast();
@@ -286,97 +217,67 @@ const props = defineProps({
const emit = defineEmits<{
(e: 'hide-modal'): void;
(e: 'member'): void;
}>();
const t = (key: string) => key;
const newInvites = ref<Array<{ key: string; value: TeamMemberRole }>>([
const newMembersList = ref<Array<{ key: string; value: TeamMemberRole }>>([
{
key: '',
value: TeamMemberRole.Viewer,
},
]);
const addNewInvitee = () => {
newInvites.value.push({
const addNewMember = () => {
newMembersList.value.push({
key: '',
value: TeamMemberRole.Viewer,
});
};
const updateNewInviteeRole = (index: number, role: TeamMemberRole) => {
newInvites.value[index].value = role;
const updateNewMemberRole = (index: number, role: TeamMemberRole) => {
newMembersList.value[index].value = role;
};
const removeNewInvitee = (id: number) => {
newInvites.value.splice(id, 1);
const removeNewMember = (id: number) => {
newMembersList.value.splice(id, 1);
};
type SendInvitesErrorType =
| {
email: Email;
status: 'error';
error: any;
}
| {
email: Email;
status: 'success';
};
const addingUserToTeam = ref<boolean>(false);
const sendInvitesResult = ref<Array<SendInvitesErrorType>>([]);
const sendingInvites = ref<boolean>(false);
const sendInvites = async () => {
sendingInvites.value = true;
const addUserasTeamMember = async () => {
addingUserToTeam.value = true;
const validationResult = pipe(
newInvites.value,
newMembersList.value,
O.fromPredicate(
(invites): invites is Array<{ key: Email; value: TeamMemberRole }> =>
(
memberInvites
): memberInvites is Array<{ key: Email; value: TeamMemberRole }> =>
pipe(
invites,
A.every((invitee) => EmailCodec.is(invitee.key))
memberInvites,
A.every((member) => EmailCodec.is(member.key))
)
),
O.map(
A.map((invitee) =>
createTeamInvitation(invitee.key, invitee.value, props.editingTeamID)
A.map((member) =>
addUserToTeam(member.key, member.value, props.editingTeamID)
)
)
);
if (O.isNone(validationResult)) {
// Error handling for no validation
toast.error(`Invalid email address`);
sendingInvites.value = false;
toast.error('Invalid User!!');
addingUserToTeam.value = false;
return;
}
hideModal();
};
const getErrorMessage = (error: SendInvitesErrorType | any) => {
if (error.type === 'network_error') {
return t('error.network_error');
} else {
switch (error.error) {
case 'team/invalid_id':
return t('team.invalid_id');
case 'team/member_not_found':
return t('team.member_not_found');
case 'team_invite/already_member':
return t('team.already_member');
case 'team_invite/member_has_invite':
return t('team.member_has_invite');
}
}
};
const hideModal = () => {
sendingInvites.value = false;
sendInvitesResult.value = [];
newInvites.value = [
addingUserToTeam.value = false;
newMembersList.value = [
{
key: '',
value: TeamMemberRole.Viewer,
@@ -385,18 +286,29 @@ const hideModal = () => {
emit('hide-modal');
};
const teamInvitationMutation = useMutation(CreateTeamInvitationDocument);
const createTeamInvitation = async (
inviteeEmail: Email,
inviteeRole: TeamMemberRole,
const addUserToTeamMutation = useMutation(AddUserToTeamByAdminDocument);
const addUserToTeam = async (
email: string,
userRole: TeamMemberRole,
teamID: string
) => {
const res = await teamInvitationMutation.executeMutation({
inviteeEmail,
inviteeRole,
teamID,
});
const variables = { userEmail: email, role: userRole, teamID: teamID };
const res = await addUserToTeamMutation
.executeMutation(variables)
.then((result) => {
if (result.error) {
console.log(result.error);
if (result.error.toString() == '[GraphQL] user/not_found') {
toast.error('User not found in the infra!!');
} else {
toast.error('Failed to add user to the team!!');
}
} else {
toast.success('User is now a member of the team!!');
emit('member');
}
});
return res;
};
</script>

View File

@@ -1,229 +1,209 @@
<template>
<div class="flex flex-col">
<div class="relative flex">
<input
id="selectLabelTeamEdit"
v-model="name"
v-focus
class="input floating-input"
placeholder=" "
type="text"
autocomplete="off"
@keyup.enter="saveTeam"
/>
<label for="selectLabelTeamEdit">
{{ t('action.label') }}
</label>
</div>
<div class="flex items-center justify-between flex-1 pt-4">
<label for="memberList" class="p-4">
{{ t('team.members') }}
</label>
<div class="flex">
<HoppButtonSecondary
:icon="IconUserPlus"
:label="t('team.invite')"
filled
@click="
() => {
emit('invite-team');
}
"
/>
</div>
</div>
<div v-if="teamDetails.loading" class="border rounded border-divider">
<div class="flex items-center justify-center p-4">
<HoppSmartSpinner />
</div>
</div>
<div class="border rounded border-divider">
<div
v-if="teamMembers === 0"
class="flex flex-col items-center justify-center p-4 text-secondaryLight"
>
<img
:src="`/images/states/dark/add_group.svg`"
loading="lazy"
class="inline-flex flex-col object-contain object-center w-16 h-16 my-4"
:alt="`${t('empty.members')}`"
/>
<span class="pb-4 text-center">
{{ t('empty.members') }}
</span>
<HoppButtonSecondary
:icon="IconUserPlus"
:label="t('team.invite')"
@click="
() => {
emit('invite-team');
}
"
/>
</div>
<div v-else class="divide-y divide-dividerLight">
<div
v-for="(member, index) in membersList"
:key="`member-${index}`"
class="flex divide-x divide-dividerLight"
>
<input
class="flex flex-1 px-4 py-2 bg-transparent"
:placeholder="`${t('team.email')}`"
:name="'param' + index"
:value="member.email"
readonly
<div class="my-6">
<h3 class="text-2xl font-bold text-gray-200">Team Members</h3>
<div class="flex flex-col mb-6">
<div class="flex items-center justify-end flex-1 pt-4 mb-4">
<div class="flex">
<HoppButtonPrimary
:icon="IconUserPlus"
label="Add Members"
filled
@click="showInvite = !showInvite"
/>
<span>
<tippy
interactive
trigger="click"
theme="popover"
:on-shown="() => tippyActions![index].focus()"
>
<span class="select-wrapper">
<input
class="flex flex-1 px-4 py-2 bg-transparent cursor-pointer"
:placeholder="`${t('team.permissions')}`"
:name="'value' + index"
:value="member.role"
readonly
/>
</span>
<template #content="{ hide }">
<div
ref="tippyActions"
class="flex flex-col focus:outline-none"
tabindex="0"
@keyup.escape="hide()"
>
<HoppSmartItem
label="OWNER"
:icon="member.role === 'OWNER' ? IconCircleDot : IconCircle"
:active="member.role === 'OWNER'"
@click="
() => {
updateMemberRole(member.userID, TeamMemberRole.Owner);
hide();
}
"
/>
<HoppSmartItem
label="EDITOR"
:icon="
member.role === 'EDITOR' ? IconCircleDot : IconCircle
"
:active="member.role === 'EDITOR'"
@click="
() => {
updateMemberRole(member.userID, TeamMemberRole.Editor);
hide();
}
"
/>
<HoppSmartItem
label="VIEWER"
:icon="
member.role === 'VIEWER' ? IconCircleDot : IconCircle
"
:active="member.role === 'VIEWER'"
@click="
() => {
updateMemberRole(member.userID, TeamMemberRole.Viewer);
hide();
}
"
/>
</div>
</template>
</tippy>
</div>
</div>
<div class="border rounded border-divider">
<div
v-if="team?.teamMembers?.length === 0"
class="flex flex-col items-center justify-center p-4 text-secondaryLight"
>
<span class="pb-4 text-center">
No members in this team. Add members to this team to collaborate
</span>
<div class="flex">
<HoppButtonSecondary
id="member"
v-tippy="{ theme: 'tooltip' }"
:title="t('action.remove')"
:icon="IconUserMinus"
color="red"
:loading="isLoadingIndex === index"
@click="removeExistingTeamMember(member.userID, index)"
<HoppButtonSecondary
:icon="IconUserPlus"
label="Add Members"
@click="
() => {
showInvite = !showInvite;
}
"
/>
</div>
<div v-else class="divide-y divide-dividerLight">
<div
v-for="(member, index) in membersList"
:key="`member-${index}`"
class="flex divide-x divide-dividerLight"
>
<input
class="flex flex-1 px-4 py-2 bg-transparent"
placeholder="Email"
:name="'param' + index"
:value="member.email"
readonly
/>
<span>
<tippy
interactive
trigger="click"
theme="popover"
:on-shown="() => tippyActions![index].focus()"
>
<span class="select-wrapper">
<input
class="flex flex-1 px-4 py-2 bg-transparent cursor-pointer"
placeholder="Permissions"
:name="'value' + index"
:value="member.role"
readonly
/>
</span>
<template #content="{ hide }">
<div
ref="tippyActions"
class="flex flex-col focus:outline-none"
tabindex="0"
@keyup.escape="hide()"
>
<HoppSmartItem
label="OWNER"
:icon="
member.role === 'OWNER' ? IconCircleDot : IconCircle
"
:active="member.role === 'OWNER'"
@click="
() => {
updateMemberRole(member.userID, TeamMemberRole.Owner);
hide();
}
"
/>
<HoppSmartItem
label="EDITOR"
:icon="
member.role === 'EDITOR' ? IconCircleDot : IconCircle
"
:active="member.role === 'EDITOR'"
@click="
() => {
updateMemberRole(
member.userID,
TeamMemberRole.Editor
);
hide();
}
"
/>
<HoppSmartItem
label="VIEWER"
:icon="
member.role === 'VIEWER' ? IconCircleDot : IconCircle
"
:active="member.role === 'VIEWER'"
@click="
() => {
updateMemberRole(
member.userID,
TeamMemberRole.Viewer
);
hide();
}
"
/>
</div>
</template>
</tippy>
</span>
<div class="flex">
<HoppButtonSecondary
id="member"
v-tippy="{ theme: 'tooltip' }"
title="Remove"
:icon="IconUserMinus"
color="red"
:loading="isLoadingIndex === index"
@click="removeExistingTeamMember(member.userID, index)"
/>
</div>
</div>
</div>
</div>
<div v-if="!fetching && !team" class="flex flex-col items-center">
<component :is="IconHelpCircle" class="mb-4 svg-icons" />
Something went wrong. Please try again later.
</div>
</div>
<div
v-if="!teamDetails.loading && E.isLeft(teamDetails.data)"
class="flex flex-col items-center"
>
<component :is="IconHelpCircle" class="mb-4 svg-icons" />
{{ t('error.something_went_wrong') }}
</div>
<HoppButtonPrimary label="Save" outline @click="saveUpdatedTeam" />
</div>
<HoppButtonPrimary
:label="t('action.save')"
:loading="isLoading"
outline
@click="saveTeam"
<TeamsInvite
:show="showInvite"
:editingTeamID="route.params.id.toString()"
@member="updateMembers"
@hide-modal="
() => {
showInvite = false;
}
"
/>
</template>
<script setup lang="ts">
import { computed, ref, toRef, watch } from 'vue';
import * as E from 'fp-ts/Either';
import {
GetTeamDocument,
GetTeamQuery,
GetTeamQueryVariables,
TeamMemberAddedDocument,
TeamMemberRemovedDocument,
TeamMemberRole,
TeamMemberUpdatedDocument,
RemoveTeamMemberDocument,
} from '~/helpers/backend/graphql';
import {
removeTeamMember,
updateTeamMemberRole,
} from '~/helpers/backend/mutations/Team';
import { useToast } from '~/composables/toast';
import IconCircleDot from '~icons/lucide/circle-dot';
import IconCircle from '~icons/lucide/circle';
import IconUserPlus from '~icons/lucide/user-plus';
import IconUserMinus from '~icons/lucide/user-minus';
import IconHelpCircle from '~icons/lucide/help-circle';
const t = (key: string) => key;
const emit = defineEmits<{
(e: 'hide-modal'): void;
(e: 'refetch-teams'): void;
(e: 'invite-team'): void;
}>();
// Template refs
const tippyActions = ref<any | null>(null);
const props = defineProps<{
show: boolean;
editingTeam: {
name: string;
};
editingTeamID: string;
teamMembers: any;
}>();
import { useClientHandle, useMutation } from '@urql/vue';
import { computed, onMounted, ref, watch } from 'vue';
import { useRoute } from 'vue-router';
import { useToast } from '../../composables/toast';
import {
ChangeUserRoleInTeamByAdminDocument,
TeamInfoDocument,
TeamMemberRole,
RemoveUserFromTeamByAdminDocument,
TeamInfoQuery,
} from '../../helpers/backend/graphql';
import { HoppButtonPrimary, HoppButtonSecondary } from '@hoppscotch/ui';
const toast = useToast();
const name = toRef(props.editingTeam, 'name');
// Used to Invoke the Invite Members Modal
const showInvite = ref(false);
watch(
() => props.editingTeam.name,
(newName: string) => {
name.value = newName;
// Get Team Details
const team = ref<TeamInfoQuery['admin']['teamInfo'] | undefined>();
const fetching = ref(true);
const route = useRoute();
const { client } = useClientHandle();
const getTeamInfo = async () => {
fetching.value = true;
const result = await client
.query(TeamInfoDocument, { teamID: route.params.id.toString() })
.toPromise();
if (result.error) {
return toast.error('Unable to Load Team Info..');
}
);
if (result.data?.admin.teamInfo) {
team.value = result.data.admin.teamInfo;
}
fetching.value = false;
};
onMounted(async () => {
await getTeamInfo();
});
// Update members tab after a change in the members list or member roles
const updateMembers = () => getTeamInfo();
// Template refs
const tippyActions = ref<any | null>(null);
const roleUpdates = ref<
{
@@ -233,24 +213,21 @@ const roleUpdates = ref<
>([]);
watch(
() => teamDetails,
() => {
if (teamDetails.loading) return;
() => team.value,
(teamDetails) => {
const members = teamDetails?.teamMembers ?? [];
const data = teamDetails.data;
if (E.isRight(data)) {
const members = data.right.team?.teamMembers ?? [];
// Remove deleted members
roleUpdates.value = roleUpdates.value.filter(
(update) =>
members.findIndex((y) => y.user.uid === update.userID) !== -1
);
}
// Remove deleted members
roleUpdates.value = roleUpdates.value.filter(
(update) =>
members.findIndex(
(y: { user: { uid: string } }) => y.user.uid === update.userID
) !== -1
);
}
);
// Update the role of the member selected in the UI
const updateMemberRole = (userID: string, role: TeamMemberRole) => {
const updateIndex = roleUpdates.value.findIndex(
(item) => item.userID === userID
@@ -267,8 +244,10 @@ const updateMemberRole = (userID: string, role: TeamMemberRole) => {
}
};
// Obtain the list of members in the team
const membersList = computed(() => {
const members = (props.teamMembers ?? []).map((member) => {
if (!team.value) return [];
const members = (team.value.teamMembers ?? []).map((member) => {
const updatedRole = roleUpdates.value.find(
(update) => update.userID === member.user.uid
);
@@ -283,47 +262,73 @@ const membersList = computed(() => {
return members;
});
// Change the role of the selected user in the team
const changeUserRoleInTeamMutation = useMutation(
ChangeUserRoleInTeamByAdminDocument
);
const changeUserRoleInTeam = (
userUID: string,
teamID: string,
newRole: TeamMemberRole
) => {
return changeUserRoleInTeamMutation.executeMutation({
userUID,
teamID,
newRole,
});
};
// Save the updates done to the team
const isLoading = ref(false);
const saveUpdatedTeam = async () => {
isLoading.value = true;
roleUpdates.value.forEach(async (update) => {
if (!team.value) return;
const updateMemberRoleResult = await changeUserRoleInTeam(
update.userID,
team.value.id,
update.role
);
if (updateMemberRoleResult.error) {
toast.error('Role updation has failed!!');
} else {
toast.success('Roles updated successfully!!');
}
isLoading.value = false;
});
};
// Remove user from the team mutation
const removeUserFromTeamMutation = useMutation(
RemoveUserFromTeamByAdminDocument
);
const removeUserFromTeam = (userUid: string, teamID: string) => {
return () =>
removeUserFromTeamMutation.executeMutation({
userUid,
teamID,
});
};
// Remove a team member from the team
const isLoadingIndex = ref<null | number>(null);
const removeExistingTeamMember = async (userID: string, index: number) => {
if (!team.value) return;
isLoadingIndex.value = index;
const removeTeamMemberResult = await removeTeamMember(
const removeTeamMemberResult = await removeUserFromTeam(
userID,
props.editingTeamID
team.value.id
)();
if (E.isLeft(removeTeamMemberResult)) {
toast.error(`${t('error.something_went_wrong')}`);
if (removeTeamMemberResult.error) {
toast.error(`Member couldn't be removed!!`);
} else {
toast.success(`${t('team.member_removed')}`);
emit('refetch-teams');
teamDetails.execute({ teamID: props.editingTeamID });
team.value.teamMembers = team.value.teamMembers?.filter(
(member: any) => member.user.uid !== userID
);
toast.success('Member removed successfully!!');
}
isLoadingIndex.value = null;
};
const isLoading = ref(false);
const saveTeam = async () => {
isLoading.value = true;
roleUpdates.value.forEach(async (update) => {
const updateMemberRoleResult = await updateTeamMemberRole(
update.userID,
props.editingTeamID,
update.role
)();
if (E.isLeft(updateMemberRoleResult)) {
toast.error(`${t('error.something_went_wrong')}`);
console.error(updateMemberRoleResult.left.error);
}
});
hideModal();
toast.success(`${t('team.saved')}`);
isLoading.value = false;
};
const hideModal = () => {
emit('hide-modal');
};
</script>

View File

@@ -1,15 +1,14 @@
<template>
<div class="flex items-center justify-between flex-1">
<label for="memberList" class="pb-4"> Pending Invites </label>
</div>
<h3 class="text-2xl font-bold text-gray-200 mb-5">Pending Invites</h3>
<div class="border rounded divide-y divide-dividerLight border-divider">
<div v-if="fetching" class="flex items-center justify-center p-4">
<HoppSmartSpinner />
</div>
<div v-else>
<div v-if="data" class="divide-y divide-dividerLight">
<div v-if="team" class="divide-y divide-dividerLight">
<div
v-for="(invitee, index) in data?.team?.teamInvitations"
v-for="(invitee, index) in pendingInvites"
:key="`invitee-${index}`"
class="flex divide-x divide-dividerLight"
>
@@ -41,7 +40,7 @@
</div>
</div>
<div
v-if="data && data?.team?.teamInvitations.length === 0"
v-if="team && pendingInvites?.length === 0"
class="flex flex-col items-center justify-center p-4 text-secondaryLight"
>
<span class="text-center"> No pending invites </span>
@@ -57,43 +56,72 @@
<script setup lang="ts">
import IconTrash from '~icons/lucide/trash';
import IconHelpCircle from '~icons/lucide/help-circle';
import { useMutation, useQuery } from '@urql/vue';
import { ref } from 'vue';
import { useMutation, useClientHandle } from '@urql/vue';
import { ref, onMounted } from 'vue';
import {
GetPendingInvitesDocument,
RevokeTeamInvitationDocument,
TeamInfoDocument,
TeamInfoQuery,
} from '~/helpers/backend/graphql';
import { useToast } from '~/composables/toast';
const props = defineProps({
editingTeamID: { type: String, default: null },
});
import { useRoute } from 'vue-router';
const toast = useToast();
const isLoadingIndex = ref<null | number>(null);
const { fetching, data, error } = useQuery({
query: GetPendingInvitesDocument,
variables: {
teamID: props.editingTeamID,
},
// Get details of the team
const fetching = ref(true);
const error = ref(false);
const { client } = useClientHandle();
const route = useRoute();
const team = ref<TeamInfoQuery['admin']['teamInfo'] | undefined>();
const pendingInvites = ref<
TeamInfoQuery['admin']['teamInfo']['teamInvitations'] | undefined
>();
const getTeamInfo = async () => {
fetching.value = true;
const result = await client
.query(TeamInfoDocument, { teamID: route.params.id.toString() })
.toPromise();
if (result.error) {
error.value = true;
return toast.error('Unable to load team details..');
}
if (result.data?.admin.teamInfo) {
team.value = result.data.admin.teamInfo;
pendingInvites.value = team.value.teamInvitations;
}
fetching.value = false;
};
onMounted(async () => {
await getTeamInfo();
});
console.log(data);
// Remove Invitation
const isLoadingIndex = ref<null | number>(null);
const revokeInvitationMutation = useMutation(RevokeTeamInvitationDocument);
const revokeTeamInvitation = (inviteID: string) =>
revokeInvitationMutation.executeMutation({ inviteID });
const removeInvitee = async (id: string, index: number) => {
isLoadingIndex.value = index;
const result = await revokeTeamInvitation(id);
if (result.error) {
toast.error(`Member could not be removed`);
toast.error('Removal of invitee failed!!');
} else {
toast.success(`Member removed successfully`);
if (pendingInvites.value) {
pendingInvites.value = pendingInvites.value.filter(
(invite: { id: string }) => {
return invite.id !== id;
}
);
toast.success('Removal of invitee is successfull!!');
}
}
isLoadingIndex.value = null;
};
const revokeInvitationMutation = useMutation(RevokeTeamInvitationDocument);
function revokeTeamInvitation(inviteID: string) {
return revokeInvitationMutation.executeMutation({ inviteID });
}
</script>