feat: add shareable catalog pagination
This commit is contained in:
@@ -2,8 +2,9 @@ import { ref, watch } from 'vue';
|
||||
|
||||
import { searchCatalog } from '@/services/catalog-service.js';
|
||||
|
||||
export function useCatalogSearch() {
|
||||
const query = ref('');
|
||||
export function useCatalogSearch({ initialQuery = '', initialPage = 1 } = {}) {
|
||||
const query = ref(initialQuery);
|
||||
const page = ref(initialPage);
|
||||
const result = ref({ items: [], pagination: { page: 1, limit: 20, total: 0, totalPages: 0 } });
|
||||
const isLoading = ref(false);
|
||||
const error = ref(null);
|
||||
@@ -15,7 +16,7 @@ export function useCatalogSearch() {
|
||||
error.value = null;
|
||||
|
||||
try {
|
||||
const nextResult = await searchCatalog({ query: query.value });
|
||||
const nextResult = await searchCatalog({ query: query.value, page: page.value });
|
||||
if (currentRequestId === requestId) result.value = nextResult;
|
||||
} catch (requestError) {
|
||||
if (currentRequestId === requestId) error.value = requestError;
|
||||
@@ -25,6 +26,13 @@ export function useCatalogSearch() {
|
||||
}
|
||||
|
||||
watch(query, load);
|
||||
watch(page, load);
|
||||
|
||||
return { query, result, isLoading, error, load };
|
||||
function goToPage(nextPage) {
|
||||
if (nextPage >= 1 && nextPage <= result.value.pagination.totalPages) {
|
||||
page.value = nextPage;
|
||||
}
|
||||
}
|
||||
|
||||
return { query, page, result, isLoading, error, load, goToPage };
|
||||
}
|
||||
Reference in New Issue
Block a user