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