Refactoring code module 02_users
This commit is contained in:
parent
7540311518
commit
c11ff006eb
12 changed files with 301 additions and 1363 deletions
|
|
@ -1,433 +0,0 @@
|
|||
<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";
|
||||
|
||||
/** 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/DialogAddUser.vue";
|
||||
// import DialogManagementRole from "@/modules/02_users/components/Users/DialogManagementRole.vue";
|
||||
|
||||
/** importStore*/
|
||||
import { useCounterMixin } from "@/stores/mixin";
|
||||
|
||||
/** use*/
|
||||
const $q = useQuasar();
|
||||
const router = useRouter();
|
||||
const {
|
||||
dialogRemove,
|
||||
messageError,
|
||||
showLoader,
|
||||
hideLoader,
|
||||
success,
|
||||
dialogConfirm,
|
||||
} = useCounterMixin();
|
||||
|
||||
/** Table*/
|
||||
const rows = ref<Users[]>([]);
|
||||
const total = ref<number>(0);
|
||||
const currentPage = ref<number>(1);
|
||||
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 keyword = ref<string>("");
|
||||
|
||||
/** addUser*/
|
||||
const modalDialogAdd = ref<boolean>(false);
|
||||
const isStatusEdit = ref<boolean>(false);
|
||||
const userId = ref<string>("");
|
||||
|
||||
/** List Mune*/
|
||||
const itemMenu = ref<ItemsMenu[]>([
|
||||
// {
|
||||
// label: "แก้ไข",
|
||||
// icon: "edit",
|
||||
// color: "edit",
|
||||
// type: "edit",
|
||||
// },
|
||||
{
|
||||
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",
|
||||
},
|
||||
]);
|
||||
|
||||
/** function fetch รายชื่อผู้ใช้งาน*/
|
||||
function fetchListUsers() {
|
||||
let max = pageSize.value;
|
||||
let first = (currentPage.value - 1) * pageSize.value;
|
||||
showLoader();
|
||||
http
|
||||
.get(
|
||||
config.API.managementUser +
|
||||
`?max=${max}&first=${first}&search=${keyword.value}`
|
||||
)
|
||||
.then((res) => {
|
||||
const data = 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);
|
||||
}
|
||||
}
|
||||
|
||||
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(() => {
|
||||
success($q, "ลบข้อมูลสำเร็จ");
|
||||
fetchListUsers();
|
||||
})
|
||||
.catch((err) => {
|
||||
messageError($q, err);
|
||||
})
|
||||
.finally(() => {
|
||||
hideLoader();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* function ระงับการใช้งาน
|
||||
* @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();
|
||||
await 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();
|
||||
}
|
||||
);
|
||||
|
||||
onMounted(() => {
|
||||
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>
|
||||
Loading…
Add table
Add a link
Reference in a new issue