Refactoring code module 04_system

This commit is contained in:
DESKTOP-1R2VSQH\Lenovo ThinkPad E490 2024-09-09 16:21:28 +07:00
parent 2e9bbe3dd1
commit 6154ebf41d
5 changed files with 95 additions and 115 deletions

View file

@ -1,12 +1,11 @@
<script setup lang="ts"> <script setup lang="ts">
import { ref, onMounted, computed } from "vue"; import { ref } from "vue";
import { useQuasar } from "quasar"; import { useQuasar } from "quasar";
/** /**
* importType * importType
*/ */
import type { QTableProps } from "quasar"; import type { QTableProps } from "quasar";
import type { DataBackup } from "@/modules/04_system/interface/response/Main";
/** /**
* importStore * importStore
@ -19,25 +18,12 @@ import { storeToRefs } from "pinia";
* use * use
*/ */
const $q = useQuasar(); const $q = useQuasar();
const { showLoader, hideLoader, date2Thai, dialogRemove, dialogConfirm } = const { showLoader, date2Thai, dialogRemove, dialogConfirm } =
useCounterMixin(); useCounterMixin();
const { const { createBackUp, restore, deleteBackUp } = useDataStore();
fetchListBackup,
createBackUp,
restore,
deleteBackUp,
backupRunningList,
restoreRunningList,
getSize,
} = useDataStore();
const storeData = useDataStore(); const storeData = useDataStore();
const { dataBackUp, backupRunTotal, restoreRunTotal } = storeToRefs(storeData); const { dataBackUp, backupRunTotal, restoreRunTotal } = storeToRefs(storeData);
/**
* props
*/
const tab = defineModel<string>("tab", { required: true });
/** /**
* Table * Table
*/ */
@ -49,7 +35,7 @@ const visibleColumns = ref<string[]>([
"storageSize", "storageSize",
"status", "status",
]); ]);
const baseColumns = ref<QTableProps["columns"]>([ const columns = ref<QTableProps["columns"]>([
{ {
name: "name", name: "name",
align: "left", align: "left",
@ -64,32 +50,12 @@ const baseColumns = ref<QTableProps["columns"]>([
align: "center", align: "center",
label: "วันที่สร้าง", label: "วันที่สร้าง",
sortable: true, sortable: true,
// field: (v) => date2Thai(v, false, true),
field: "timestamp", field: "timestamp",
format: (v) => date2Thai(v, false, true), format: (v) => date2Thai(v, false, true),
headerStyle: "font-size: 14px", headerStyle: "font-size: 14px",
style: "font-size: 14px", style: "font-size: 14px",
}, },
// {
// name: "databaseSize",
// align: "center",
// label: " ",
// sortable: true,
// // field: (v) => date2Thai(v, false, true),
// field: "databaseSize",
// headerStyle: "font-size: 14px",
// style: "font-size: 14px",
// },
// {
// name: "storageSize",
// align: "center",
// label: " ",
// sortable: true,
// // field: (v) => date2Thai(v, false, true),
// field: "storageSize",
// headerStyle: "font-size: 14px",
// style: "font-size: 14px",
// },
{ {
name: "status", name: "status",
align: "center", align: "center",
@ -100,9 +66,6 @@ const baseColumns = ref<QTableProps["columns"]>([
style: "font-size: 14px", style: "font-size: 14px",
}, },
]); ]);
const columns = computed(() => {
return baseColumns.value;
});
/** /**
* function สรางรายการขอมลสำรอง * function สรางรายการขอมลสำรอง

View file

@ -35,32 +35,14 @@ const {
dialogMessageNotify, dialogMessageNotify,
} = useCounterMixin(); } = useCounterMixin();
const { const {
fetchListBackup,
createBackUp,
restore,
deleteBackUp,
backupRunningList,
restoreRunningList,
getSize,
getSchedule, getSchedule,
createSchedule, createSchedule,
editSchedule, editSchedule,
deleteSchedule, deleteSchedule,
toggleSchedule, toggleSchedule,
} = useDataStore(); } = useDataStore();
const storeData = useDataStore(); const storeData = useDataStore();
const { dataBackUp, restoreRunTotal, dataSchedule } = storeToRefs(storeData); const { restoreRunTotal, dataSchedule } = storeToRefs(storeData);
/**
* props
*/
/**
* ref
*/
const idEditSchedule = ref<string>(""); const idEditSchedule = ref<string>("");
const openDialog = ref<boolean>(false); const openDialog = ref<boolean>(false);
@ -94,7 +76,7 @@ const visibleColumns = ref<string[]>([
"startAt", "startAt",
"status", "status",
]); ]);
const baseColumns = ref<QTableProps["columns"]>([ const columns = ref<QTableProps["columns"]>([
{ {
name: "name", name: "name",
align: "left", align: "left",
@ -145,14 +127,6 @@ const baseColumns = ref<QTableProps["columns"]>([
style: "font-size: 14px", style: "font-size: 14px",
}, },
]); ]);
const columns = computed(() => {
return baseColumns.value;
});
/**
* function assignDataformDataschedule() iรบคาเข formDataschedule
*
*/
function assignDataformDataschedule(data: Schedule) { function assignDataformDataschedule(data: Schedule) {
typeTime.value = data.time !== "" ? "set" : "start"; typeTime.value = data.time !== "" ? "set" : "start";
@ -251,25 +225,8 @@ function onDelete(id: string) {
}); });
} }
/**
* function นคาขอมลสำรอง
* @param id อมลสำรอง
*/
function onRestore(id: string) {
dialogConfirm(
$q,
async () => {
await restore(id);
},
"ยืนยันการคืนค่าข้อมูลสำรอง",
"ต้องการยืนยันการคืนค่าข้อมูลสำรองนี้ใช่หรือไม่?"
);
}
const typeOnSubmit = ref<"edit" | "create">("create"); const typeOnSubmit = ref<"edit" | "create">("create");
const dateStart = ref<string>("");
const typeTime = ref<string>("set"); const typeTime = ref<string>("set");
const timeStartEvery = ref<number>();
const tab = ref<string>("daily"); const tab = ref<string>("daily");
const tabItems = ref<ItemsTeb[]>([ const tabItems = ref<ItemsTeb[]>([
{ name: "daily", label: "ทุกวัน", icon: "" }, { name: "daily", label: "ทุกวัน", icon: "" },
@ -312,10 +269,6 @@ function clearForm() {
formDataschedule.value.schedule = ""; formDataschedule.value.schedule = "";
} }
onMounted(async () => {
getSchedule();
});
watch(tab, () => { watch(tab, () => {
if (tab.value === formDataschedule.value.type) { if (tab.value === formDataschedule.value.type) {
formDataschedule.value = { ...prevFormDataschedule.value }; formDataschedule.value = { ...prevFormDataschedule.value };
@ -327,6 +280,10 @@ watch(tab, () => {
typeTime.value = "set"; typeTime.value = "set";
} }
}); });
onMounted(async () => {
getSchedule();
});
</script> </script>
<template> <template>

View file

@ -1,16 +1,16 @@
<script setup lang="ts"> <script setup lang="ts">
import { ref, watch } from "vue"; import { watch } from "vue";
import { useCounterMixin } from "@/stores/mixin"; import { useCounterMixin } from "@/stores/mixin";
const { date2Thai } = useCounterMixin(); const { date2Thai } = useCounterMixin();
const name = defineModel<string>("name"); const name = defineModel<string>("name"); //
const typeTime = defineModel<string>("typeTime", { default: "set" }); const typeTime = defineModel<string>("typeTime", { default: "set" });
const time = defineModel<string>("time"); const time = defineModel<string>("time"); //
const timeStartEvery = defineModel<string>("timeStartEvery"); const timeStartEvery = defineModel<string>("timeStartEvery");
const startAt = defineModel<any>("startAt", { default: new Date() }); const startAt = defineModel<any>("startAt", { default: new Date() }); // -
const selectDate = defineModel<string[]>("date", { default: [] }); const selectDate = defineModel<string[]>("date", { default: [] }); //
const tab = defineModel<string>("tab"); const tab = defineModel<string>("tab"); //
const timeStartEveryOptions: { value: string }[] = Array.from( const timeStartEveryOptions: { value: string }[] = Array.from(
{ length: 24 }, { length: 24 },

View file

@ -1,7 +1,9 @@
import { defineStore } from "pinia"; import { defineStore } from "pinia";
import { useQuasar } from "quasar";
import moment from "moment";
import http from "@/plugins/http"; import http from "@/plugins/http";
import config from "@/app.config"; import config from "@/app.config";
import moment from "moment";
import { useCounterMixin } from "@/stores/mixin"; import { useCounterMixin } from "@/stores/mixin";
@ -15,14 +17,8 @@ import type { ScheduleCreate } from "@/modules/04_system/interface/request/Main"
import { ref } from "vue"; import { ref } from "vue";
const mixin = useCounterMixin(); const mixin = useCounterMixin();
const { const $q = useQuasar();
dialogRemove, const { messageError, showLoader, hideLoader } = mixin;
messageError,
showLoader,
hideLoader,
success,
dialogConfirm,
} = mixin;
export const useDataStore = defineStore("systemStore", () => { export const useDataStore = defineStore("systemStore", () => {
const dataBackUp = ref<DataBackup[]>([]); const dataBackUp = ref<DataBackup[]>([]);
@ -35,11 +31,12 @@ export const useDataStore = defineStore("systemStore", () => {
const recordRestore = ref<Record<string, DataBackup>>({}); const recordRestore = ref<Record<string, DataBackup>>({});
/**
*
*/
async function fetchListBackup() { async function fetchListBackup() {
showLoader(); showLoader();
const res = await http.get(config.API.backup); const res = await http.get(config.API.backup);
hideLoader(); hideLoader();
if (!res) return false; if (!res) return false;
@ -57,18 +54,28 @@ export const useDataStore = defineStore("systemStore", () => {
} }
} }
/**
*
*/
async function createBackUp() { async function createBackUp() {
showLoader(); showLoader();
await http await http
.post(config.API.backup + "/create") .post(config.API.backup + "/create")
.then(async (res) => { .then(async () => {
await backupRunningList(fetchListBackup); await backupRunningList(fetchListBackup);
}) })
.catch((err) => {
messageError($q, err);
})
.finally(() => { .finally(() => {
hideLoader(); hideLoader();
}); });
} }
/**
*
* @param filename
*/
async function deleteBackUp(filename: string) { async function deleteBackUp(filename: string) {
showLoader(); showLoader();
await http await http
@ -83,11 +90,18 @@ export const useDataStore = defineStore("systemStore", () => {
}); });
} }
}) })
.catch((err) => {
messageError($q, err);
})
.finally(() => { .finally(() => {
hideLoader(); hideLoader();
}); });
} }
/**
*
* @param filename
*/
async function restore(filename: string) { async function restore(filename: string) {
await http await http
.post<string>(config.API.restore, { .post<string>(config.API.restore, {
@ -105,9 +119,16 @@ export const useDataStore = defineStore("systemStore", () => {
} }
restoreRunningList(fetchListBackup); restoreRunningList(fetchListBackup);
})
.catch((err) => {
messageError($q, err);
}); });
} }
/**
* BackUp auto
* @param cb
*/
async function backupRunningList(cb: () => void) { async function backupRunningList(cb: () => void) {
await http await http
.get<BackUpRunning[]>(config.API.backup + "/backup-running-list") .get<BackUpRunning[]>(config.API.backup + "/backup-running-list")
@ -144,9 +165,16 @@ export const useDataStore = defineStore("systemStore", () => {
prevBackupRunTotal.value = backupRunTotal.value; prevBackupRunTotal.value = backupRunTotal.value;
} }
})
.catch((err) => {
messageError($q, err);
}); });
} }
/**
* auto
* @param cb
*/
async function restoreRunningList(cb: () => void) { async function restoreRunningList(cb: () => void) {
await http await http
.get<BackUpRunning[]>(config.API.backup + "/restore-running-list") .get<BackUpRunning[]>(config.API.backup + "/restore-running-list")
@ -171,6 +199,9 @@ export const useDataStore = defineStore("systemStore", () => {
prevRestoreRunTotal.value = restoreRunTotal.value; prevRestoreRunTotal.value = restoreRunTotal.value;
} }
})
.catch((err) => {
messageError($q, err);
}); });
} }
@ -243,6 +274,9 @@ export const useDataStore = defineStore("systemStore", () => {
return ""; return "";
} }
/**
*
*/
async function getSchedule() { async function getSchedule() {
showLoader(); showLoader();
await http await http
@ -265,11 +299,18 @@ export const useDataStore = defineStore("systemStore", () => {
}; };
}); });
}) })
.catch((err) => {
messageError($q, err);
})
.finally(() => { .finally(() => {
hideLoader(); hideLoader();
}); });
} }
/**
*
* @param data
*/
async function createSchedule(data: ScheduleCreate) { async function createSchedule(data: ScheduleCreate) {
showLoader(); showLoader();
await http await http
@ -281,11 +322,19 @@ export const useDataStore = defineStore("systemStore", () => {
.then(async (res) => { .then(async (res) => {
return res.data; return res.data;
}) })
.catch((err) => {
messageError($q, err);
})
.finally(() => { .finally(() => {
hideLoader(); hideLoader();
}); });
} }
/**
*
* @param id id
* @param data
*/
async function editSchedule(id: string, data: ScheduleCreate) { async function editSchedule(id: string, data: ScheduleCreate) {
showLoader(); showLoader();
await http await http
@ -298,6 +347,9 @@ export const useDataStore = defineStore("systemStore", () => {
.then(async (res) => { .then(async (res) => {
return res.data; return res.data;
}) })
.catch((err) => {
messageError($q, err);
})
.finally(() => { .finally(() => {
hideLoader(); hideLoader();
}); });
@ -310,18 +362,27 @@ export const useDataStore = defineStore("systemStore", () => {
.then(async (res) => { .then(async (res) => {
return res.data; return res.data;
}) })
.catch((err) => {
messageError($q, err);
})
.finally(() => { .finally(() => {
hideLoader(); hideLoader();
}); });
} }
/**
*
* @param id id */
async function deleteSchedule(id: string) { async function deleteSchedule(id: string) {
showLoader(); showLoader();
await http await http
.delete(config.API.backup + "/schedule/" + id) .delete(config.API.backup + "/schedule/" + id)
.then(async (res) => { .then(async () => {
await fetchListBackup(); await fetchListBackup();
}) })
.catch((err) => {
messageError($q, err);
})
.finally(() => { .finally(() => {
hideLoader(); hideLoader();
}); });

View file

@ -1,6 +1,8 @@
<script setup lang="ts"> <script setup lang="ts">
import { onMounted, ref } from "vue"; import { onMounted, ref } from "vue";
import { useDataStore } from "@/modules/04_system/stores/main";
import { storeToRefs } from "pinia";
/** /**
* import type * import type
*/ */
@ -9,11 +11,8 @@ import type { ItemsTeb } from "@/modules/04_system/interface/index/Main";
/** /**
* import components * import components
*/ */
import Card from "@/modules/04_system/components/cardBackupRestore.vue"; import Card from "@/modules/04_system/components/01_cardBackupRestore.vue"; //
import CardAutoBackup from "@/modules/04_system/components/cardAutoBackup.vue"; import CardAutoBackup from "@/modules/04_system/components/02_cardAutoBackup.vue"; //
import { useDataStore } from "@/modules/04_system/stores/main";
import { storeToRefs } from "pinia";
const { fetchListBackup, backupRunningList, restoreRunningList } = const { fetchListBackup, backupRunningList, restoreRunningList } =
useDataStore(); useDataStore();