Refactoring code module 01_metadata => 01_personal

This commit is contained in:
DESKTOP-1R2VSQH\Lenovo ThinkPad E490 2024-09-03 15:24:00 +07:00
parent d41a2516c3
commit 6c72c90524
42 changed files with 629 additions and 7824 deletions

View file

@ -1,29 +1,26 @@
<script setup lang="ts">
import { ref, onMounted } from "vue";
import type { QTableProps } from "quasar";
import { useCounterMixin } from "@/stores/mixin";
import { useRouter, useRoute } from "vue-router";
import { usePersonalDataStore } from "@/modules/01_metadata/stores/personalStore";
import { useQuasar } from "quasar";
import DialogForm from "@/modules/01_metadata/components/personal/DialogForm.vue";
import { useRouter, useRoute } from "vue-router";
import http from "@/plugins/http";
import config from "@/app.config";
import { useCounterMixin } from "@/stores/mixin";
import type { QTableProps } from "quasar";
import type { FormDistrict } from "@/modules/01_metadata/interface/response/personal/personal";
import DialogForm from "@/modules/01_metadata/components/personal/DialogForm.vue"; // ,
const $q = useQuasar();
const router = useRouter();
const route = useRoute();
const id = ref<string>(route.params.id as string);
const store = usePersonalDataStore();
const mixin = useCounterMixin();
const {
dialogRemove,
messageError,
showLoader,
hideLoader,
success,
date2Thai,
} = mixin;
const { messageError, showLoader, hideLoader, success, date2Thai } =
useCounterMixin();
/**
* Table
*/
const columns = [
{
name: "name",
@ -70,16 +67,6 @@ const columns = [
a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
},
] as const satisfies QTableProps["columns"];
const $q = useQuasar();
const editId = ref<string>("");
const filterKeyword = ref<string>("");
const dialog = ref<boolean>(false);
const district = ref<string>("");
const rows = ref<FormDistrict[]>([]);
const dialogStatus = ref<string>("");
const personalName = ref<string>("เขต/อำเภอ");
const visibleColumns = ref<string[]>([
"name",
"createdAt",
@ -87,16 +74,33 @@ const visibleColumns = ref<string[]>([
"lastUpdateFullName",
]);
const id = ref<string>(route.params.id as string); // id
const filterKeyword = ref<string>(""); //
const district = ref<string>(""); // /
const editId = ref<string>(""); // id /
const dialog = ref<boolean>(false); // ,
const dialogStatus = ref<string>(""); // ,
const personalName = ref<string>("เขต/อำเภอ"); //label input
const rows = ref<FormDistrict[]>([]); // /
/**
* fetch อมลรายการ ระดบการเขต/อำเภอ ตาม id ของจงหว
* - ดการกบขอม
* - กำหนด rows.value เทากบขอมลท respones
*/
async function fetchData() {
showLoader();
await http
.get(config.API.orgProvince + `/${id.value}`)
.then(async (res) => {
const data = res.data.result.districts;
const list = data.map((e: any) => ({
const data = await res.data.result.districts;
const list = data.map((e: FormDistrict) => ({
...e,
createdAt: e.createdAt ? date2Thai(e.createdAt) : "",
lastUpdatedAt: e.lastUpdatedAt ? date2Thai(e.lastUpdatedAt) : "",
createdAt: e.createdAt ? date2Thai(new Date(e.createdAt)) : "",
lastUpdatedAt: e.lastUpdatedAt
? date2Thai(new Date(e.lastUpdatedAt))
: "",
}));
rows.value = list;
})
@ -108,14 +112,19 @@ async function fetchData() {
});
}
/**
* เพมขอมลรายการระดบการเขต/อำเภอ
* แล fetch อมลรายการ ระดบการเขต/อำเภอ ใหม
*/
async function addData() {
showLoader();
await http
.post(config.API.orgDistrict, {
name: district.value,
provinceId: id.value,
})
.then(() => {
fetchData();
.then(async () => {
await fetchData();
success($q, "บันทึกข้อมูลสำเร็จ");
})
.catch((err) => {
@ -126,13 +135,19 @@ async function addData() {
});
}
/**
* แกไขขอมลรายการระดบการเขต/อำเภอ
* แล fetch อมลรายการ ระดบการเขต/อำเภอ ใหม
* @param id รายการระดบการเขต/อำเภอ
*/
async function editData(id: string) {
showLoader();
await http
.put(config.API.orgDistrict + `/${id}`, {
name: district.value,
})
.then(() => {
fetchData();
.then(async () => {
await fetchData();
success($q, "บันทึกข้อมูลสำเร็จ");
})
.catch((err) => {
@ -143,27 +158,20 @@ async function editData(id: string) {
});
}
async function deleteData(id: string) {
await http
.delete(config.API.orgDistrict + `/${id}`)
.then(() => {
fetchData();
success($q, "ลบข้อมูลสำเร็จ");
})
.catch((err) => {
messageError($q, err);
})
.finally(() => {
hideLoader();
});
}
/**
* ไปยงหน รายากรเแขวง/ตำบลโดย id ของเขต/อำเภอ
* @param idSub เขต/อำเภอ
*/
function nextPage(idSub: string) {
router.push(`/master-data/personal/sub-district/${id.value}/${idSub}`);
}
/**
* hook ทำงานเม Components กเรยกใชงาน
* แลวเรยก function fetch อมลรายการระดบการเขต/อำเภอ
*/
onMounted(async () => {
fetchData();
await fetchData();
});
</script>
@ -268,20 +276,6 @@ onMounted(async () => {
>
<q-tooltip>แกไขขอม</q-tooltip>
</q-btn>
<q-btn
color="red"
flat
dense
round
icon="mdi-delete"
clickable
@click.stop="
dialogRemove($q, async () => await deleteData(props.row.id))
"
v-close-popup
>
<q-tooltip>ลบขอม</q-tooltip>
</q-btn>
</q-td>
<q-td
v-for="col in props.cols"
@ -301,10 +295,10 @@ onMounted(async () => {
<DialogForm
v-model:dialog="dialog"
v-model:data="district"
v-model:personalName="personalName"
v-model:dialogStatus="dialogStatus"
v-model:editId="editId"
:addData="addData"
v-model:personal-name="personalName"
v-model:dialog-status="dialogStatus"
v-model:edit-id="editId"
:add-data="addData"
:fetch-data="fetchData"
:edit-data="editData"
/>