Files
webapp/app/stores/location.ts
Ruslan Bakiev 65b07271d9
All checks were successful
Build Docker Image / build (push) Successful in 4m11s
Simplify GEO API - use new list endpoints and routes in nearestOffers
- Replace GetNodesDocument with HubsListDocument in useCatalogHubs.ts
- Replace GetSupplierProfilesDocument with SuppliersListDocument in useCatalogSuppliers.ts
- Replace manual grouping with ProductsListDocument in useCatalogProducts.ts
- Update nearestOffers to pass hubUuid for server-side route calculation
- Remove RouteToCoordinate calls - routes now included in nearestOffers response
- Delete 15 obsolete GraphQL files
- Add 3 new list endpoints: HubsList, SuppliersList, ProductsList
- Fix TypeScript errors in CalcResultContent, LocationsContent, hubs page, location store
2026-01-26 14:08:21 +07:00

120 lines
4.3 KiB
TypeScript

import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
import { useGraphQL } from '~/composables/useGraphQL'
import { GetNodeDocument } from '~/composables/graphql/public/geo-generated'
interface SelectedLocation {
type: 'address' | 'hub'
uuid: string
name: string
latitude: number
longitude: number
}
export const useLocationStore = defineStore('location', () => {
const { mutate } = useGraphQL()
const selectedLocation = ref<SelectedLocation | null>(null)
const isLoading = ref(false)
const hasLocation = computed(() => !!selectedLocation.value)
const locationName = computed(() => selectedLocation.value?.name || '')
const locationType = computed(() => selectedLocation.value?.type || null)
async function fetch() {
isLoading.value = true
try {
const { GetTeamDocument } = await import('~/composables/graphql/team/teams-generated')
const { data: teamData, error: teamError } = await useServerQuery('location-team', GetTeamDocument, {}, 'team', 'teams')
if (teamError.value) throw teamError.value
const team = teamData.value?.team
if (team?.selectedLocation?.type && team?.selectedLocation?.uuid) {
const { type, uuid } = team.selectedLocation
if (type === 'hub') {
const { data: nodeData, error: nodeError } = await useServerQuery('location-node', GetNodeDocument, { uuid }, 'public', 'geo')
if (nodeError.value) throw nodeError.value
const hub = nodeData.value?.node
if (hub && hub.uuid && hub.name && hub.latitude != null && hub.longitude != null) {
selectedLocation.value = {
type: 'hub',
uuid: hub.uuid,
name: hub.name,
latitude: hub.latitude,
longitude: hub.longitude
}
}
} else if (type === 'address') {
const { GetTeamAddressesDocument } = await import('~/composables/graphql/team/teams-generated')
const { data: addressesData, error: addressesError } = await useServerQuery('location-addresses', GetTeamAddressesDocument, {}, 'team', 'teams')
if (addressesError.value) throw addressesError.value
const address = addressesData.value?.teamAddresses?.find((a) => a?.uuid === uuid)
if (address && address.uuid && address.name && address.latitude != null && address.longitude != null) {
selectedLocation.value = {
type: 'address',
uuid: address.uuid,
name: address.name,
latitude: address.latitude,
longitude: address.longitude
}
}
}
}
} finally {
isLoading.value = false
}
}
async function select(type: 'address' | 'hub', uuid: string, name: string, latitude: number, longitude: number) {
console.log('[locationStore.select] called', { type, uuid, name, latitude, longitude })
isLoading.value = true
try {
const { SetSelectedLocationDocument } = await import('~/composables/graphql/team/teams-generated')
console.log('[locationStore.select] calling mutate')
const result = await mutate(SetSelectedLocationDocument, { input: { type, uuid, name, latitude, longitude } }, 'team', 'teams') as { selectedLocation?: { success?: boolean } } | null
console.log('[locationStore.select] result:', result)
if (result?.selectedLocation?.success) {
selectedLocation.value = { type, uuid, name, latitude, longitude }
return true
}
console.error('[locationStore.select] success=false or missing', result)
return false
} catch (e) {
console.error('[locationStore.select] Error:', e)
return false
} finally {
isLoading.value = false
}
}
function setFromUserData(loc: { type: string; uuid: string; name: string; latitude: number; longitude: number } | null | undefined) {
if (loc?.type && loc?.uuid && loc?.name) {
selectedLocation.value = {
type: loc.type as 'address' | 'hub',
uuid: loc.uuid,
name: loc.name,
latitude: loc.latitude,
longitude: loc.longitude
}
}
}
function clear() {
selectedLocation.value = null
}
return {
selectedLocation,
isLoading,
hasLocation,
locationName,
locationType,
fetch,
select,
setFromUserData,
clear
}
})