UsersTable.vue
5.37 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
<script setup lang="ts">
import { defineVaDataTableColumns, useModal } from 'vuestic-ui'
import { User, UserRole } from '../types'
import UserAvatar from './UserAvatar.vue'
import { PropType, computed, toRef } from 'vue'
import { Pagination, Sorting } from '../../../data/pages/users'
import { useVModel } from '@vueuse/core'
import { Project } from '../../projects/types'
const columns = defineVaDataTableColumns([
{ label: 'Full Name', key: 'fullname', sortable: true },
{ label: 'Email', key: 'email', sortable: true },
{ label: 'Username', key: 'username', sortable: true },
{ label: 'Role', key: 'role', sortable: true },
{ label: 'Projects', key: 'projects', sortable: true },
{ label: ' ', key: 'actions', align: 'right' },
])
const props = defineProps({
users: {
type: Array as PropType<User[]>,
required: true,
},
projects: {
type: Array as PropType<Project[]>,
required: true,
},
loading: { type: Boolean, default: false },
pagination: { type: Object as PropType<Pagination>, required: true },
sortBy: { type: String as PropType<Sorting['sortBy']>, required: true },
sortingOrder: { type: String as PropType<Sorting['sortingOrder']>, default: null },
})
const emit = defineEmits<{
(event: 'edit-user', user: User): void
(event: 'delete-user', user: User): void
(event: 'update:sortBy', sortBy: Sorting['sortBy']): void
(event: 'update:sortingOrder', sortingOrder: Sorting['sortingOrder']): void
}>()
const users = toRef(props, 'users')
const sortByVModel = useVModel(props, 'sortBy', emit)
const sortingOrderVModel = useVModel(props, 'sortingOrder', emit)
const roleColors: Record<UserRole, string> = {
admin: 'danger',
user: 'background-element',
owner: 'warning',
}
const totalPages = computed(() => Math.ceil(props.pagination.total / props.pagination.perPage))
const { confirm } = useModal()
const onUserDelete = async (user: User) => {
const agreed = await confirm({
title: 'Delete user',
message: `Are you sure you want to delete ${user.fullname}?`,
okText: 'Delete',
cancelText: 'Cancel',
size: 'small',
maxWidth: '380px',
})
if (agreed) {
emit('delete-user', user)
}
}
const formatProjectNames = (projects: Project['id'][]) => {
const names = projects.reduce((acc, p) => {
const project = props.projects?.find(({ id }) => p === id)
if (project) {
acc.push(project.project_name)
}
return acc
}, [] as string[])
if (names.length === 0) return 'No projects'
if (names.length <= 2) {
return names.map((name) => name).join(', ')
}
return (
names
.slice(0, 2)
.map((name) => name)
.join(', ') +
' + ' +
(names.length - 2) +
' more'
)
}
</script>
<template>
<VaDataTable
v-model:sort-by="sortByVModel"
v-model:sorting-order="sortingOrderVModel"
:columns="columns"
:items="users"
:loading="$props.loading"
>
<template #cell(fullname)="{ rowData }">
<div class="flex items-center gap-2 max-w-[230px] ellipsis">
<UserAvatar :user="rowData as User" size="small" />
{{ rowData.fullname }}
</div>
</template>
<template #cell(username)="{ rowData }">
<div class="max-w-[120px] ellipsis">
{{ rowData.username }}
</div>
</template>
<template #cell(email)="{ rowData }">
<div class="ellipsis max-w-[230px]">
{{ rowData.email }}
</div>
</template>
<template #cell(role)="{ rowData }">
<VaBadge :text="rowData.role" :color="roleColors[rowData.role as UserRole]" />
</template>
<template #cell(projects)="{ rowData }">
<div class="ellipsis max-w-[300px] lg:max-w-[450px]">
{{ formatProjectNames(rowData.projects) }}
</div>
</template>
<template #cell(actions)="{ rowData }">
<div class="flex gap-2 justify-end">
<VaButton
preset="primary"
size="small"
icon="mso-edit"
aria-label="Edit user"
@click="$emit('edit-user', rowData as User)"
/>
<VaButton
preset="primary"
size="small"
icon="mso-delete"
color="danger"
aria-label="Delete user"
@click="onUserDelete(rowData as User)"
/>
</div>
</template>
</VaDataTable>
<div class="flex flex-col-reverse md:flex-row gap-2 justify-between items-center py-2">
<div>
<b>{{ $props.pagination.total }} results.</b>
Results per page
<VaSelect v-model="$props.pagination.perPage" class="!w-20" :options="[10, 50, 100]" />
</div>
<div v-if="totalPages > 1" class="flex">
<VaButton
preset="secondary"
icon="va-arrow-left"
aria-label="Previous page"
:disabled="$props.pagination.page === 1"
@click="$props.pagination.page--"
/>
<VaButton
class="mr-2"
preset="secondary"
icon="va-arrow-right"
aria-label="Next page"
:disabled="$props.pagination.page === totalPages"
@click="$props.pagination.page++"
/>
<VaPagination
v-model="$props.pagination.page"
buttons-preset="secondary"
:pages="totalPages"
:visible-pages="5"
:boundary-links="false"
:direction-links="false"
/>
</div>
</div>
</template>
<style lang="scss" scoped>
.va-data-table {
::v-deep(.va-data-table__table-tr) {
border-bottom: 1px solid var(--va-background-border);
}
}
</style>