-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
index.vue
517 lines (467 loc) · 15.8 KB
/
index.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
<template>
<div class="flex flex-col">
<!-- Table View for All Users -->
<div class="flex flex-col">
<h1 class="text-lg font-bold text-secondaryDark">
{{ t('users.users') }}
</h1>
<div class="flex items-center space-x-4 mt-10 mb-5">
<HoppButtonPrimary
:label="t('users.invite_user')"
@click="showInviteUserModal = true"
:icon="IconAddUser"
/>
<div class="flex">
<HoppButtonSecondary
outline
filled
:label="t('users.pending_invites')"
:to="'/users/invited'"
/>
</div>
</div>
<div class="overflow-x-auto">
<div v-if="error">{{ t('users.load_list_error') }}</div>
<UsersTable
v-else-if="usersList"
:headings="headings"
:list="usersList"
:checkbox="true"
:loading="showSpinner"
:selected-rows="selectedRows"
:pagination="{ totalPages }"
@pageNumber="handlePageChange"
@onRowClicked="goToUserDetails"
>
<template #extension>
<div class="flex w-full items-center bg-primary">
<icon-lucide-search class="mx-3 text-xs" />
<HoppSmartInput
v-model="searchQuery"
styles="w-full bg-primary py-1"
input-styles="h-full border-none"
placeholder="Search by name or email"
/>
</div>
</template>
<template #head>
<th class="px-6 py-2">{{ t('users.id') }}</th>
<th class="px-6 py-2">{{ t('users.name') }}</th>
<th class="px-6 py-2">{{ t('users.email') }}</th>
<th class="px-6 py-2">{{ t('users.date') }}</th>
<!-- Empty header for Action Button -->
<th class="px-6 py-2"></th>
</template>
<template #body="{ row: user }">
<td class="py-2 px-7 max-w-[8rem] truncate">
{{ user.uid }}
</td>
<td class="py-2 px-7">
<div class="flex items-center space-x-2">
<span>
{{ user.displayName ?? t('users.unnamed') }}
</span>
<span
v-if="user.isAdmin"
class="text-xs font-medium px-3 py-0.5 rounded-full bg-green-900 text-green-300"
>
{{ t('users.admin') }}
</span>
</div>
</td>
<td class="py-2 px-7">
{{ user.email }}
</td>
<td class="py-2 px-7">
{{ getCreatedDate(user.createdOn) }}
<div class="text-gray-400 text-tiny">
{{ getCreatedTime(user.createdOn) }}
</div>
</td>
<td @click.stop>
<div class="relative">
<tippy interactive trigger="click" theme="popover">
<HoppButtonSecondary
v-tippy="{ theme: 'tooltip' }"
:icon="IconMoreHorizontal"
/>
<template #content="{ hide }">
<div
ref="tippyActions"
class="flex flex-col focus:outline-none"
tabindex="0"
@keyup.escape="hide()"
>
<HoppSmartItem
:icon="user.isAdmin ? IconUserMinus : IconUserCheck"
:label="
user.isAdmin
? t('users.remove_admin_status')
: t('users.make_admin')
"
class="!hover:bg-emerald-600"
@click="
() => {
user.isAdmin
? confirmAdminToUser(user.uid)
: confirmUserToAdmin(user.uid);
hide();
}
"
/>
<HoppSmartItem
v-if="!user.isAdmin"
:icon="IconTrash"
:label="t('users.delete_user')"
class="!hover:bg-red-600"
@click="
() => {
confirmUserDeletion(user.uid);
hide();
}
"
/>
</div>
</template>
</tippy>
</div>
</td>
</template>
</UsersTable>
<!-- Actions for Selected Rows -->
<div
v-if="selectedRows.length"
class="fixed m-2 bottom-0 left-40 right-0 w-min mx-auto shadow-2xl"
>
<div
class="flex justify-center items-end bg-primaryLight border border-divider rounded-md mb-5"
>
<HoppButtonSecondary
:label="t('state.selected', { count: selectedRows.length })"
class="py-4 border-divider rounded-r-none bg-emerald-800 text-secondaryDark"
/>
<HoppButtonSecondary
:icon="IconUserCheck"
:label="t('users.make_admin')"
class="py-4 border-divider border-r-1 rounded-none hover:bg-emerald-600"
@click="confirmUsersToAdmin = true"
/>
<HoppButtonSecondary
:icon="IconUserMinus"
:label="t('users.remove_admin_status')"
class="py-4 border-divider border-r-1 rounded-none hover:bg-orange-500"
@click="confirmAdminsToUsers = true"
/>
<HoppButtonSecondary
:icon="IconTrash"
:label="t('users.delete_users')"
class="py-4 border-divider rounded-l-none hover:bg-red-500"
@click="confirmUsersDeletion = true"
/>
</div>
</div>
</div>
</div>
<UsersInviteModal
:show="showInviteUserModal"
@hide-modal="showInviteUserModal = false"
@send-invite="sendInvite"
/>
<HoppSmartConfirmModal
:show="confirmUsersToAdmin"
:title="
AreMultipleUsersSelected
? t('state.confirm_users_to_admin')
: t('state.confirm_user_to_admin')
"
@hide-modal="resetConfirmUserToAdmin"
@resolve="makeUsersToAdmin(usersToAdminUID)"
/>
<HoppSmartConfirmModal
:show="confirmAdminsToUsers"
:title="
AreMultipleUsersSelectedToAdmin
? t('state.confirm_admins_to_users')
: t('state.confirm_admin_to_user')
"
@hide-modal="resetConfirmAdminToUser"
@resolve="makeAdminsToUsers(adminsToUserUID)"
/>
<HoppSmartConfirmModal
:show="confirmUsersDeletion"
:title="
AreMultipleUsersSelectedForDeletion
? t('state.confirm_users_deletion')
: t('state.confirm_user_deletion')
"
@hide-modal="resetConfirmUserDeletion"
@resolve="deleteUsers(deleteUserUID)"
/>
</div>
</template>
<script setup lang="ts">
import { useMutation, useQuery } from '@urql/vue';
import { format } from 'date-fns';
import { computed, ref, watch } from 'vue';
import { useRouter } from 'vue-router';
import { useI18n } from '~/composables/i18n';
import { useToast } from '~/composables/toast';
import { usePagedQuery } from '~/composables/usePagedQuery';
import {
DemoteUsersByAdminDocument,
InviteNewUserDocument,
MakeUsersAdminDocument,
MetricsDocument,
RemoveUsersByAdminDocument,
UserInfoQuery,
UsersListQuery,
UsersListV2Document,
} from '~/helpers/backend/graphql';
import {
ADMIN_CANNOT_BE_DELETED,
DELETE_USER_FAILED_ONLY_ONE_ADMIN,
USER_ALREADY_INVITED,
} from '~/helpers/errors';
import IconMoreHorizontal from '~icons/lucide/more-horizontal';
import IconTrash from '~icons/lucide/trash';
import IconUserCheck from '~icons/lucide/user-check';
import IconUserMinus from '~icons/lucide/user-minus';
import IconAddUser from '~icons/lucide/user-plus';
// Get Proper Date Formats
const t = useI18n();
const toast = useToast();
const getCreatedDate = (date: string) => format(new Date(date), 'dd-MM-yyyy');
const getCreatedTime = (date: string) => format(new Date(date), 'hh:mm a');
// Table Headings
const headings = [
{ key: 'uid', label: t('users.id') },
{ key: 'displayName', label: t('users.name') },
{ key: 'email', label: t('users.email') },
{ key: 'createdOn', label: t('users.date') },
];
// Get Paginated Results of all the users in the infra
const usersPerPage = 2;
const {
fetching,
error,
refetch,
list: usersList,
} = usePagedQuery(
UsersListV2Document,
(x) => x.infra.allUsersV2,
usersPerPage,
{ searchString: '', take: usersPerPage, skip: 0 }
);
// Selected Rows
const selectedRows = ref<UsersListQuery['infra']['allUsers']>([]);
// Debounce Function
const debounce = (func: () => void, delay: number) => {
let debounceTimeout: ReturnType<typeof setTimeout> | null = null;
if (debounceTimeout) clearTimeout(debounceTimeout);
debounceTimeout = setTimeout(func, delay);
};
// Search
const searchQuery = ref('');
const handleSearch = async (input: string) => {
searchQuery.value = input;
await refetch({ searchString: input, take: usersPerPage, skip: 0 });
};
watch(searchQuery, () => {
debounce(() => {
handleSearch(searchQuery.value);
}, 500);
});
// Spinner
const showSpinner = ref(false);
watch(fetching, (fetching) => {
if (fetching) {
showSpinner.value = true;
debounce(() => {
showSpinner.value = false;
}, 500);
}
});
// Pagination
const { data } = useQuery({ query: MetricsDocument });
const usersCount = computed(() => data?.value?.infra.usersCount);
const totalPages = computed(() => {
if (!usersCount.value) return 0;
if (searchQuery.value.length > 0) {
return usersList.value.length;
}
return Math.ceil(usersCount.value / usersPerPage);
});
const handlePageChange = async (page: number) => {
if (page < 1 || page > totalPages.value) {
return;
} else {
await refetch({
searchString: searchQuery.value,
take: usersPerPage,
skip: (page - 1) * usersPerPage,
});
}
};
// Go to Individual User Details Page
const router = useRouter();
const goToUserDetails = (user: UserInfoQuery['infra']['userInfo']) =>
router.push('/users/' + user.uid);
// Send Invitation through Email
const showInviteUserModal = ref(false);
const sendInvitation = useMutation(InviteNewUserDocument);
const sendInvite = async (email: string) => {
if (!email.trim()) {
toast.error(t('state.invalid_email'));
return;
}
const variables = { inviteeEmail: email.trim() };
const result = await sendInvitation.executeMutation(variables);
if (result.error) {
if (result.error.message === USER_ALREADY_INVITED)
toast.error(t('state.user_already_invited'));
else toast.error(t('state.email_failure'));
} else {
toast.success(t('state.email_success'));
showInviteUserModal.value = false;
}
};
// Make Multiple Users Admin
const confirmUsersToAdmin = ref(false);
const usersToAdminUID = ref<string | null>(null);
const usersToAdmin = useMutation(MakeUsersAdminDocument);
const AreMultipleUsersSelected = computed(
() => usersToAdminUID.value === null && selectedRows.value.length > 0
);
const confirmUserToAdmin = (id: string | null) => {
confirmUsersToAdmin.value = true;
usersToAdminUID.value = id;
};
// Resets variables if user cancels the confirmation
const resetConfirmUserToAdmin = () => {
confirmUsersToAdmin.value = false;
usersToAdminUID.value = null;
};
const makeUsersToAdmin = async (id: string | null) => {
const userUIDs = id ? [id] : selectedRows.value.map((user) => user.uid);
const variables = { userUIDs };
const result = await usersToAdmin.executeMutation(variables);
if (result.error) {
toast.error(
id ? t('state.admin_failure') : t('state.users_to_admin_failure')
);
} else {
toast.success(
id ? t('state.admin_success') : t('state.users_to_admin_success')
);
usersList.value = usersList.value.map((user) => ({
...user,
isAdmin: userUIDs.includes(user.uid) ? true : user.isAdmin,
}));
selectedRows.value.splice(0, selectedRows.value.length);
}
confirmUsersToAdmin.value = false;
usersToAdminUID.value = null;
};
// Remove Admin Status from Multiple Users
const confirmAdminsToUsers = ref(false);
const adminsToUserUID = ref<string | null>(null);
const adminsToUser = useMutation(DemoteUsersByAdminDocument);
const confirmAdminToUser = (id: string | null) => {
confirmAdminsToUsers.value = true;
adminsToUserUID.value = id;
};
// Resets variables if user cancels the confirmation
const resetConfirmAdminToUser = () => {
confirmAdminsToUsers.value = false;
adminsToUserUID.value = null;
};
const AreMultipleUsersSelectedToAdmin = computed(
() => adminsToUserUID.value === null && selectedRows.value.length > 0
);
const makeAdminsToUsers = async (id: string | null) => {
const userUIDs = id ? [id] : selectedRows.value.map((user) => user.uid);
const variables = { userUIDs };
const result = await adminsToUser.executeMutation(variables);
if (result.error) {
toast.error(
id
? t('state.remove_admin_failure')
: t('state.remove_admin_from_users_failure')
);
} else {
toast.success(
id
? t('state.remove_admin_success')
: t('state.remove_admin_from_users_success')
);
usersList.value = usersList.value.map((user) => ({
...user,
isAdmin: userUIDs.includes(user.uid) ? false : user.isAdmin,
}));
selectedRows.value.splice(0, selectedRows.value.length);
}
confirmAdminsToUsers.value = false;
adminsToUserUID.value = null;
};
// Delete Multiple Users
const confirmUsersDeletion = ref(false);
const deleteUserUID = ref<string | null>(null);
const usersDeletion = useMutation(RemoveUsersByAdminDocument);
const confirmUserDeletion = (id: string | null) => {
confirmUsersDeletion.value = true;
deleteUserUID.value = id;
};
// Resets variables if user cancels the confirmation
const resetConfirmUserDeletion = () => {
confirmUsersDeletion.value = false;
deleteUserUID.value = null;
};
const AreMultipleUsersSelectedForDeletion = computed(
() => deleteUserUID.value === null && selectedRows.value.length > 0
);
const deleteUsers = async (id: string | null) => {
const userUIDs = id ? [id] : selectedRows.value.map((user) => user.uid);
const variables = { userUIDs };
const result = await usersDeletion.executeMutation(variables);
if (result.error) {
const errorMessage =
result.error.message === DELETE_USER_FAILED_ONLY_ONE_ADMIN
? t('state.delete_user_failed_only_one_admin')
: id
? t('state.delete_user_failure')
: t('state.delete_users_failure');
toast.error(errorMessage);
} else {
const deletedUsers = result.data?.removeUsersByAdmin || [];
const deletedIDs = deletedUsers
.filter((user) => user.isDeleted)
.map((user) => user.userUID);
const isAdminError = deletedUsers.some(
(user) => user.errorMessage === ADMIN_CANNOT_BE_DELETED
);
usersList.value = usersList.value.filter(
(user) => !deletedIDs.includes(user.uid)
);
if (isAdminError) {
toast.success(
t('state.delete_some_users_success', { count: deletedIDs.length })
);
toast.error(
t('state.delete_some_users_failure', {
count: deletedUsers.length - deletedIDs.length,
})
);
setTimeout(() => {
toast.error(t('state.remove_admin_for_deletion'));
}, 2000);
} else {
toast.success(
id ? t('state.delete_user_success') : t('state.delete_users_success')
);
}
selectedRows.value.splice(0, selectedRows.value.length);
}
confirmUsersDeletion.value = false;
deleteUserUID.value = null;
};
</script>