refactor: postal region, add new field on list
feat: implement postal region model and update address handling - Add new PostalRegion model and service - Replace postalCode with postalRegion in address-related components - Update schemas and models to use locationType_code consistently - Add usePostalRegion composable for postal code selection - Modify patient form to handle address changes more robustly feat(patient): add ID column and improve date formatting - Add patient ID column to patient list - Format dates using 'id-ID' locale in preview - Update identity number display for foreign patients - Include passport number for foreign nationals
This commit is contained in:
No files matched your search
@@ -1,23 +1,23 @@
|
||||
import { ref, computed, watch, readonly, type Ref } from 'vue'
|
||||
import { refDebounced } from '@vueuse/core'
|
||||
import type { PostalCode } from '~/models/postal-code'
|
||||
import type { PostalRegion } from '~/models/postal-region'
|
||||
import type { SelectItem } from '~/components/pub/my-ui/form/select.vue'
|
||||
import * as postalCodeService from '../services/postal-code.service'
|
||||
import * as postalRegionService from '~/services/postal-region.service'
|
||||
|
||||
// Global cache untuk postal codes berdasarkan village code
|
||||
const postalCodesCache = ref<Map<string, PostalCode[]>>(new Map())
|
||||
const postalRegionCache = ref<Map<string, PostalRegion[]>>(new Map())
|
||||
const loadingStates = ref<Map<string, boolean>>(new Map())
|
||||
const errorStates = ref<Map<string, string | null>>(new Map())
|
||||
|
||||
export function usePostalCodes(villageCode: Ref<string | undefined> | string | undefined) {
|
||||
export function usePostalRegion(villageCode: Ref<string | undefined> | string | undefined) {
|
||||
// Convert villageCode ke ref jika bukan ref
|
||||
const villageCodeRef = typeof villageCode === 'string' || villageCode === undefined ? ref(villageCode) : villageCode
|
||||
|
||||
// Computed untuk mendapatkan postalCodes berdasarkan village code
|
||||
const postalCodes = computed(() => {
|
||||
// Computed untuk mendapatkan postalRegion berdasarkan village code
|
||||
const postalRegion = computed(() => {
|
||||
const code = villageCodeRef.value
|
||||
if (!code) return []
|
||||
return postalCodesCache.value.get(code) || []
|
||||
return postalRegionCache.value.get(code) || []
|
||||
})
|
||||
|
||||
// Computed untuk loading state
|
||||
@@ -35,22 +35,22 @@ export function usePostalCodes(villageCode: Ref<string | undefined> | string | u
|
||||
})
|
||||
|
||||
// Computed untuk format SelectItem
|
||||
const postalCodeOptions = computed<SelectItem[]>(() => {
|
||||
return postalCodes.value.map((postalCode) => ({
|
||||
label: postalCode.code,
|
||||
value: postalCode.code,
|
||||
searchValue: postalCode.code,
|
||||
const postalRegionOptions = computed<SelectItem[]>(() => {
|
||||
return postalRegion.value.map((postalRegion) => ({
|
||||
label: postalRegion.code,
|
||||
value: postalRegion.code,
|
||||
searchValue: postalRegion.code,
|
||||
}))
|
||||
})
|
||||
|
||||
// Function untuk fetch postalCodes berdasarkan village code
|
||||
async function fetchPostalCodes(villageCodeParam?: string, forceRefresh = false, isUserAction = false) {
|
||||
// Function untuk fetch postalRegion berdasarkan village code
|
||||
async function fetchpostalRegion(villageCodeParam?: string, forceRefresh = false, isUserAction = false) {
|
||||
const code = villageCodeParam || villageCodeRef.value
|
||||
if (!code) return
|
||||
|
||||
// Jika user action atau force refresh, selalu fetch
|
||||
// Jika bukan user action dan sudah ada cache, skip
|
||||
if (!isUserAction && !forceRefresh && postalCodesCache.value.has(code)) {
|
||||
if (!isUserAction && !forceRefresh && postalRegionCache.value.has(code)) {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -71,15 +71,15 @@ export function usePostalCodes(villageCode: Ref<string | undefined> | string | u
|
||||
errorStates.value.set(code, null)
|
||||
|
||||
try {
|
||||
const response = await postalCodeService.getList({
|
||||
const response = await postalRegionService.getList({
|
||||
sort: 'code:asc',
|
||||
'village-code': code,
|
||||
'page-no-limit': true,
|
||||
})
|
||||
|
||||
if (response.success) {
|
||||
const postalCodesData = response.body.data || []
|
||||
postalCodesCache.value.set(code, postalCodesData)
|
||||
const postalRegionData = response.body.data || []
|
||||
postalRegionCache.value.set(code, postalRegionData)
|
||||
} else {
|
||||
errorStates.value.set(code, 'Gagal memuat data kode pos')
|
||||
console.error('Failed to fetch postal codes:', response)
|
||||
@@ -93,20 +93,20 @@ export function usePostalCodes(villageCode: Ref<string | undefined> | string | u
|
||||
}
|
||||
}
|
||||
|
||||
// Function untuk mencari postalCode berdasarkan code
|
||||
function getPostalCodeByCode(code: string): PostalCode | undefined {
|
||||
// Function untuk mencari postalRegion berdasarkan code
|
||||
function getpostalRegionByCode(code: string): PostalRegion | undefined {
|
||||
const villageCode = villageCodeRef.value
|
||||
if (!villageCode) return undefined
|
||||
|
||||
const postalCodesForVillage = postalCodesCache.value.get(villageCode) || []
|
||||
return postalCodesForVillage.find((postalCode) => postalCode.code === code)
|
||||
const postalRegionForVillage = postalRegionCache.value.get(villageCode) || []
|
||||
return postalRegionForVillage.find((postalRegion) => postalRegion.code === code)
|
||||
}
|
||||
|
||||
// Function untuk clear cache village tertentu
|
||||
function clearCache(villageCodeParam?: string) {
|
||||
const code = villageCodeParam || villageCodeRef.value
|
||||
if (code) {
|
||||
postalCodesCache.value.delete(code)
|
||||
postalRegionCache.value.delete(code)
|
||||
loadingStates.value.delete(code)
|
||||
errorStates.value.delete(code)
|
||||
}
|
||||
@@ -114,16 +114,16 @@ export function usePostalCodes(villageCode: Ref<string | undefined> | string | u
|
||||
|
||||
// Function untuk clear semua cache
|
||||
function clearAllCache() {
|
||||
postalCodesCache.value.clear()
|
||||
postalRegionCache.value.clear()
|
||||
loadingStates.value.clear()
|
||||
errorStates.value.clear()
|
||||
}
|
||||
|
||||
// Function untuk refresh data
|
||||
function refreshPostalCodes(villageCodeParam?: string) {
|
||||
function refreshpostalRegion(villageCodeParam?: string) {
|
||||
const code = villageCodeParam || villageCodeRef.value
|
||||
if (code) {
|
||||
return fetchPostalCodes(code, true)
|
||||
return fetchpostalRegion(code, true)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -137,7 +137,7 @@ export function usePostalCodes(villageCode: Ref<string | undefined> | string | u
|
||||
if (newCode && newCode !== oldCode) {
|
||||
// Jika ada oldCode berarti user action (ganti pilihan)
|
||||
const isUserAction = !!oldCode
|
||||
fetchPostalCodes(newCode, false, isUserAction)
|
||||
fetchpostalRegion(newCode, false, isUserAction)
|
||||
}
|
||||
},
|
||||
{ immediate: true },
|
||||
@@ -145,25 +145,25 @@ export function usePostalCodes(villageCode: Ref<string | undefined> | string | u
|
||||
|
||||
return {
|
||||
// Data
|
||||
postalCodes: readonly(postalCodes),
|
||||
postalCodeOptions,
|
||||
postalRegion: readonly(postalRegion),
|
||||
postalRegionOptions,
|
||||
|
||||
// State
|
||||
isLoading: readonly(isLoading),
|
||||
error: readonly(error),
|
||||
|
||||
// Methods
|
||||
fetchPostalCodes,
|
||||
refreshPostalCodes,
|
||||
getPostalCodeByCode,
|
||||
fetchpostalRegion,
|
||||
refreshpostalRegion,
|
||||
getpostalRegionByCode,
|
||||
clearCache,
|
||||
clearAllCache,
|
||||
}
|
||||
}
|
||||
|
||||
// Export untuk direct access ke cached data (jika diperlukan)
|
||||
export const usePostalCodesCache = () => ({
|
||||
postalCodesCache: readonly(postalCodesCache),
|
||||
export const usepostalRegionCache = () => ({
|
||||
postalRegionCache: readonly(postalRegionCache),
|
||||
loadingStates: readonly(loadingStates),
|
||||
errorStates: readonly(errorStates),
|
||||
})
|
||||
Reference in New Issue
Block a user