diff --git a/src/stores/dataStore.ts b/src/stores/dataStore.ts index acdbc7e..af13037 100644 --- a/src/stores/dataStore.ts +++ b/src/stores/dataStore.ts @@ -1,11 +1,13 @@ import { defineStore } from 'pinia' import { ref, reactive } from 'vue' import { useGlobalDataStore } from '@/stores/globalDataStore' +import { useGlobalStore } from '@/stores/globalStore' import { useToastStore } from '@/components/global/toastStore' import axios from 'axios' export const useDataStore = defineStore('dataStore', () => { const globalDataStore = useGlobalDataStore() + const globalStore = useGlobalStore() const toastStore = useToastStore() const siteBase = ref(import.meta.env.VITE_API_URL) @@ -26,6 +28,8 @@ export const useDataStore = defineStore('dataStore', () => { options?: Record toast?: Record full?: boolean + /** Bu status kodları için CheckApiError atlanır, hata objesi döner */ + skipErrorForStatuses?: number[] } = {} ) => { try { @@ -37,7 +41,10 @@ export const useDataStore = defineStore('dataStore', () => { if (data.headers !== undefined) sendData.headers = data.headers 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) { toastStore.AddToast(data.toast.toast, data.toast.type, data.toast.timeout) @@ -49,7 +56,11 @@ export const useDataStore = defineStore('dataStore', () => { return response.data } } 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) 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[]) => { + Object.keys(globalDataStore.ilMapById).forEach((key) => { + delete globalDataStore.ilMapById[key] + }) + Object.keys(globalDataStore.ilMapByAd).forEach((key) => { + delete globalDataStore.ilMapByAd[key] + }) + + list.forEach((item: Record) => { + globalDataStore.ilMapById[String(item.id)] = item + globalDataStore.ilMapByAd[normalizeLocationKey(item.ad)] = item + }) + } + const GetIlList = async () => { 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 { isLoading, siteBase, @@ -237,6 +316,10 @@ export const useDataStore = defineStore('dataStore', () => { dataPut, dataDelete, GetCustomerTipList, - GetIlList + GetIlList, + GetIlcelerByIlId, + GetMahallelerByIlceId, + GetIlById, + GetIlByName } }) diff --git a/src/stores/globalDataStore.ts b/src/stores/globalDataStore.ts index bc4fb74..61528a0 100644 --- a/src/stores/globalDataStore.ts +++ b/src/stores/globalDataStore.ts @@ -1,12 +1,27 @@ import { defineStore } from 'pinia' -import { ref } from 'vue' +import { reactive, ref } from 'vue' export const useGlobalDataStore = defineStore('globalDataStore', () => { const customerTips = ref[]>([]) - const ilList = ref[]>([]) + const ilList = ref[]>([]) + const ilMapById = reactive>>({}) + const ilMapByAd = reactive>>({}) + const ilcelerByIlId = reactive[]>>({}) + const mahallelerByIlceId = reactive[]>>({}) + const locationPerfStats = reactive({ + ilListCount: 0, + ilListRequestMs: 0, + ilceRequestMs: 0, + mahalleRequestMs: 0 + }) return { customerTips, - ilList + ilList, + ilMapById, + ilMapByAd, + ilcelerByIlId, + mahallelerByIlceId, + locationPerfStats } })