Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:User CRUD #35

Merged
merged 9 commits into from
Apr 10, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/components/MainNavigationDrawer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ import {
mdiNoteOutline,
mdiTag,
mdiOfficeBuilding,
mdiViewGrid
mdiViewGrid,
mdiAccountGroup
} from '@mdi/js'

export default {
Expand Down Expand Up @@ -110,6 +111,13 @@ export default {
icon: mdiLoginVariant,
title: 'Log in',
to: '/login'
},
{
icon: mdiAccountGroup,
org: false,
title: 'Users',
to: '/users',
auth: true
}
]
},
Expand Down
169 changes: 169 additions & 0 deletions src/components/UserDetail.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
<template>
<v-row>
<v-col>
<v-row>
<v-col>
<v-card>
<v-container fluid>
<v-row>
<v-col>
<ValidationProvider
v-slot="{ errors }"
name="email"
rules="required|email"
>
<v-text-field
v-model.trim="value.email"
:error-messages="errors"
:readonly="!editing"
label="Email"
required
></v-text-field>
</ValidationProvider>

<ValidationProvider
v-slot="{ errors }"
name="full_name"
rules="required|min:1|max:100"
>
<v-text-field
v-model.trim="value.full_name"
:error-messages="errors"
:readonly="!editing"
label="Full Name"
required
></v-text-field>
</ValidationProvider>

<ValidationProvider
v-if="create"
v-slot="{ errors }"
name="password"
rules="required|min:1|max:100"
>
<v-text-field
v-model.trim="value.password"
:error-messages="errors"
:readonly="!editing"
label="Password"
required
></v-text-field>
</ValidationProvider>

<ValidationProvider
v-slot="{ errors }"
name="name"
rules="required|min:1|max:100"
>
<v-text-field
v-model.trim="value.name"
:error-messages="errors"
:readonly="!editing"
label="Name"
required
></v-text-field>
</ValidationProvider>

<ValidationProvider
v-slot="{ errors }"
name="roles"
rules="required"
>
<v-select
v-model="value.roles"
:items="['user', 'manager']"
:error-messages="errors"
chips
deletable-chips
dense
label="Roles"
hide-details
small-chips
variant="solo"
:readonly="!editing"
></v-select>
</ValidationProvider>
</v-col>
</v-row>

<standard-audit v-if="!editing" :value="value" />
<standard-identifier v-if="!editing" :value="value" />

<v-card-actions v-if="!editing && $canRemove('user')">
<v-btn color="red" dark @click="isRemoveOpen = true"
><v-icon left>{{ mdiDelete }}</v-icon
>Remove user</v-btn
>
</v-card-actions>
</v-container>
</v-card>
</v-col>
</v-row>
</v-col>

<v-dialog v-model="isRemoveOpen" dark max-width="500" persistent>
<v-card>
<v-card-title class="headline"> Remove User </v-card-title>

<v-card-text>
Are you Sure to delete &#x22;{{ value.full_name }}&#x22;
</v-card-text>

<v-card-actions>
<v-btn color="primary" @click="isRemoveOpen = false">cancel</v-btn>
<v-btn @click="removeUser(true)">Continue</v-btn>
</v-card-actions>
</v-card>
</v-dialog>
</v-row>
</template>

<script>
import { mapActions } from 'vuex'
import { ValidationProvider } from 'vee-validate'
import StandardAudit from '@/components/StandardAudit'
import StandardIdentifier from '@/components/StandardIdentifier'

export default {
components: {
ValidationProvider,
StandardAudit,
StandardIdentifier
},

props: {
editing: { default: false, type: Boolean },
value: { type: Object, required: true },
create: { default: false, type: Boolean }
},

data() {
return {
isRemoveOpen: false
}
},

methods: {
...mapActions({ remove: 'users/remove' }),

async removeUser() {
try {
await this.remove([this.value._id, {}])
this.$router.push(
{
name: 'users'
},
() => {
this.$bus.$emit('edit-status', {
type: 'success',
message: 'User deleted.' // TODO: Localize
})
}
)
} catch (err) {
this.$bus.$emit('edit-status', { type: 'error', message: err.message })
}
}
}
}
</script>
199 changes: 199 additions & 0 deletions src/components/UsersSearch.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
<template>
<v-container fluid>
<v-row dense>
<v-col>
<v-text-field
v-model.trim="searchDebounce"
:append-icon="mdiMagnify"
filled
flat
label="Filter users"
></v-text-field>
</v-col>
</v-row>

