Refactoring code module 02_users

This commit is contained in:
DESKTOP-1R2VSQH\Lenovo ThinkPad E490 2024-09-09 14:59:32 +07:00
parent 7540311518
commit c11ff006eb
12 changed files with 301 additions and 1363 deletions

View file

@ -0,0 +1,453 @@
<script setup lang="ts">
import { ref, onMounted, watch } from "vue";
import { useQuasar } from "quasar";
import { useRouter } from "vue-router";
import http from "@/plugins/http";
import config from "@/app.config";
import { useCounterMixin } from "@/stores/mixin";
/** importType*/
import type { QTableProps } from "quasar";
import type {
ItemsMenu,
Pagination,
} from "@/modules/02_users/interface/index/Main";
import type { Users, Roles } from "@/modules/02_users/interface/response/Main";
/** importComponents*/
import DialogAddUser from "@/modules/02_users/components/Users/DialogFormUser.vue";
/** use*/
const $q = useQuasar();
const router = useRouter();
const {
dialogRemove,
messageError,
showLoader,
hideLoader,
success,
dialogConfirm,
} = useCounterMixin();
/** Table*/
const keyword = ref<string>(""); //
const rows = ref<Users[]>([]); //
const currentPage = ref<number>(1); //
const total = ref<number>(0); //
const maxPage = ref<number>(0); //
const pageSize = ref<number>(10); //
const columns = ref<QTableProps["columns"]>([
{
name: "no",
align: "left",
label: "ลำดับ",
sortable: true,
field: (row) =>
(currentPage.value - 1) * pageSize.value + rows.value.indexOf(row) + 1,
headerStyle: "font-size: 14px",
style: "font-size: 14px",
},
{
name: "username",
align: "left",
label: "ชื่อผู้ใช้งาน",
sortable: true,
field: "username",
headerStyle: "font-size: 14px",
style: "font-size: 14px",
},
{
name: "email",
align: "left",
label: "Email",
sortable: true,
field: "email",
headerStyle: "font-size: 14px",
style: "font-size: 14px",
},
{
name: "firstname",
align: "left",
label: "ชื่อ",
sortable: true,
field: "firstname",
headerStyle: "font-size: 14px",
style: "font-size: 14px",
},
{
name: "lastname",
align: "left",
label: "นามสกุล",
sortable: true,
field: "lastname",
headerStyle: "font-size: 14px",
style: "font-size: 14px",
},
{
name: "role",
align: "left",
label: "สิทธิ์",
sortable: true,
headerStyle: "font-size: 14px",
style: "font-size: 14px",
field: (row) => {
const names = row.roles.map((role: Roles) => role.name);
return names.join(", ");
},
},
{
name: "enabled",
align: "left",
label: "สถานะการใช้งาน",
sortable: true,
field: "enabled",
headerStyle: "font-size: 14px",
style: "font-size: 14px",
},
]);
const visibleColumns = ref<string[]>([
"no",
"username",
"email",
"firstname",
"lastname",
"role",
"enabled",
]);
/** เพิ่มข้อมูลผู้ใช้งาน*/
const modalDialogAdd = ref<boolean>(false); // popup
const isStatusEdit = ref<boolean>(false); //
const userId = ref<string>(""); // id
//
const itemMenu = ref<ItemsMenu[]>([
{
label: "ระงับการใช้งาน",
icon: "mdi-lock-outline",
color: "orange-10",
type: "close",
},
{
label: "เปิดใช้งาน",
icon: "mdi-lock-open-outline",
color: "primary",
type: "open",
},
{
label: "จัดการสิทธิ์",
icon: "mdi-account-group",
color: "blue-9",
type: "managementRole",
},
{
label: "ลบ",
icon: "delete",
color: "red",
type: "delete",
},
]);
/**
* งกนดงขอมลรายชอผใชงาน
*/
async function fetchListUsers() {
let max = pageSize.value;
let first = (currentPage.value - 1) * pageSize.value;
showLoader();
await http
.get(
config.API.managementUser +
`?max=${max}&first=${first}&search=${keyword.value}`
)
.then(async (res) => {
const data = await res.data.data;
total.value = res.data.total;
maxPage.value = Math.ceil(total.value / pageSize.value);
rows.value = data.map((e: Users) => ({
...e,
roles: Array.isArray(e.roles)
? e.roles.filter(
(e: Roles) =>
e.name === "STAFF" ||
e.name === "SUPER_ADMIN" ||
e.name === "ADMIN" ||
e.name === "USER"
)
: [],
}));
})
.catch((err) => {
messageError($q, err);
})
.finally(() => {
hideLoader();
});
}
/**
* งกนจดการการกระทำตาง บขอมลผใชงาน
* @param type ประเภทตวเลอก
* @param data อมลรายการผใชงาน
*/
function onClickAction(type: string, data: Users) {
//
if (type === "edit") {
//
modalDialogAdd.value = true;
isStatusEdit.value = true;
userId.value = data.id;
} else if (type === "delete") {
//
onDeleteUser(data.id);
} else if (type === "managementRole") {
//
data && router.push(`/users/roles/${data.id}`);
} else if (type === "open" || type === "close") {
// /
const status = type === "open" ? true : false;
onLockUser(data.id, status);
}
}
/**
* งกนเป popup สำหรบเพมขอมลผใชงาน
*
* กำหนดสถานะการแกไขเป false และ id ใชงานทองการแกไขเปนคาวาง
*/
function openDialog() {
modalDialogAdd.value = true;
isStatusEdit.value = false;
userId.value = "";
}
/**
* function นยนการลบขอมลรายการผใชงาน
* @param id รายการผใชงาน
*
* ลบขอมลรายชอเสรจแลวทำการดงขอมลรายชอผใชงานใหม
*/
function onDeleteUser(id: string) {
dialogRemove($q, () => {
showLoader();
http
.delete(config.API.managementUser + `/${id}`)
.then(async () => {
await fetchListUsers();
success($q, "ลบข้อมูลสำเร็จ");
})
.catch((err) => {
messageError($q, err);
})
.finally(() => {
hideLoader();
});
});
}
/**
* งกนระงบการใชงานผใชงาน
* @param id ใชงาน
* @param type เปดใชงาน,ระงบการใชงาน
*
* เสรจแลวทำการดงขอมลรายชอผใชงานใหม
*/
function onLockUser(id: string, type: boolean) {
dialogConfirm(
$q,
() => {
showLoader();
http
.put(config.API.managementUser + `/${id}/enableStatus/${type}`)
.then(async () => {
await fetchListUsers();
success($q, type ? "เปิดใช้งานสำเร็จ" : "ระงับการใช้งานสำเร็จ");
})
.catch((err) => {
messageError($q, err);
hideLoader();
});
},
type ? "ยืนยันการเปิดใช้" : "ยืนยันการระงับใช้งาน",
type
? "ต้องการยืนยันการเปิดใช้นี้หรือไม่ ?"
: "ต้องการยืนยันระงับการใช้งานนี้หรือไม่ ?"
);
}
/**
* function ปเดท paging
* @param initialPagination อม pagination
*
* กำหนดหนาไปยงหนาแรก
*/
function updatePagination(initialPagination: Pagination) {
currentPage.value = 1;
pageSize.value = initialPagination.rowsPerPage;
}
/**
* การเปลยนแปลงของจำนวนขอมลตอหน
*
* งขอมลรายชอผใชงานตามจำนวนขอมลตอหน
*/
watch(
() => pageSize.value,
() => {
fetchListUsers();
}
);
/**
* hook ทำงานเมอมการเรยกใชงาน Components
*
* งขอมลรายชอผใชงาน
*/
onMounted(async () => {
await fetchListUsers();
});
</script>
<template>
<div class="row items-center">
<div class="toptitle text-dark row items-center q-py-xs">
ดการผใชงาน
</div>
</div>
<q-card flast bordered class="q-pa-md">
<div class="items-center col-12 row q-col-gutter-sm q-mb-sm">
<q-btn flat round dense color="primary" icon="add" @click="openDialog()">
<q-tooltip>เพมผใชงาน </q-tooltip>
</q-btn>
<q-space />
<q-input
borderless
dense
outlined
v-model="keyword"
placeholder="ค้นหา"
@keydown.enter.prevent="(currentPage = 1), fetchListUsers()"
>
<template v-slot:append>
<q-icon name="search" />
</template>
</q-input>
<q-select
v-model="visibleColumns"
multiple
outlined
dense
options-dense
:display-value="$q.lang.table.columns"
emit-value
map-options
:options="columns"
option-value="name"
options-cover
/>
</div>
<div class="col-12 q-pt-sm">
<d-table
ref="table"
:columns="columns"
:rows="rows"
row-key="id"
flat
bordered
:paging="true"
dense
:rows-per-page-options="[10, 25, 50, 100]"
:visible-columns="visibleColumns"
@update:pagination="updatePagination"
>
<template v-slot:header="props">
<q-tr :props="props">
<q-th v-for="col in props.cols" :key="col.name" :props="props">
<span class="text-weight-medium">{{ col.label }}</span>
</q-th>
<q-th auto-width></q-th>
</q-tr>
</template>
<template v-slot:body="props">
<q-tr :props="props" class="cursor-pointer">
<q-td v-for="col in props.cols" :key="col.name" :props="props">
<div v-if="col.name === 'enabled'">
<q-icon
:name="col.value ? 'check' : 'close'"
:color="col.value ? 'primary' : 'red'"
size="24px"
/>
</div>
<div v-else>
{{ col.value ? col.value : "-" }}
</div>
</q-td>
<q-td>
<q-btn
flat
dense
icon="mdi-dots-vertical"
class="q-pa-none q-ml-xs"
color="grey-13"
size="12px"
>
<q-menu>
<q-list dense style="min-width: 200px">
<q-item
v-for="(item, index) in props.row.enabled
? itemMenu.filter((e) => e.type !== 'open')
: itemMenu.filter((e) => e.type !== 'close')"
:key="index"
clickable
v-close-popup
@click.stop="onClickAction(item.type, props.row)"
>
<q-item-section>
<div class="row items-center">
<q-icon
:color="item.color"
size="17px"
:name="item.icon"
/>
<div class="q-pl-md">{{ item.label }}</div>
</div>
</q-item-section>
</q-item>
</q-list>
</q-menu>
</q-btn>
</q-td>
</q-tr>
</template>
<template v-slot:pagination="scope">
งหมด {{ total }} รายการ
<q-pagination
v-model="currentPage"
active-color="primary"
color="dark"
:max="Number(maxPage)"
size="sm"
boundary-links
direction-links
:max-pages="5"
@update:model-value="fetchListUsers"
></q-pagination>
</template>
</d-table>
</div>
</q-card>
<DialogAddUser
:fetchlist="fetchListUsers"
v-model:modal="modalDialogAdd"
v-model:isStatusEdit="isStatusEdit"
:userId="userId"
/>
</template>
<style scoped></style>