Add public category lookup options, allowlisted sort ordering, URL-backed catalog controls, and strict search route validation with regression tests.
This commit is contained in:
@@ -2,11 +2,12 @@ import { ref, watch } from 'vue';
|
||||
|
||||
import { searchCatalog } from '@/services/catalog-service.js';
|
||||
|
||||
export function useCatalogSearch({ initialQuery = '', initialCategory = '', initialMinPriceMinor, initialMaxPriceMinor, initialPage = 1 } = {}) {
|
||||
export function useCatalogSearch({ initialQuery = '', initialCategory = '', initialMinPriceMinor, initialMaxPriceMinor, initialSort = 'recent', initialPage = 1 } = {}) {
|
||||
const query = ref(initialQuery);
|
||||
const category = ref(initialCategory);
|
||||
const minPriceMinor = ref(initialMinPriceMinor);
|
||||
const maxPriceMinor = ref(initialMaxPriceMinor);
|
||||
const sort = ref(initialSort);
|
||||
const page = ref(initialPage);
|
||||
const result = ref({ items: [], pagination: { page: 1, limit: 20, total: 0, totalPages: 0 } });
|
||||
const isLoading = ref(false);
|
||||
@@ -19,7 +20,7 @@ export function useCatalogSearch({ initialQuery = '', initialCategory = '', init
|
||||
error.value = null;
|
||||
|
||||
try {
|
||||
const nextResult = await searchCatalog({ query: query.value, category: category.value, minPriceMinor: minPriceMinor.value, maxPriceMinor: maxPriceMinor.value, page: page.value });
|
||||
const nextResult = await searchCatalog({ query: query.value, category: category.value, minPriceMinor: minPriceMinor.value, maxPriceMinor: maxPriceMinor.value, sort: sort.value, page: page.value });
|
||||
if (currentRequestId === requestId) result.value = nextResult;
|
||||
} catch (requestError) {
|
||||
if (currentRequestId === requestId) error.value = requestError;
|
||||
@@ -28,7 +29,7 @@ export function useCatalogSearch({ initialQuery = '', initialCategory = '', init
|
||||
}
|
||||
}
|
||||
|
||||
watch([query, category, minPriceMinor, maxPriceMinor], () => {
|
||||
watch([query, category, minPriceMinor, maxPriceMinor, sort], () => {
|
||||
if (page.value !== 1) page.value = 1;
|
||||
else load();
|
||||
});
|
||||
@@ -40,5 +41,5 @@ export function useCatalogSearch({ initialQuery = '', initialCategory = '', init
|
||||
}
|
||||
}
|
||||
|
||||
return { query, category, minPriceMinor, maxPriceMinor, page, result, isLoading, error, load, goToPage };
|
||||
return { query, category, minPriceMinor, maxPriceMinor, sort, page, result, isLoading, error, load, goToPage };
|
||||
}
|
||||
@@ -1,29 +1,35 @@
|
||||
<script setup>
|
||||
import { onMounted, watch } from 'vue';
|
||||
import { onMounted, ref, watch } from 'vue';
|
||||
import { useRoute, useRouter } from 'vue-router';
|
||||
|
||||
import { useCatalogSearch } from '@/composables/useCatalogSearch.js';
|
||||
import { listPublicCategories } from '@/services/catalog-service.js';
|
||||
import ListingCard from '@/components/molecules/ListingCard.vue';
|
||||
|
||||
const route = useRoute();
|
||||
const router = useRouter();
|
||||
const { query, category, minPriceMinor, maxPriceMinor, page, result, isLoading, error, load, goToPage } = useCatalogSearch({
|
||||
const categories = ref([]);
|
||||
const { query, category, minPriceMinor, maxPriceMinor, sort, page, result, isLoading, error, load, goToPage } = useCatalogSearch({
|
||||
initialQuery: typeof route.query.q === 'string' ? route.query.q : '',
|
||||
initialCategory: typeof route.query.category === 'string' ? route.query.category : '',
|
||||
initialMinPriceMinor: Number.parseInt(route.query.min, 10) || undefined,
|
||||
initialMaxPriceMinor: Number.parseInt(route.query.max, 10) || undefined,
|
||||
initialSort: typeof route.query.sort === 'string' ? route.query.sort : 'recent',
|
||||
initialPage: Number.parseInt(route.query.page, 10) || 1,
|
||||
});
|
||||
|
||||
onMounted(load);
|
||||
onMounted(async () => {
|
||||
await Promise.all([load(), listPublicCategories().then((items) => { categories.value = items; }).catch(() => {})]);
|
||||
});
|
||||
|
||||
watch([query, category, minPriceMinor, maxPriceMinor, page], ([nextQuery, nextCategory, nextMin, nextMax, nextPage]) => {
|
||||
watch([query, category, minPriceMinor, maxPriceMinor, sort, page], ([nextQuery, nextCategory, nextMin, nextMax, nextSort, nextPage]) => {
|
||||
router.replace({
|
||||
query: {
|
||||
...(nextQuery ? { q: nextQuery } : {}),
|
||||
...(nextCategory ? { category: nextCategory } : {}),
|
||||
...(typeof nextMin === 'number' ? { min: String(nextMin) } : {}),
|
||||
...(typeof nextMax === 'number' ? { max: String(nextMax) } : {}),
|
||||
...(nextSort !== 'recent' ? { sort: nextSort } : {}),
|
||||
...(nextPage > 1 ? { page: String(nextPage) } : {}),
|
||||
},
|
||||
});
|
||||
@@ -41,9 +47,10 @@ watch([query, category, minPriceMinor, maxPriceMinor, page], ([nextQuery, nextCa
|
||||
<input v-model="query" type="search" placeholder="Try Charizard, 151, plush..." />
|
||||
</label>
|
||||
<div class="catalog-filters">
|
||||
<label>Category <input v-model="category" type="search" placeholder="Cards or plush" /></label>
|
||||
<label>Category <select v-model="category"><option value="">All categories</option><option v-for="item in categories" :key="item.id" :value="item.id">{{ item.name }}</option></select></label>
|
||||
<label>Min price (cents) <input v-model.number="minPriceMinor" type="number" min="0" /></label>
|
||||
<label>Max price (cents) <input v-model.number="maxPriceMinor" type="number" min="0" /></label>
|
||||
<label>Sort <select v-model="sort"><option value="recent">Recently added</option><option value="price_low">Price: low to high</option><option value="price_high">Price: high to low</option></select></label>
|
||||
</div>
|
||||
</header>
|
||||
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
const apiBaseUrl = import.meta.env?.VITE_API_BASE_URL ?? '/api/v1';
|
||||
|
||||
export async function searchCatalog({ query = '', category = '', minPriceMinor, maxPriceMinor, page = 1, limit = 20 } = {}) {
|
||||
export async function searchCatalog({ query = '', category = '', minPriceMinor, maxPriceMinor, sort = 'recent', page = 1, limit = 20 } = {}) {
|
||||
const params = new URLSearchParams({ query, page: String(page), limit: String(limit) });
|
||||
if (sort !== 'recent') params.set('sort', sort);
|
||||
if (category) params.set('category', category);
|
||||
if (typeof minPriceMinor === 'number') params.set('minPriceMinor', String(minPriceMinor));
|
||||
if (typeof maxPriceMinor === 'number') params.set('maxPriceMinor', String(maxPriceMinor));
|
||||
@@ -15,6 +16,12 @@ export async function searchCatalog({ query = '', category = '', minPriceMinor,
|
||||
return payload.data;
|
||||
}
|
||||
|
||||
export async function listPublicCategories() {
|
||||
const response = await fetch(`${apiBaseUrl}/public/lookups/categories`);
|
||||
if (!response.ok) throw new Error('Categories are unavailable.');
|
||||
return (await response.json()).data;
|
||||
}
|
||||
|
||||
export async function getPublicListing(id) {
|
||||
const response = await fetch(`${apiBaseUrl}/public/listings/${encodeURIComponent(id)}`);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user