UserList.vue
8.86 KB
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
<script setup>
const searchQuery = ref('')
const selectedRole = ref()
const selectedPlan = ref()
const selectedStatus = ref()
// Data table options
const itemsPerPage = ref(10)
const page = ref(1)
const sortBy = ref()
const orderBy = ref()
const selectedRows = ref([])
const updateOptions = options => {
sortBy.value = options.sortBy[0]?.key
orderBy.value = options.sortBy[0]?.order
}
// Headers
const headers = [
{
title: 'User',
key: 'user',
},
{
title: 'Email',
key: 'email',
},
{
title: 'Role',
key: 'role',
},
{
title: 'Plan',
key: 'plan',
},
{
title: 'Status',
key: 'status',
},
{
title: 'Actions',
key: 'actions',
sortable: false,
},
]
const {
data: usersData,
execute: fetchUsers,
} = await useApi(createUrl('/apps/users', {
query: {
q: searchQuery,
status: selectedStatus,
plan: selectedPlan,
role: selectedRole,
itemsPerPage,
page,
sortBy,
orderBy,
},
}))
const users = computed(() => usersData.value.users)
const totalUsers = computed(() => usersData.value.totalUsers)
// 👉 search filters
const roles = [
{
title: 'Admin',
value: 'admin',
},
{
title: 'Author',
value: 'author',
},
{
title: 'Editor',
value: 'editor',
},
{
title: 'Maintainer',
value: 'maintainer',
},
{
title: 'Subscriber',
value: 'subscriber',
},
]
const resolveUserRoleVariant = role => {
const roleLowerCase = role.toLowerCase()
if (roleLowerCase === 'subscriber')
return {
color: 'success',
icon: 'ri-user-line',
}
if (roleLowerCase === 'author')
return {
color: 'error',
icon: 'ri-computer-line',
}
if (roleLowerCase === 'maintainer')
return {
color: 'info',
icon: 'ri-pie-chart-line',
}
if (roleLowerCase === 'editor')
return {
color: 'warning',
icon: 'ri-edit-box-line',
}
if (roleLowerCase === 'admin')
return {
color: 'primary',
icon: 'ri-vip-crown-line',
}
return {
color: 'primary',
icon: 'ri-user-line',
}
}
const resolveUserStatusVariant = stat => {
const statLowerCase = stat.toLowerCase()
if (statLowerCase === 'pending')
return 'warning'
if (statLowerCase === 'active')
return 'success'
if (statLowerCase === 'inactive')
return 'secondary'
return 'primary'
}
const deleteUser = async id => {
await $api(`/apps/users/${ id }`, { method: 'DELETE' })
// Delete from selectedRows
const index = selectedRows.value.findIndex(row => row === id)
if (index !== -1)
selectedRows.value.splice(index, 1)
// refetch User
// TODO: Make this async
fetchUsers()
}
</script>
<template>
<section>
<VCard>
<VCardText class="d-flex flex-wrap gap-4">
<!-- 👉 Export button -->
<VBtn
variant="outlined"
color="secondary"
prepend-icon="ri-share-box-line"
>
Export
</VBtn>
<VSpacer />
<div class="d-flex flex-wrap gap-4">
<!-- 👉 Search -->
<div style="inline-size: 15.625rem;">
<VTextField
v-model="searchQuery"
placeholder="Search User"
density="compact"
/>
</div>
<!-- 👉 Add user button -->
<div style="inline-size: 10rem;">
<VSelect
v-model="selectedRole"
placeholder="Select Role"
:items="roles"
density="compact"
clearable
clear-icon="ri-close-line"
/>
</div>
</div>
</VCardText>
<!-- SECTION datatable -->
<VDataTableServer
v-model:items-per-page="itemsPerPage"
v-model:model-value="selectedRows"
:items-per-page-options="[
{ value: 10, title: '10' },
{ value: 20, title: '20' },
{ value: 50, title: '50' },
{ value: -1, title: '$vuetify.dataFooter.itemsPerPageAll' },
]"
:items="users"
item-value="id"
:items-length="totalUsers"
:headers="headers"
show-select
class="text-no-wrap rounded-0"
@update:options="updateOptions"
>
<!-- User -->
<template #item.user="{ item }">
<div class="d-flex">
<VAvatar
size="34"
:variant="!item.avatar ? 'tonal' : undefined"
:color="!item.avatar ? resolveUserRoleVariant(item.role).color : undefined"
class="me-3"
>
<VImg
v-if="item.avatar"
:src="item.avatar"
/>
<span v-else>{{ avatarText(item.fullName) }}</span>
</VAvatar>
<div class="d-flex flex-column">
<RouterLink
:to="{ name: 'apps-user-view-id', params: { id: item.id } }"
class="text-h6"
>
{{ item.fullName }}
</RouterLink>
<span class="text-sm">@{{ item.username }}</span>
</div>
</div>
</template>
<!-- Role -->
<template #item.role="{ item }">
<div class="d-flex gap-4">
<VIcon
size="22"
:icon="resolveUserRoleVariant(item.role).icon"
:color="resolveUserRoleVariant(item.role).color"
/>
<h6 class="text-h6 text-capitalize font-weight-regular">
{{ item.role }}
</h6>
</div>
</template>
<!-- Plan -->
<template #item.plan="{ item }">
<h6 class="text-h6 font-weight-regular text-capitalize">
{{ item.currentPlan }}
</h6>
</template>
<!-- Status -->
<template #item.status="{ item }">
<VChip
:color="resolveUserStatusVariant(item.status)"
size="small"
class="text-capitalize"
>
{{ item.status }}
</VChip>
</template>
<!-- Actions -->
<template #item.actions="{ item }">
<IconBtn
size="small"
@click="deleteUser(item.id)"
>
<VIcon icon="ri-delete-bin-7-line" />
</IconBtn>
<IconBtn
size="small"
:to="{ name: 'apps-user-view-id', params: { id: item.id } }"
>
<VIcon icon="ri-eye-line" />
</IconBtn>
<IconBtn size="small">
<VIcon icon="ri-more-2-line" />
<VMenu activator="parent">
<VList>
<VListItem link>
<template #prepend>
<VIcon
size="20"
icon="ri-edit-box-line"
/>
</template>
<VListItemTitle>Edit</VListItemTitle>
</VListItem>
<VListItem link>
<template #prepend>
<VIcon
size="20"
icon="ri-download-line"
/>
</template>
<VListItemTitle>Download</VListItemTitle>
</VListItem>
</VList>
</VMenu>
</IconBtn>
</template>
<!-- Pagination -->
<template #bottom>
<VDivider />
<div class="d-flex justify-end flex-wrap gap-x-6 px-2 py-1">
<div class="d-flex align-center gap-x-2 text-medium-emphasis text-base">
Rows Per Page:
<VSelect
v-model="itemsPerPage"
class="per-page-select"
variant="plain"
:items="[10, 20, 25, 50, 100]"
/>
</div>
<p class="d-flex align-center text-base text-high-emphasis me-2 mb-0">
{{ paginationMeta({ page, itemsPerPage }, totalUsers) }}
</p>
<div class="d-flex gap-x-2 align-center me-2">
<VBtn
class="flip-in-rtl"
icon="ri-arrow-left-s-line"
variant="text"
density="comfortable"
color="high-emphasis"
:disabled="page <= 1"
@click="page <= 1 ? page = 1 : page--"
/>
<VBtn
class="flip-in-rtl"
icon="ri-arrow-right-s-line"
density="comfortable"
variant="text"
color="high-emphasis"
:disabled="page >= Math.ceil(totalUsers / itemsPerPage)"
@click="page >= Math.ceil(totalUsers / itemsPerPage) ? page = Math.ceil(totalUsers / itemsPerPage) : page++ "
/>
</div>
</div>
</template>
</VDataTableServer>
<!-- SECTION -->
</VCard>
</section>
</template>
<style lang="scss">
.text-capitalize {
text-transform: capitalize;
}
</style>