mirror of
https://github.com/crater-invoice/crater.git
synced 2025-10-28 12:11:08 -04:00
v6 update
This commit is contained in:
293
resources/scripts/admin/views/users/Create.vue
Normal file
293
resources/scripts/admin/views/users/Create.vue
Normal file
@ -0,0 +1,293 @@
|
||||
<template>
|
||||
<BasePage>
|
||||
<BasePageHeader :title="pageTitle">
|
||||
<BaseBreadcrumb>
|
||||
<BaseBreadcrumbItem :title="$t('general.home')" to="dashboard" />
|
||||
<BaseBreadcrumbItem :title="$tc('users.user', 2)" to="/admin/users" />
|
||||
<BaseBreadcrumbItem :title="pageTitle" to="#" active />
|
||||
</BaseBreadcrumb>
|
||||
</BasePageHeader>
|
||||
|
||||
<form action="" autocomplete="off" @submit.prevent="submitUser">
|
||||
<div class="grid grid-cols-12">
|
||||
<BaseCard class="mt-6 col-span-12 md:col-span-8">
|
||||
<BaseInputGrid layout="one-column">
|
||||
<BaseInputGroup
|
||||
:content-loading="isFetchingInitialData"
|
||||
:label="$t('users.name')"
|
||||
:error="
|
||||
v$.userData.name.$error && v$.userData.name.$errors[0].$message
|
||||
"
|
||||
required
|
||||
>
|
||||
<BaseInput
|
||||
v-model.trim="userStore.userData.name"
|
||||
:content-loading="isFetchingInitialData"
|
||||
:invalid="v$.userData.name.$error"
|
||||
@input="v$.userData.name.$touch()"
|
||||
>
|
||||
</BaseInput>
|
||||
</BaseInputGroup>
|
||||
|
||||
<BaseInputGroup
|
||||
:content-loading="isFetchingInitialData"
|
||||
:label="$t('users.email')"
|
||||
:error="
|
||||
v$.userData.email.$error &&
|
||||
v$.userData.email.$errors[0].$message
|
||||
"
|
||||
required
|
||||
>
|
||||
<BaseInput
|
||||
v-model.trim="userStore.userData.email"
|
||||
type="email"
|
||||
:content-loading="isFetchingInitialData"
|
||||
:invalid="v$.userData.email.$error"
|
||||
@input="v$.userData.email.$touch()"
|
||||
>
|
||||
</BaseInput>
|
||||
</BaseInputGroup>
|
||||
|
||||
<BaseInputGroup
|
||||
:content-loading="isFetchingInitialData"
|
||||
:label="$t('users.companies')"
|
||||
:error="
|
||||
v$.userData.companies.$error &&
|
||||
v$.userData.companies.$errors[0].$message
|
||||
"
|
||||
required
|
||||
>
|
||||
<BaseMultiselect
|
||||
v-model="userStore.userData.companies"
|
||||
mode="tags"
|
||||
:object="true"
|
||||
autocomplete="new-password"
|
||||
label="name"
|
||||
:options="companies"
|
||||
value-prop="id"
|
||||
:invalid="v$.userData.companies.$error"
|
||||
:content-loading="isFetchingInitialData"
|
||||
searchable
|
||||
:can-deselect="false"
|
||||
class="w-full"
|
||||
/>
|
||||
</BaseInputGroup>
|
||||
|
||||
<ValidateEach
|
||||
v-for="(company, i) in userStore.userData.companies"
|
||||
:key="i"
|
||||
:state="company"
|
||||
:rules="companyArrayRules"
|
||||
>
|
||||
<template #default="{ v }">
|
||||
<div class="space-y-6">
|
||||
<BaseInputGroup
|
||||
:content-loading="isFetchingInitialData"
|
||||
:label="
|
||||
$t('users.select_company_role', { company: company.name })
|
||||
"
|
||||
:error="v.role.$error && v.role.$errors[0].$message"
|
||||
required
|
||||
>
|
||||
<BaseMultiselect
|
||||
v-model="userStore.userData.companies[i].role"
|
||||
value-prop="name"
|
||||
track-by="id"
|
||||
autocomplete="off"
|
||||
:content-loading="isFetchingInitialData"
|
||||
label="name"
|
||||
:options="userStore.userData.companies[i].roles"
|
||||
:can-deselect="false"
|
||||
:invalid="v.role.$invalid"
|
||||
@change="v.role.$touch()"
|
||||
/>
|
||||
</BaseInputGroup>
|
||||
</div>
|
||||
</template>
|
||||
</ValidateEach>
|
||||
|
||||
<BaseInputGroup
|
||||
:content-loading="isFetchingInitialData"
|
||||
:label="$tc('users.password')"
|
||||
:error="
|
||||
v$.userData.password.$error &&
|
||||
v$.userData.password.$errors[0].$message
|
||||
"
|
||||
:required="!isEdit"
|
||||
>
|
||||
<BaseInput
|
||||
v-model="userStore.userData.password"
|
||||
name="new-password"
|
||||
autocomplete="new-password"
|
||||
:content-loading="isFetchingInitialData"
|
||||
type="password"
|
||||
:invalid="v$.userData.password.$error"
|
||||
@input="v$.userData.password.$touch()"
|
||||
>
|
||||
</BaseInput>
|
||||
</BaseInputGroup>
|
||||
|
||||
<BaseInputGroup
|
||||
:content-loading="isFetchingInitialData"
|
||||
:label="$t('users.phone')"
|
||||
>
|
||||
<BaseInput
|
||||
v-model.trim="userStore.userData.phone"
|
||||
:content-loading="isFetchingInitialData"
|
||||
></BaseInput>
|
||||
</BaseInputGroup>
|
||||
</BaseInputGrid>
|
||||
|
||||
<BaseButton
|
||||
:content-loading="isFetchingInitialData"
|
||||
type="submit"
|
||||
:loading="isSaving"
|
||||
:disabled="isSaving"
|
||||
class="mt-6"
|
||||
>
|
||||
<template #left="slotProps">
|
||||
<BaseIcon
|
||||
v-if="!isSaving"
|
||||
name="SaveIcon"
|
||||
:class="slotProps.class"
|
||||
/>
|
||||
</template>
|
||||
{{ isEdit ? $t('users.update_user') : $t('users.save_user') }}
|
||||
</BaseButton>
|
||||
</BaseCard>
|
||||
</div>
|
||||
</form>
|
||||
</BasePage>
|
||||
</template>
|
||||
|
||||
<script setup>
|
||||
import { ref, computed, reactive } from 'vue'
|
||||
import { useRouter, useRoute } from 'vue-router'
|
||||
import { useCompanyStore } from '@/scripts/admin/stores/company'
|
||||
import {
|
||||
required,
|
||||
minLength,
|
||||
email,
|
||||
requiredIf,
|
||||
helpers,
|
||||
} from '@vuelidate/validators'
|
||||
import useVuelidate from '@vuelidate/core'
|
||||
import { ValidateEach } from '@vuelidate/components'
|
||||
import { useI18n } from 'vue-i18n'
|
||||
import { useUsersStore } from '@/scripts/admin/stores/users'
|
||||
|
||||
const userStore = useUsersStore()
|
||||
|
||||
const { t } = useI18n()
|
||||
const route = useRoute()
|
||||
const router = useRouter()
|
||||
const companyStore = useCompanyStore()
|
||||
|
||||
let isSaving = ref(false)
|
||||
let isFetchingInitialData = ref(false)
|
||||
let selectedCompanies = ref([])
|
||||
let companies = ref([])
|
||||
|
||||
const isEdit = computed(() => route.name === 'users.edit')
|
||||
|
||||
const pageTitle = computed(() =>
|
||||
isEdit.value ? t('users.edit_user') : t('users.new_user')
|
||||
)
|
||||
|
||||
const rules = computed(() => {
|
||||
return {
|
||||
userData: {
|
||||
name: {
|
||||
required: helpers.withMessage(t('validation.required'), required),
|
||||
minLength: helpers.withMessage(
|
||||
t('validation.name_min_length', { count: 3 }),
|
||||
minLength(3)
|
||||
),
|
||||
},
|
||||
email: {
|
||||
required: helpers.withMessage(t('validation.required'), required),
|
||||
email: helpers.withMessage(t('validation.email_incorrect'), email),
|
||||
},
|
||||
password: {
|
||||
required: requiredIf(function () {
|
||||
helpers.withMessage(t('validation.required'), required)
|
||||
return !isEdit.value
|
||||
}),
|
||||
minLength: helpers.withMessage(
|
||||
t('validation.password_min_length', { count: 8 }),
|
||||
minLength(8)
|
||||
),
|
||||
},
|
||||
companies: {
|
||||
required: helpers.withMessage(t('validation.required'), required),
|
||||
},
|
||||
},
|
||||
}
|
||||
})
|
||||
|
||||
const companyArrayRules = {
|
||||
role: {
|
||||
required: helpers.withMessage(t('validation.required'), required),
|
||||
},
|
||||
}
|
||||
|
||||
const v$ = useVuelidate(rules, userStore, {
|
||||
$scope: true,
|
||||
})
|
||||
|
||||
loadInitialData()
|
||||
|
||||
userStore.resetUserData()
|
||||
|
||||
async function loadInitialData() {
|
||||
isFetchingInitialData.value = true
|
||||
try {
|
||||
if (isEdit.value) {
|
||||
await userStore.fetchUser(route.params.id)
|
||||
}
|
||||
|
||||
let res = await companyStore.fetchUserCompanies()
|
||||
|
||||
if (res?.data?.data) {
|
||||
companies.value = res.data.data.map((r) => {
|
||||
r.role = null
|
||||
|
||||
return r
|
||||
})
|
||||
}
|
||||
} catch {
|
||||
isFetchingInitialData.value = false
|
||||
}
|
||||
|
||||
isFetchingInitialData.value = false
|
||||
}
|
||||
|
||||
async function submitUser() {
|
||||
v$.value.$touch()
|
||||
|
||||
if (v$.value.$invalid) {
|
||||
return true
|
||||
}
|
||||
|
||||
try {
|
||||
isSaving.value = true
|
||||
let data = {
|
||||
...userStore.userData,
|
||||
companies: userStore.userData.companies.map((c) => {
|
||||
return {
|
||||
role: c.role,
|
||||
id: c.id,
|
||||
}
|
||||
}),
|
||||
}
|
||||
|
||||
const action = isEdit.value ? userStore.updateUser : userStore.addUser
|
||||
await action(data)
|
||||
|
||||
router.push('/admin/users')
|
||||
isSaving.value = false
|
||||
} catch (error) {
|
||||
isSaving.value = false
|
||||
}
|
||||
}
|
||||
</script>
|
||||
394
resources/scripts/admin/views/users/Index.vue
Normal file
394
resources/scripts/admin/views/users/Index.vue
Normal file
@ -0,0 +1,394 @@
|
||||
<template>
|
||||
<BasePage>
|
||||
<!-- Page Header Section -->
|
||||
<BasePageHeader :title="$t('users.title')">
|
||||
<BaseBreadcrumb>
|
||||
<BaseBreadcrumbItem :title="$t('general.home')" to="dashboard" />
|
||||
<BaseBreadcrumbItem :title="$tc('users.title', 2)" to="#" active />
|
||||
</BaseBreadcrumb>
|
||||
|
||||
<template #actions>
|
||||
<div class="flex items-center justify-end space-x-5">
|
||||
<BaseButton
|
||||
v-show="usersStore.totalUsers"
|
||||
variant="primary-outline"
|
||||
@click="toggleFilter"
|
||||
>
|
||||
{{ $t('general.filter') }}
|
||||
<template #right="slotProps">
|
||||
<BaseIcon
|
||||
v-if="!showFilters"
|
||||
name="FilterIcon"
|
||||
:class="slotProps.class"
|
||||
/>
|
||||
<BaseIcon v-else name="XIcon" :class="slotProps.class" />
|
||||
</template>
|
||||
</BaseButton>
|
||||
|
||||
<BaseButton
|
||||
v-if="userStore.currentUser.is_owner"
|
||||
@click="$router.push('users/create')"
|
||||
>
|
||||
<template #left="slotProps">
|
||||
<BaseIcon
|
||||
name="PlusIcon"
|
||||
:class="slotProps.class"
|
||||
aria-hidden="true"
|
||||
/>
|
||||
</template>
|
||||
{{ $t('users.add_user') }}
|
||||
</BaseButton>
|
||||
</div>
|
||||
</template>
|
||||
</BasePageHeader>
|
||||
|
||||
<BaseFilterWrapper :show="showFilters" class="mt-3" @clear="clearFilter">
|
||||
<BaseInputGroup :label="$tc('users.name')" class="flex-1 mt-2 mr-4">
|
||||
<BaseInput
|
||||
v-model="filters.name"
|
||||
type="text"
|
||||
name="name"
|
||||
autocomplete="off"
|
||||
/>
|
||||
</BaseInputGroup>
|
||||
|
||||
<BaseInputGroup :label="$tc('users.email')" class="flex-1 mt-2 mr-4">
|
||||
<BaseInput
|
||||
v-model="filters.email"
|
||||
type="text"
|
||||
name="email"
|
||||
autocomplete="off"
|
||||
/>
|
||||
</BaseInputGroup>
|
||||
|
||||
<BaseInputGroup class="flex-1 mt-2" :label="$tc('users.phone')">
|
||||
<BaseInput
|
||||
v-model="filters.phone"
|
||||
type="text"
|
||||
name="phone"
|
||||
autocomplete="off"
|
||||
/>
|
||||
</BaseInputGroup>
|
||||
</BaseFilterWrapper>
|
||||
|
||||
<!-- User Empty Placeholder -->
|
||||
|
||||
<BaseEmptyPlaceholder
|
||||
v-show="showEmptyScreen"
|
||||
:title="$t('users.no_users')"
|
||||
:description="$t('users.list_of_users')"
|
||||
>
|
||||
<AstronautIcon class="mt-5 mb-4" />
|
||||
|
||||
<template #actions>
|
||||
<BaseButton
|
||||
v-if="userStore.currentUser.is_owner"
|
||||
variant="primary-outline"
|
||||
@click="$router.push('/admin/users/create')"
|
||||
>
|
||||
<template #left="slotProps">
|
||||
<BaseIcon name="PlusIcon" :class="slotProps.class" />
|
||||
</template>
|
||||
{{ $t('users.add_user') }}
|
||||
</BaseButton>
|
||||
</template>
|
||||
</BaseEmptyPlaceholder>
|
||||
|
||||
<div v-show="!showEmptyScreen" class="relative table-container">
|
||||
<div
|
||||
class="
|
||||
relative
|
||||
flex
|
||||
items-center
|
||||
justify-end
|
||||
h-5
|
||||
border-gray-200 border-solid
|
||||
"
|
||||
>
|
||||
<BaseDropdown v-if="usersStore.selectedUsers.length">
|
||||
<template #activator>
|
||||
<span
|
||||
class="
|
||||
flex
|
||||
text-sm
|
||||
font-medium
|
||||
cursor-pointer
|
||||
select-none
|
||||
text-primary-400
|
||||
"
|
||||
>
|
||||
{{ $t('general.actions') }}
|
||||
<BaseIcon name="ChevronDownIcon" class="h-5" />
|
||||
</span>
|
||||
</template>
|
||||
<BaseDropdownItem @click="removeMultipleUsers">
|
||||
<BaseIcon name="TrashIcon" class="h-5 mr-3 text-gray-600" />
|
||||
{{ $t('general.delete') }}
|
||||
</BaseDropdownItem>
|
||||
</BaseDropdown>
|
||||
</div>
|
||||
|
||||
<BaseTable
|
||||
ref="table"
|
||||
:data="fetchData"
|
||||
:columns="userTableColumns"
|
||||
class="mt-3"
|
||||
>
|
||||
<!-- Select All Checkbox -->
|
||||
<template #header>
|
||||
<div class="absolute z-10 items-center left-6 top-2.5 select-none">
|
||||
<BaseCheckbox
|
||||
v-model="selectAllFieldStatus"
|
||||
variant="primary"
|
||||
@change="usersStore.selectAllUsers"
|
||||
/>
|
||||
</div>
|
||||
</template>
|
||||
<template #cell-status="{ row }">
|
||||
<div class="custom-control custom-checkbox">
|
||||
<BaseCheckbox
|
||||
:id="row.data.id"
|
||||
v-model="selectField"
|
||||
:value="row.data.id"
|
||||
variant="primary"
|
||||
/>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<template #cell-name="{ row }">
|
||||
<router-link
|
||||
:to="{ path: `users/${row.data.id}/edit` }"
|
||||
class="font-medium text-primary-500"
|
||||
>
|
||||
{{ row.data.name }}
|
||||
</router-link>
|
||||
</template>
|
||||
|
||||
<template #cell-phone="{ row }">
|
||||
<span>{{ row.data.phone ? row.data.phone : '-' }} </span>
|
||||
</template>
|
||||
|
||||
<template #cell-created_at="{ row }">
|
||||
<span>{{ row.data.formatted_created_at }}</span>
|
||||
</template>
|
||||
|
||||
<template v-if="userStore.currentUser.is_owner" #cell-actions="{ row }">
|
||||
<UserDropdown
|
||||
:row="row.data"
|
||||
:table="table"
|
||||
:load-data="refreshTable"
|
||||
/>
|
||||
</template>
|
||||
</BaseTable>
|
||||
</div>
|
||||
</BasePage>
|
||||
</template>
|
||||
|
||||
<script setup>
|
||||
import { computed, onUnmounted, ref, reactive, onMounted, watch } from 'vue'
|
||||
import { useI18n } from 'vue-i18n'
|
||||
import { useRouter } from 'vue-router'
|
||||
import { useUsersStore } from '@/scripts/admin/stores/users'
|
||||
import { useNotificationStore } from '@/scripts/stores/notification'
|
||||
import { useDialogStore } from '@/scripts/stores/dialog'
|
||||
import { useUserStore } from '@/scripts/admin/stores/user'
|
||||
import AstronautIcon from '@/scripts/components/icons/empty/AstronautIcon.vue'
|
||||
import UserDropdown from '@/scripts/admin/components/dropdowns/UserIndexDropdown.vue'
|
||||
import abilities from '@/scripts/admin/stub/abilities'
|
||||
|
||||
const notificationStore = useNotificationStore()
|
||||
const dialogStore = useDialogStore()
|
||||
const usersStore = useUsersStore()
|
||||
const userStore = useUserStore()
|
||||
|
||||
const router = useRouter()
|
||||
|
||||
let showFilters = ref(false)
|
||||
let isFetchingInitialData = ref(true)
|
||||
let id = ref(null)
|
||||
let sortedBy = ref('created_at')
|
||||
let isLoading = ref(false)
|
||||
const { t } = useI18n()
|
||||
let table = ref(null)
|
||||
|
||||
let filters = reactive({
|
||||
name: '',
|
||||
email: '',
|
||||
phone: '',
|
||||
})
|
||||
|
||||
const userTableColumns = computed(() => {
|
||||
return [
|
||||
{
|
||||
key: 'status',
|
||||
thClass: 'extra',
|
||||
tdClass: 'font-medium text-gray-900',
|
||||
sortable: false,
|
||||
},
|
||||
{
|
||||
key: 'name',
|
||||
label: t('users.name'),
|
||||
thClass: 'extra',
|
||||
tdClass: 'font-medium text-gray-900',
|
||||
},
|
||||
{ key: 'email', label: 'Email' },
|
||||
{
|
||||
key: 'phone',
|
||||
label: t('users.phone'),
|
||||
},
|
||||
{
|
||||
key: 'created_at',
|
||||
label: t('users.added_on'),
|
||||
},
|
||||
{
|
||||
key: 'actions',
|
||||
tdClass: 'text-right text-sm font-medium',
|
||||
sortable: false,
|
||||
},
|
||||
]
|
||||
})
|
||||
|
||||
const showEmptyScreen = computed(() => {
|
||||
return !usersStore.totalUsers && !isFetchingInitialData.value
|
||||
})
|
||||
|
||||
const selectField = computed({
|
||||
get: () => usersStore.selectedUsers,
|
||||
set: (value) => {
|
||||
return usersStore.selectUser(value)
|
||||
},
|
||||
})
|
||||
|
||||
const selectAllFieldStatus = computed({
|
||||
get: () => usersStore.selectAllField,
|
||||
set: (value) => {
|
||||
return usersStore.setSelectAllState(value)
|
||||
},
|
||||
})
|
||||
|
||||
watch(
|
||||
filters,
|
||||
() => {
|
||||
setFilters()
|
||||
},
|
||||
{ deep: true }
|
||||
)
|
||||
|
||||
onMounted(() => {
|
||||
usersStore.fetchUsers()
|
||||
usersStore.fetchRoles()
|
||||
})
|
||||
|
||||
onUnmounted(() => {
|
||||
if (usersStore.selectAllField) {
|
||||
usersStore.selectAllUsers()
|
||||
}
|
||||
})
|
||||
|
||||
function selectAllUser(params) {
|
||||
usersStore.selectAllUsers()
|
||||
}
|
||||
|
||||
function setFilters() {
|
||||
refreshTable()
|
||||
}
|
||||
|
||||
function refreshTable() {
|
||||
table.value && table.value.refresh()
|
||||
}
|
||||
|
||||
async function fetchData({ page, filter, sort }) {
|
||||
let data = {
|
||||
display_name: filters.name !== null ? filters.name : '',
|
||||
phone: filters.phone !== null ? filters.phone : '',
|
||||
email: filters.email !== null ? filters.email : '',
|
||||
orderByField: sort.fieldName || 'created_at',
|
||||
orderBy: sort.order || 'desc',
|
||||
page,
|
||||
}
|
||||
|
||||
isFetchingInitialData.value = true
|
||||
|
||||
let response = await usersStore.fetchUsers(data)
|
||||
|
||||
isFetchingInitialData.value = false
|
||||
|
||||
return {
|
||||
data: response.data.data,
|
||||
pagination: {
|
||||
totalPages: response.data.meta.last_page,
|
||||
currentPage: page,
|
||||
totalCount: response.data.meta.total,
|
||||
limit: 10,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
function clearFilter() {
|
||||
filters.name = ''
|
||||
filters.email = ''
|
||||
filters.phone = null
|
||||
}
|
||||
|
||||
function toggleFilter() {
|
||||
if (showFilters.value) {
|
||||
clearFilter()
|
||||
}
|
||||
|
||||
showFilters.value = !showFilters.value
|
||||
}
|
||||
|
||||
function removeUser(id) {
|
||||
dialogStore
|
||||
.openDialog({
|
||||
title: t('general.are_you_sure'),
|
||||
message: t('users.confirm_delete', 1),
|
||||
yesLabel: t('general.ok'),
|
||||
noLabel: t('general.cancel'),
|
||||
variant: 'danger',
|
||||
size: 'lg',
|
||||
hideNoButton: false,
|
||||
})
|
||||
.then((res) => {
|
||||
if (res) {
|
||||
let user = [id]
|
||||
usersStore.deleteUser(user).then((response) => {
|
||||
if (response.data.success) {
|
||||
table.value && table.value.refresh()
|
||||
return true
|
||||
}
|
||||
|
||||
if (response.data.error === 'user_attached') {
|
||||
notificationStore.showNotification({
|
||||
type: 'error',
|
||||
message: t('users.user_attached_message'),
|
||||
})
|
||||
return true
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
function removeMultipleUsers() {
|
||||
dialogStore
|
||||
.openDialog({
|
||||
title: t('general.are_you_sure'),
|
||||
message: t('users.confirm_delete', 2),
|
||||
yesLabel: t('general.ok'),
|
||||
noLabel: t('general.cancel'),
|
||||
variant: 'danger',
|
||||
size: 'lg',
|
||||
hideNoButton: false,
|
||||
})
|
||||
.then((res) => {
|
||||
if (res) {
|
||||
usersStore.deleteMultipleUsers().then((res) => {
|
||||
if (res.data.success) {
|
||||
table.value && table.value.refresh()
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
</script>
|
||||
Reference in New Issue
Block a user