refactor(personal-detail): resetData befor fetchData
This commit is contained in:
parent
376fba6059
commit
c21cfa838f
1 changed files with 36 additions and 11 deletions
|
|
@ -51,8 +51,8 @@ const registSubDistrictName = ref<string>("");
|
|||
|
||||
// Cache ข้อมูล
|
||||
const provincesCache = ref<any[]>([]);
|
||||
const districtsCache = ref<Map<string, any[]>>(new Map());
|
||||
const subDistrictsCache = ref<Map<string, any[]>>(new Map());
|
||||
const districtsCache = ref<Map<string, any[]>>(new Map()); // key: provinceId, value: districts[]
|
||||
const subDistrictsCache = ref<Map<string, any[]>>(new Map()); // key: districtId, value: subDistricts[]
|
||||
|
||||
/**หัวตาราง */
|
||||
const columns = ref<QTableProps["columns"]>([
|
||||
|
|
@ -102,6 +102,9 @@ const columns = ref<QTableProps["columns"]>([
|
|||
* ฟังก์ชันดึงข้อมูลรายละเอียด
|
||||
*/
|
||||
async function fetchData() {
|
||||
// Reset ข้อมูลเก่าก่อนโหลดข้อมูลใหม่
|
||||
resetData();
|
||||
|
||||
showLoader();
|
||||
await http
|
||||
.get(config.API.getDatapersonal(props.personalId))
|
||||
|
|
@ -133,6 +136,24 @@ async function fetchData() {
|
|||
await convertAddressIds();
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset ข้อมูลทุกครั้งที่เปิดคนใหม่
|
||||
*/
|
||||
function resetData() {
|
||||
// Reset ข้อมูลส่วนตัว
|
||||
personalForm.value = undefined;
|
||||
age.value = "";
|
||||
rows.value = [];
|
||||
|
||||
// Reset ข้อมูลที่อยู่
|
||||
currentProvinceName.value = "";
|
||||
currentDistrictName.value = "";
|
||||
currentSubDistrictName.value = "";
|
||||
registProvinceName.value = "";
|
||||
registDistrictName.value = "";
|
||||
registSubDistrictName.value = "";
|
||||
}
|
||||
|
||||
/**
|
||||
* ฟังก์ชันแปลง ID เป็นชื่อจริงของที่อยู่ (แบบ optimize)
|
||||
*/
|
||||
|
|
@ -144,18 +165,18 @@ async function convertAddressIds() {
|
|||
await loadProvinces();
|
||||
|
||||
// แปลงข้อมูลที่อยู่ปัจจุบัน
|
||||
if (personalForm.value.currentProvinceId) {
|
||||
if (personalForm.value.currentProvinceId && personalForm.value.currentProvinceId.trim() !== "") {
|
||||
currentProvinceName.value = getProvinceNameFromCache(
|
||||
personalForm.value.currentProvinceId
|
||||
);
|
||||
|
||||
if (personalForm.value.currentDistrictId) {
|
||||
if (personalForm.value.currentDistrictId && personalForm.value.currentDistrictId.trim() !== "") {
|
||||
currentDistrictName.value = await getDistrictNameOptimized(
|
||||
personalForm.value.currentProvinceId,
|
||||
personalForm.value.currentDistrictId
|
||||
);
|
||||
|
||||
if (personalForm.value.currentSubDistrictId) {
|
||||
if (personalForm.value.currentSubDistrictId && personalForm.value.currentSubDistrictId.trim() !== "") {
|
||||
currentSubDistrictName.value = await getSubDistrictNameOptimized(
|
||||
personalForm.value.currentDistrictId,
|
||||
personalForm.value.currentSubDistrictId
|
||||
|
|
@ -165,18 +186,18 @@ async function convertAddressIds() {
|
|||
}
|
||||
|
||||
// แปลงข้อมูลที่อยู่ตามทะเบียน
|
||||
if (personalForm.value.registProvinceId) {
|
||||
if (personalForm.value.registProvinceId && personalForm.value.registProvinceId.trim() !== "") {
|
||||
registProvinceName.value = getProvinceNameFromCache(
|
||||
personalForm.value.registProvinceId
|
||||
);
|
||||
|
||||
if (personalForm.value.registDistrictId) {
|
||||
if (personalForm.value.registDistrictId && personalForm.value.registDistrictId.trim() !== "") {
|
||||
registDistrictName.value = await getDistrictNameOptimized(
|
||||
personalForm.value.registProvinceId,
|
||||
personalForm.value.registDistrictId
|
||||
);
|
||||
|
||||
if (personalForm.value.registSubDistrictId) {
|
||||
if (personalForm.value.registSubDistrictId && personalForm.value.registSubDistrictId.trim() !== "") {
|
||||
registSubDistrictName.value = await getSubDistrictNameOptimized(
|
||||
personalForm.value.registDistrictId,
|
||||
personalForm.value.registSubDistrictId
|
||||
|
|
@ -215,13 +236,15 @@ function getProvinceNameFromCache(provinceId: string): string {
|
|||
|
||||
/**
|
||||
* ดึงชื่ออำเภอแบบ optimize (เรียก API เฉพาะจังหวัดที่ต้องการ)
|
||||
* @param provinceId ID ของจังหวัด
|
||||
* @param districtId ID ของอำเภอ
|
||||
*/
|
||||
async function getDistrictNameOptimized(
|
||||
provinceId: string,
|
||||
districtId: string
|
||||
): Promise<string> {
|
||||
try {
|
||||
// เช็ค cache ก่อน
|
||||
// เช็ค cache ว่ามีข้อมูล districts ของ province นี้หรือไม่
|
||||
if (!districtsCache.value.has(provinceId)) {
|
||||
const res = await http.get(config.API.listDistrict(provinceId));
|
||||
districtsCache.value.set(provinceId, res.data.result.districts);
|
||||
|
|
@ -238,13 +261,15 @@ async function getDistrictNameOptimized(
|
|||
|
||||
/**
|
||||
* ดึงชื่อตำบลแบบ optimize (เรียก API เฉพาะอำเภอที่ต้องการ)
|
||||
* @param districtId ID ของอำเภอ
|
||||
* @param subDistrictId ID ของตำบล
|
||||
*/
|
||||
async function getSubDistrictNameOptimized(
|
||||
districtId: string,
|
||||
subDistrictId: string
|
||||
): Promise<string> {
|
||||
try {
|
||||
// เช็ค cache ก่อน
|
||||
// เช็ค cache ว่ามีข้อมูล subDistricts ของ district นี้หรือไม่
|
||||
if (!subDistrictsCache.value.has(districtId)) {
|
||||
const res = await http.get(config.API.listSubDistrict(districtId));
|
||||
subDistrictsCache.value.set(districtId, res.data.result.subDistricts);
|
||||
|
|
@ -283,7 +308,7 @@ function formBmaofficer(val: string) {
|
|||
*/
|
||||
async function close() {
|
||||
props.close();
|
||||
rows.value = [];
|
||||
resetData(); // Reset ข้อมูลเมื่อปิด dialog
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue