UsersPage.vue
3.77 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
<script setup lang="ts">
import { ref, watchEffect } from 'vue'
import UsersTable from './widgets/UsersTable.vue'
import EditUserForm from './widgets/EditUserForm.vue'
import { User } from './types'
import { useUsers } from './composables/useUsers'
import { useModal, useToast } from 'vuestic-ui'
import { useProjects } from '../projects/composables/useProjects'
const doShowEditUserModal = ref(false)
const { users, isLoading, filters, sorting, pagination, error, ...usersApi } = useUsers()
const { projects } = useProjects()
const userToEdit = ref<User | null>(null)
const showEditUserModal = (user: User) => {
userToEdit.value = user
doShowEditUserModal.value = true
}
const showAddUserModal = () => {
userToEdit.value = null
doShowEditUserModal.value = true
}
const { init: notify } = useToast()
watchEffect(() => {
if (error.value) {
notify({
message: error.value.message,
color: 'danger',
})
}
})
const onUserSaved = async (user: User) => {
if (user.avatar.startsWith('blob:')) {
const blob = await fetch(user.avatar).then((r) => r.blob())
const { publicUrl } = await usersApi.uploadAvatar(blob)
user.avatar = publicUrl
}
if (userToEdit.value) {
await usersApi.update(user)
if (!error.value) {
notify({
message: `${user.fullname} has been updated`,
color: 'success',
})
}
} else {
await usersApi.add(user)
if (!error.value) {
notify({
message: `${user.fullname} has been created`,
color: 'success',
})
}
}
}
const onUserDelete = async (user: User) => {
await usersApi.remove(user)
notify({
message: `${user.fullname} has been deleted`,
color: 'success',
})
}
const editFormRef = ref()
const { confirm } = useModal()
const beforeEditFormModalClose = async (hide: () => unknown) => {
if (editFormRef.value.isFormHasUnsavedChanges) {
const agreed = await confirm({
maxWidth: '380px',
message: 'Form has unsaved changes. Are you sure you want to close it?',
size: 'small',
})
if (agreed) {
hide()
}
} else {
hide()
}
}
</script>
<template>
<h1 class="page-title">Users</h1>
<VaCard>
<VaCardContent>
<div class="flex flex-col md:flex-row gap-2 mb-2 justify-between">
<div class="flex flex-col md:flex-row gap-2 justify-start">
<VaButtonToggle
v-model="filters.isActive"
color="background-element"
border-color="background-element"
:options="[
{ label: 'Active', value: true },
{ label: 'Inactive', value: false },
]"
/>
<VaInput v-model="filters.search" placeholder="Search">
<template #prependInner>
<VaIcon name="search" color="secondary" size="small" />
</template>
</VaInput>
</div>
<VaButton @click="showAddUserModal">Add User</VaButton>
</div>
<UsersTable
v-model:sort-by="sorting.sortBy"
v-model:sorting-order="sorting.sortingOrder"
:users="users"
:projects="projects"
:loading="isLoading"
:pagination="pagination"
@editUser="showEditUserModal"
@deleteUser="onUserDelete"
/>
</VaCardContent>
</VaCard>
<VaModal
v-slot="{ cancel, ok }"
v-model="doShowEditUserModal"
size="small"
mobile-fullscreen
close-button
hide-default-actions
:before-cancel="beforeEditFormModalClose"
>
<h1 class="va-h5">{{ userToEdit ? 'Edit user' : 'Add user' }}</h1>
<EditUserForm
ref="editFormRef"
:user="userToEdit"
:save-button-label="userToEdit ? 'Save' : 'Add'"
@close="cancel"
@save="
(user) => {
onUserSaved(user)
ok()
}
"
/>
</VaModal>
</template>