<v-row dense>
<v-col>
<feathers-vuex-find
v-slot="{ isFindPending: loading, items: users, pagination }"
:fetch-query="usersFetchQuery"
:query="usersQuery"
:watch="['fetchQuery.$and', 'fetchQuery.$limit', 'fetchQuery.$skip']"
qid="search"
service="users"
>
<v-data-table
:footer-props="{ itemsPerPageOptions: [10, 50, 100] }"
:headers="headers"
:hide-default-header="$vuetify.breakpoint.xsOnly"
:items="users"
:loading="loading"
:options.sync="tableOptions"
:server-items-length="pagination ? pagination.total : 0"
disable-sort
item-key="_id"
>
<template #item.full_name="{ item }">
<nuxt-link
v-if="showLink && item.full_name"
:to="{
name: 'users-userId',
params: {
userId: item._id
}
}"
>{{ item.full_name }}</nuxt-link
><span v-else>{{ item.full_name || '--' }}</span>
</template>

<template #item.icons="{ item }">
<span v-if="$scopedSlots.actions" class="text-no-wrap">
<slot name="actions" :item="item" />
</span>
</template>
</v-data-table>
</feathers-vuex-find>
</v-col>
</v-row>
</v-container>
</template>

<script>
import _debounce from 'lodash/debounce'
import { escapeRegExp } from '@/lib/utils'

export default {
props: {
showLink: { default: false, type: Boolean }
},

data: () => ({
headers: [
{
align: 'center',
value: 'select',
width: '50px'
},
{
align: 'left',
text: 'Full name',
value: 'full_name'
},
{
align: 'left',
text: 'Email',
value: 'email'
},
{
align: 'left',
text: 'Roles',
value: 'roles'
},
{
align: 'right',
value: 'icons',
width: '5%'
}
],

queryUserIds: [],

search: null,
searchDebounce: null,

tableOptions: {
descending: false,
page: 1,
itemsPerPage: 100,
totalItems: null
}
}),

computed: {
usersFetchQuery() {
const { search, tableOptions } = this
const { page, itemsPerPage } = tableOptions

const query = {
$limit: itemsPerPage,
$skip: (page - 1) * itemsPerPage,
$sort: { email: 1, _id: 1 },
$select: ['_id', 'full_name', 'email', 'name', 'roles']
}

const ands = []

if (search && search.length) {
// TODO: Implement n-grams for partial full-text search! https://en.wikipedia.org/wiki/N-gram
ands.push({
$or: [
{
name: {
$regex: `${escapeRegExp(search)}`,
$options: 'i'
}
},
{
full_name: {
$regex: `${escapeRegExp(search)}`,
$options: 'i'
}
},
{
email: {
$regex: `${escapeRegExp(search)}`,
$options: 'i'
}
}
]
})
}

if (ands.length) query.$and = ands

return query
},

paginationSearch() {
return this.$store.state.users.pagination.search
},

usersQuery() {
return {
_id: { $in: this.queryUserIds },
$sort: { name: 1, _id: 1 }
}
}
},

watch: {
paginationSearch(newValue) {
this.queryUserIds = (newValue && newValue.ids) || []
},

searchDebounce(newValue) {
this.debouncedSearch(newValue)
}
},

created() {
this.debouncedSearch = _debounce(value => {
this.search = value
}, 400)
},

mounted() {
let { headers } = this

if (!this.$scopedSlots.select) headers = headers.slice(1)
if (!this.$scopedSlots.actions) headers = headers.slice(0, -1)

this.headers = headers
},

beforeDestroy() {
this.debouncedSearch.cancel()
this.debouncedSearch = null
}
}
</script>
Loading