diff --git a/frontend/app/composables/useConstructeurs.ts b/frontend/app/composables/useConstructeurs.ts index ef7e7ea..6c02e08 100644 --- a/frontend/app/composables/useConstructeurs.ts +++ b/frontend/app/composables/useConstructeurs.ts @@ -1,7 +1,7 @@ import { ref } from 'vue' import { useApi } from './useApi' import { useToast } from './useToast' -import { extractCollection } from '~/shared/utils/apiHelpers' +import { extractCollection, extractTotal } from '~/shared/utils/apiHelpers' export interface ConstructeurTelephone { '@id'?: string @@ -33,6 +33,24 @@ interface ConstructeurResult { error?: string } +export interface ConstructeurPageOptions { + page?: number + itemsPerPage?: number + search?: string + categoryId?: string + orderField?: 'name' | 'email' | 'createdAt' + orderDirection?: 'asc' | 'desc' +} + +export interface ConstructeurPageResult { + success: boolean + items: Constructeur[] + totalItems: number + totalPages: number + currentPage: number + error?: string +} + const constructeurs = ref([]) const loading = ref(false) const loaded = ref(false) @@ -83,8 +101,10 @@ export function useConstructeurs() { } loading.value = true try { - const query = search ? `?search=${encodeURIComponent(search)}` : '' - const result = await get(`/constructeurs${query}`) + const params = new URLSearchParams() + params.set('itemsPerPage', '2000') + if (search) params.set('search', search) + const result = await get(`/constructeurs?${params.toString()}`) if (result.success) { const items = extractCollection(result.data) constructeurs.value = uniqueConstructeurs(items) @@ -104,6 +124,37 @@ export function useConstructeurs() { return loadConstructeurs(search) } + const fetchConstructeursPage = async (opts: ConstructeurPageOptions = {}): Promise => { + const page = Math.max(1, opts.page ?? 1) + const itemsPerPage = Math.max(1, opts.itemsPerPage ?? 30) + loading.value = true + try { + const params = new URLSearchParams() + params.set('page', String(page)) + params.set('itemsPerPage', String(itemsPerPage)) + if (opts.search && opts.search.trim()) params.set('search', opts.search.trim()) + if (opts.categoryId) params.set('categories.id', opts.categoryId) + if (opts.orderField) { + params.set(`order[${opts.orderField}]`, opts.orderDirection ?? 'asc') + } + const result = await get(`/constructeurs?${params.toString()}`) + if (!result.success) { + return { success: false, items: [], totalItems: 0, totalPages: 0, currentPage: page, error: result.error } + } + const items = extractCollection(result.data) + const totalItems = extractTotal(result.data, items.length) + const totalPages = Math.max(1, Math.ceil(totalItems / itemsPerPage)) + upsertConstructeurs(items) + return { success: true, items, totalItems, totalPages, currentPage: page } + } catch (error) { + const err = error as Error + console.error('Erreur lors du chargement de la page fournisseurs:', error) + return { success: false, items: [], totalItems: 0, totalPages: 0, currentPage: page, error: err.message } + } finally { + loading.value = false + } + } + const createConstructeur = async (data: Record): Promise => { loading.value = true try { @@ -227,6 +278,7 @@ export function useConstructeurs() { loading, loadConstructeurs, searchConstructeurs, + fetchConstructeursPage, createConstructeur, updateConstructeur, deleteConstructeur, diff --git a/frontend/app/pages/constructeurs.vue b/frontend/app/pages/constructeurs.vue index 1e3268b..84bd721 100644 --- a/frontend/app/pages/constructeurs.vue +++ b/frontend/app/pages/constructeurs.vue @@ -19,13 +19,17 @@