diff --git a/src/modules/06_webservices/components/DialogApi.vue b/src/modules/06_webservices/components/DialogApi.vue index bd411aa9..f281d7a1 100644 --- a/src/modules/06_webservices/components/DialogApi.vue +++ b/src/modules/06_webservices/components/DialogApi.vue @@ -6,6 +6,11 @@ import { useCounterMixin } from "@/stores/mixin"; import { useDataStoreManage } from "@/modules/06_webservices/stores/manage"; import type { DataOption } from "@/modules/06_webservices/interface/index/Main"; +import type { + DataSystems, + Propertys, + DataAttributes, +} from "@/modules/06_webservices/interface/response/Main"; /** importComponents*/ import DialogHeader from "@/components/DialogHeader.vue"; @@ -34,36 +39,56 @@ const props = defineProps({ }, }); +//ชื่อ title ของ dialog const titleName = computed(() => { return isEdit.value ? "แก้ไข API" : "เพิ่ม API"; }); +// รายการระบบที่ไม่ใช่ "all" const options = computed(() => { return systemList.value.filter((e) => e.id !== "all") ?? []; }); +// กรองรายการระบบที่เป็น Main const mainFields = computed(() => { return availableFields.value.filter((field) => field.isMain === true); }); -const systemOptions = ref(options.value); // Default to first option +// ฟังก์ชันสำหรับนับจำนวนแอตทริบิวต์ที่ถูกเลือกในแต่ละหัวข้อ +const getSelectedCount = computed(() => { + return (tbName: string) => { + if (!selectedAttributes.value[tbName]) return 0; -const apiPath = ref(""); + return Object.values(selectedAttributes.value[tbName]).filter( + (selected) => selected === true + ).length; + }; +}); + +const systemMain = ref(""); // Default system +const systemOptions = ref(options.value); // Default to first option +const apiPath = ref(""); // Path API +const splitterModel = ref(15); // Initial splitter position +const tabs = ref(""); // Tabs // Form data const formData = reactive({ name: "", methodApi: "GET", system: "", - isActive: true, + isActive: false, }); -// เพิ่ม state สำหรับเก็บ fields และ attributes ที่เลือก -const availableFields = ref([]); -const selectedAttributes = ref>>({}); -const loading = ref(false); -const validationErrors = ref([]); +const availableFields = ref([]); // เก็บข้อมูล fields ที่ดึงมา +const selectedAttributes = ref>>({}); // เก็บข้อมูล attributes ที่เลือก +const dataAttributes = ref([]); // เก็บข้อมูล attributes ที่ดึงมา +const loading = ref(false); // ใช้สำหรับแสดงสถานะการโหลดข้อมูล +const validationErrors = ref([]); // เก็บข้อความ error สำหรับ validation +/** + * ดึงข้อมูล fields ของ API ตามชื่อระบบ + * @param system ชื่อระบบที่ต้องการดึงข้อมูล fields + */ async function fetchData(system: string) { availableFields.value = []; loading.value = true; @@ -80,56 +105,64 @@ async function fetchData(system: string) { selectedAttributes.value[field.tb] = {}; if (field.propertys && field.propertys.length > 0) { - field.propertys.forEach((prop: any) => { + field.propertys.forEach((prop: Propertys) => { // เก็บ properties ในแต่ละ group selectedAttributes.value[field.tb][prop.propertyName] = false; }); } }); + + tabs.value = + availableFields.value.length > 0 ? availableFields.value[0].tb : ""; } catch (error) { - console.error("Error fetching fields:", error); + messageError($q, error); availableFields.value = []; } finally { loading.value = false; } } +/** + * ดึงข้อมูล API ตาม ID + * @param id ID ของ API ที่ต้องการดึงข้อมูล + */ async function fetchDataId(id: string) { + showLoader(); try { const res = await http.get(`${config.API.apiManage}${id}`); const data = res.data.result; - + systemMain.value = data.system; formData.name = data.name; formData.methodApi = data.methodApi; formData.system = data.system; formData.isActive = data.isActive; apiPath.value = data.pathApi; + dataAttributes.value = data.apiAttributes || []; // ดึง fields และ attributes ที่เกี่ยวข้อง await fetchData(data.system); - console.log(data.apiAttributes); - // กำหนดค่า selectedAttributes ตามข้อมูลที่ดึงมา - if (data.apiAttributes?.length > 0) { - data.apiAttributes.forEach( - ({ tbName, propertyKey }: { tbName: string; propertyKey: string }) => { - if (selectedAttributes.value[tbName]?.[propertyKey] !== undefined) { - selectedAttributes.value[tbName][propertyKey] = true; - } - } - ); - } + updateSelectedAttributes(data.apiAttributes); } catch (error) { - console.error("Error fetching API by ID:", error); + messageError($q, error); + } finally { + hideLoader(); } } + +/** + * ฟังก์ชันเมื่อเลือกระบบจาก select + * @param value ชื่อระบบที่ถูกเลือก + */ async function onSelectSystem(value: string) { await fetchData(value); - console.log("value", value); + if (systemMain.value === value && dataAttributes.value?.length > 0) { + updateSelectedAttributes(dataAttributes.value); + } } -// ฟังก์ชันตรวจสอบ validation +/** ฟังก์ชันตรวจสอบ validation */ function validateSelection(): boolean { validationErrors.value = []; @@ -151,6 +184,7 @@ function validateSelection(): boolean { return validationErrors.value.length === 0; } +/** ฟังก์ชันบันทึกข้อมูล */ function onSubmit() { if (!validateSelection()) { validationErrors.value.forEach((error) => { @@ -211,6 +245,21 @@ function onSubmit() { }); } +/** + * ฟังก์ชันอัพเดต selectedAttributes ตามข้อมูลที่ดึงมา + * @param data ข้อมูลที่ต้องการอัพเดต selectedAttributes + */ +function updateSelectedAttributes( + data: Array<{ tbName: string; propertyKey: string }> +) { + if (!data?.length) return; + data.forEach(({ tbName, propertyKey }) => { + if (selectedAttributes.value[tbName]?.[propertyKey] !== undefined) { + selectedAttributes.value[tbName][propertyKey] = true; + } + }); +} + /** * function ค้นหาคำใน select * @param val คำค้นหา @@ -225,6 +274,7 @@ function filterSelector(val: string, update: Function) { }); } +/** ฟังก์ชันปิด dialog และรีเซ็ตข้อมูล */ function onCloseDialog() { modal.value = false; formData.name = ""; @@ -235,6 +285,8 @@ function onCloseDialog() { availableFields.value = []; selectedAttributes.value = {}; validationErrors.value = []; + dataAttributes.value = []; + systemMain.value = ""; } watch(modal, (newVal) => { @@ -257,11 +309,10 @@ watch(modal, (newVal) => { -
-
+
{ :rules="[(val:string) => !!val || 'กรุณากรอก API Name']" />
-
+
+ +
+
{ disable />
-
+
{
+ +
+
+ เลือกระบบ +
+
+
+
+
+
+ + + +
+ +
+ +
+ + + + + + +
+
+
+ -
+ -
-
- -
- -
- {{ field.description }} -
- - - -
- - - - - - {{ property.comment }} - - {{ property.propertyName }} ({{ property.type }}) - - - -
- - -
- ไม่มี attributes ย่อยสำหรับหมวดนี้ -
-
-
-
- - -
- -
กำลังโหลดข้อมูล...
-
- - -
- -
- ไม่มีข้อมูล Attributes สำหรับระบบนี้ -
-
+
-->
diff --git a/src/modules/06_webservices/interface/index/Main.ts b/src/modules/06_webservices/interface/index/Main.ts index d58d76df..739e8660 100644 --- a/src/modules/06_webservices/interface/index/Main.ts +++ b/src/modules/06_webservices/interface/index/Main.ts @@ -31,4 +31,9 @@ interface DataOption { name: string; } -export type { ListWebServices, ListApi, DataOption, ItemsDropdown }; +interface Pagination { + page: number; + rowsPerPage: number; +} + +export type { ListWebServices, ListApi, DataOption, ItemsDropdown, Pagination }; diff --git a/src/modules/06_webservices/interface/response/Main.ts b/src/modules/06_webservices/interface/response/Main.ts index a95fd168..1d0e307d 100644 --- a/src/modules/06_webservices/interface/response/Main.ts +++ b/src/modules/06_webservices/interface/response/Main.ts @@ -1,3 +1,5 @@ +import type { D } from "@fullcalendar/core/internal-common"; + interface ResListWebServices { id: string; topic: string; @@ -26,4 +28,49 @@ interface ResApHistory { ipApi: string; } -export type { ResListWebServices, ResApiName, ResApHistory }; +interface ListSystems { + code: string; + name: string; +} + +interface ListApiManages { + code: string; + createdAt: Date; + id: string; + isActive: boolean; + lastUpdatedAt: Date; + methodApi: string; + name: string; + pathApi: string; + system: string; +} + +interface DataSystems { + description: string; + isMain: boolean; + tb: string; + propertys: Propertys[]; +} + +interface Propertys { + comment: string; + key: string; + propertyName: string; + type: string; +} + +interface DataAttributes { + propertyKey: string; + tbName: string; +} + +export type { + ResListWebServices, + ResApiName, + ResApHistory, + ListSystems, + ListApiManages, + DataSystems, + Propertys, + DataAttributes, +}; diff --git a/src/modules/06_webservices/view/manage.vue b/src/modules/06_webservices/view/manage.vue index 35686238..6f4f773c 100644 --- a/src/modules/06_webservices/view/manage.vue +++ b/src/modules/06_webservices/view/manage.vue @@ -1,5 +1,5 @@