Initial commit from monorepo
This commit is contained in:
84
app/composables/useCatalogSuppliers.ts
Normal file
84
app/composables/useCatalogSuppliers.ts
Normal file
@@ -0,0 +1,84 @@
|
||||
import { GetSupplierProfilesDocument } from '~/composables/graphql/public/exchange-generated'
|
||||
|
||||
const PAGE_SIZE = 24
|
||||
|
||||
// Shared state across list and map views
|
||||
const items = ref<any[]>([])
|
||||
const total = ref(0)
|
||||
const selectedFilter = ref('all')
|
||||
const isLoading = ref(false)
|
||||
const isLoadingMore = ref(false)
|
||||
const isInitialized = ref(false)
|
||||
|
||||
export function useCatalogSuppliers() {
|
||||
const { t } = useI18n()
|
||||
const { execute } = useGraphQL()
|
||||
|
||||
const filters = computed(() => [
|
||||
{ id: 'all', label: t('catalogSuppliersSection.filters.all') },
|
||||
{ id: 'verified', label: t('catalogSuppliersSection.filters.verified') }
|
||||
])
|
||||
|
||||
const itemsWithCoords = computed(() =>
|
||||
items.value.filter(s => s.latitude && s.longitude)
|
||||
)
|
||||
|
||||
const canLoadMore = computed(() => items.value.length < total.value)
|
||||
|
||||
const fetchPage = async (offset: number, replace = false) => {
|
||||
if (replace) isLoading.value = true
|
||||
try {
|
||||
const isVerified = selectedFilter.value === 'verified' ? true : null
|
||||
const data = await execute(
|
||||
GetSupplierProfilesDocument,
|
||||
{ limit: PAGE_SIZE, offset, isVerified },
|
||||
'public',
|
||||
'exchange'
|
||||
)
|
||||
const next = data?.getSupplierProfiles || []
|
||||
items.value = replace ? next : items.value.concat(next)
|
||||
total.value = data?.getSupplierProfilesCount ?? total.value
|
||||
isInitialized.value = true
|
||||
} finally {
|
||||
isLoading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
const loadMore = async () => {
|
||||
if (isLoadingMore.value) return
|
||||
isLoadingMore.value = true
|
||||
try {
|
||||
await fetchPage(items.value.length)
|
||||
} finally {
|
||||
isLoadingMore.value = false
|
||||
}
|
||||
}
|
||||
|
||||
// При смене фильтра - перезагрузка
|
||||
watch(selectedFilter, () => {
|
||||
if (isInitialized.value) {
|
||||
fetchPage(0, true)
|
||||
}
|
||||
})
|
||||
|
||||
// Initialize data if not already loaded
|
||||
const init = async () => {
|
||||
if (!isInitialized.value && items.value.length === 0) {
|
||||
await fetchPage(0, true)
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
items,
|
||||
total,
|
||||
selectedFilter,
|
||||
filters,
|
||||
isLoading,
|
||||
isLoadingMore,
|
||||
itemsWithCoords,
|
||||
canLoadMore,
|
||||
fetchPage,
|
||||
loadMore,
|
||||
init
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user