refactor(personal-detail): resetData befor fetchData

This commit is contained in:
DESKTOP-1R2VSQH\Lenovo ThinkPad E490 2026-04-09 13:42:40 +07:00
parent 376fba6059
commit c21cfa838f

View file

@ -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
} }
/** /**