Files
webapp/app/composables/useCatalogProducts.ts
Ruslan Bakiev 9b99d8981c
All checks were successful
Build Docker Image / build (push) Successful in 4m12s
Optimize catalog loading: backend bounds filtering + early returns
- Add bounds (west/south/east/north) parameters to GetNodes query
- Add setBoundsFilter to useCatalogHubs, useCatalogSuppliers, useCatalogProducts
- Replace client-side bounds filtering with backend query
- Add early return to setProductFilter to avoid redundant fetches
- Watch filterByBounds to trigger backend refetch when checkbox changes
2026-01-24 12:19:00 +07:00

124 lines
3.2 KiB
TypeScript

import {
GetProductsDocument,
GetProductsBySupplierDocument,
GetProductsNearHubDocument
} from '~/composables/graphql/public/geo-generated'
// Shared state
const items = ref<any[]>([])
const isLoading = ref(false)
const isLoadingMore = ref(false)
const isInitialized = ref(false)
// Filter state
const filterSupplierUuid = ref<string | null>(null)
const filterHubUuid = ref<string | null>(null)
export function useCatalogProducts() {
const { execute } = useGraphQL()
// Products don't have server-side pagination yet, so we load all at once
const canLoadMore = computed(() => false)
const fetchProducts = async () => {
if (isLoading.value) return
isLoading.value = true
try {
let data
if (filterSupplierUuid.value) {
// Products from specific supplier
data = await execute(
GetProductsBySupplierDocument,
{ supplierUuid: filterSupplierUuid.value },
'public',
'geo'
)
items.value = data?.productsBySupplier || []
} else if (filterHubUuid.value) {
// Products near hub
data = await execute(
GetProductsNearHubDocument,
{ hubUuid: filterHubUuid.value },
'public',
'geo'
)
items.value = data?.productsNearHub || []
} else {
// All products
data = await execute(
GetProductsDocument,
{},
'public',
'geo'
)
items.value = data?.products || []
}
isInitialized.value = true
} finally {
isLoading.value = false
}
}
const loadMore = async () => {
// No-op: products don't support pagination yet
}
const init = async () => {
if (!isInitialized.value && items.value.length === 0) {
await fetchProducts()
}
}
// Filter setters
const setSupplierFilter = (uuid: string | null) => {
if (filterSupplierUuid.value !== uuid) {
filterSupplierUuid.value = uuid
filterHubUuid.value = null // clear other filter
isInitialized.value = false
fetchProducts()
}
}
const setHubFilter = (uuid: string | null) => {
if (filterHubUuid.value !== uuid) {
filterHubUuid.value = uuid
filterSupplierUuid.value = null // clear other filter
isInitialized.value = false
fetchProducts()
}
}
const clearFilters = () => {
if (filterSupplierUuid.value || filterHubUuid.value) {
filterSupplierUuid.value = null
filterHubUuid.value = null
isInitialized.value = false
fetchProducts()
}
}
// Products don't have coordinates directly (they're an aggregation of offers)
// Bounds filtering would require a new backend query that filters by offer locations
// For now, this is a no-op - products show all regardless of map bounds
const setBoundsFilter = (_bounds: { west: number; south: number; east: number; north: number } | null) => {
// No-op: products are not filterable by map bounds in current implementation
}
return {
items,
isLoading,
isLoadingMore,
isInitialized,
canLoadMore,
fetchProducts,
loadMore,
init,
setSupplierFilter,
setHubFilter,
clearFilters,
setBoundsFilter
}
}