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