Enhance useDataStore and globalDataStore by adding location management functions, including GetIlcelerByIlId, GetMahallelerByIlceId, GetIlById, and GetIlByName. Implement caching for improved performance and introduce skipErrorForStatuses option for flexible error handling in API responses. Update data structures to support new location mappings and performance statistics.

This commit is contained in:
burakovec
2026-03-31 09:47:06 +03:00
parent a5b604a265
commit 2615e9b3ee
2 changed files with 106 additions and 8 deletions

View File

@ -1,11 +1,13 @@
import { defineStore } from 'pinia' import { defineStore } from 'pinia'
import { ref, reactive } from 'vue' import { ref, reactive } from 'vue'
import { useGlobalDataStore } from '@/stores/globalDataStore' import { useGlobalDataStore } from '@/stores/globalDataStore'
import { useGlobalStore } from '@/stores/globalStore'
import { useToastStore } from '@/components/global/toastStore' import { useToastStore } from '@/components/global/toastStore'
import axios from 'axios' import axios from 'axios'
export const useDataStore = defineStore('dataStore', () => { export const useDataStore = defineStore('dataStore', () => {
const globalDataStore = useGlobalDataStore() const globalDataStore = useGlobalDataStore()
const globalStore = useGlobalStore()
const toastStore = useToastStore() const toastStore = useToastStore()
const siteBase = ref<string>(import.meta.env.VITE_API_URL) const siteBase = ref<string>(import.meta.env.VITE_API_URL)
@ -26,6 +28,8 @@ export const useDataStore = defineStore('dataStore', () => {
options?: Record<string, any> options?: Record<string, any>
toast?: Record<string, any> toast?: Record<string, any>
full?: boolean full?: boolean
/** Bu status kodları için CheckApiError atlanır, hata objesi döner */
skipErrorForStatuses?: number[]
} = {} } = {}
) => { ) => {
try { try {
@ -37,7 +41,10 @@ export const useDataStore = defineStore('dataStore', () => {
if (data.headers !== undefined) sendData.headers = data.headers if (data.headers !== undefined) sendData.headers = data.headers
const response = await axios.get(apiBase.value + api, sendData) const response = await axios.get(apiBase.value + api, sendData)
console.log('response --', api, response)
if (import.meta.env.DEV) {
console.log('response --', api, response)
}
if (data.toast !== undefined) { if (data.toast !== undefined) {
toastStore.AddToast(data.toast.toast, data.toast.type, data.toast.timeout) toastStore.AddToast(data.toast.toast, data.toast.type, data.toast.timeout)
@ -49,7 +56,11 @@ export const useDataStore = defineStore('dataStore', () => {
return response.data return response.data
} }
} catch (error: any) { } catch (error: any) {
CheckApiError(error.response?.status, error.response?.data) const status = error.response?.status
if (data.skipErrorForStatuses?.includes(status)) {
return { _error: true, status, data: error.response?.data }
}
CheckApiError(status, error.response?.data)
console.error('Hata oluştu -:', error) console.error('Hata oluştu -:', error)
return 'errorfalse' return 'errorfalse'
} }
@ -215,14 +226,82 @@ export const useDataStore = defineStore('dataStore', () => {
} }
} }
const normalizeLocationKey = (value: string | number | null | undefined): string => {
return globalStore.trToEngLower(String(value ?? '').trim())
}
const setLocationLookupMaps = (list: Record<string, any>[]) => {
Object.keys(globalDataStore.ilMapById).forEach((key) => {
delete globalDataStore.ilMapById[key]
})
Object.keys(globalDataStore.ilMapByAd).forEach((key) => {
delete globalDataStore.ilMapByAd[key]
})
list.forEach((item: Record<string, any>) => {
globalDataStore.ilMapById[String(item.id)] = item
globalDataStore.ilMapByAd[normalizeLocationKey(item.ad)] = item
})
}
const GetIlList = async () => { const GetIlList = async () => {
if (globalDataStore.ilList.length === 0) { if (globalDataStore.ilList.length === 0) {
let list = await dataGet('Il') const start = performance.now()
let list = await dataGet('Il/summary')
if (list !== 'errorfalse') globalDataStore.ilList = list if (list !== 'errorfalse') {
globalDataStore.ilList = list
setLocationLookupMaps(list)
globalDataStore.locationPerfStats.ilListCount = list.length
globalDataStore.locationPerfStats.ilListRequestMs = Math.round(performance.now() - start)
}
} }
} }
const GetIlcelerByIlId = async (ilId: string | number | null | undefined) => {
if (ilId === null || ilId === undefined || ilId === '') return []
const cacheKey = String(ilId)
const cachedList = globalDataStore.ilcelerByIlId[cacheKey]
if (cachedList !== undefined) return cachedList
const start = performance.now()
const list = await dataGet(`Il/${ilId}/ilceler`)
if (list === 'errorfalse') return []
globalDataStore.ilcelerByIlId[cacheKey] = list
globalDataStore.locationPerfStats.ilceRequestMs = Math.round(performance.now() - start)
return list
}
const GetMahallelerByIlceId = async (ilceId: string | number | null | undefined) => {
if (ilceId === null || ilceId === undefined || ilceId === '') return []
const cacheKey = String(ilceId)
const cachedList = globalDataStore.mahallelerByIlceId[cacheKey]
if (cachedList !== undefined) return cachedList
const start = performance.now()
const list = await dataGet(`Ilce/${ilceId}/mahalleler`)
if (list === 'errorfalse') return []
globalDataStore.mahallelerByIlceId[cacheKey] = list
globalDataStore.locationPerfStats.mahalleRequestMs = Math.round(
performance.now() - start
)
return list
}
const GetIlById = (ilId: string | number | null | undefined) => {
if (ilId === null || ilId === undefined || ilId === '') return undefined
return globalDataStore.ilMapById[String(ilId)]
}
const GetIlByName = (ilName: string | null | undefined) => {
if (ilName === null || ilName === undefined || ilName === '') return undefined
return globalDataStore.ilMapByAd[normalizeLocationKey(ilName)]
}
return { return {
isLoading, isLoading,
siteBase, siteBase,
@ -237,6 +316,10 @@ export const useDataStore = defineStore('dataStore', () => {
dataPut, dataPut,
dataDelete, dataDelete,
GetCustomerTipList, GetCustomerTipList,
GetIlList GetIlList,
GetIlcelerByIlId,
GetMahallelerByIlceId,
GetIlById,
GetIlByName
} }
}) })

View File

@ -1,12 +1,27 @@
import { defineStore } from 'pinia' import { defineStore } from 'pinia'
import { ref } from 'vue' import { reactive, ref } from 'vue'
export const useGlobalDataStore = defineStore('globalDataStore', () => { export const useGlobalDataStore = defineStore('globalDataStore', () => {
const customerTips = ref<Record<string, any>[]>([]) const customerTips = ref<Record<string, any>[]>([])
const ilList = ref<Record<string, any>[]>([]) const ilList = ref<Record<string, any>[]>([])
const ilMapById = reactive<Record<string, Record<string, any>>>({})
const ilMapByAd = reactive<Record<string, Record<string, any>>>({})
const ilcelerByIlId = reactive<Record<string, Record<string, any>[]>>({})
const mahallelerByIlceId = reactive<Record<string, Record<string, any>[]>>({})
const locationPerfStats = reactive({
ilListCount: 0,
ilListRequestMs: 0,
ilceRequestMs: 0,
mahalleRequestMs: 0
})
return { return {
customerTips, customerTips,
ilList ilList,
ilMapById,
ilMapByAd,
ilcelerByIlId,
mahallelerByIlceId,
locationPerfStats
} }
}) })