--- import { client } from '../../db/typesense'; import { db } from '../../db'; import RarityIcon from '../../components/RarityIcon.astro'; export const prerender = false; // all the facet fields we want to use for filtering const facetFields = { "productLineName": "Product Line", "setName": "Set", "variant": "Variant", "rarityName": "Rarity", "cardType": "Card Type", "energyType": "Energy Type" } // get the query from post request using form data const formData = await Astro.request.formData(); const query = formData.get('q')?.toString() || ''; const start = Number(formData.get('start')?.toString() || '0'); const filters = Array.from(formData.entries()) .filter(([key, value]) => key !== 'q' && key !== 'start') .reduce((acc, [key, value]) => { if (!acc[key]) { acc[key] = []; } acc[key].push(value.toString()); return acc; }, {} as Record); const filterChecked = (field: string, value: string) => { return (filters[field]?.includes(value) ?? false) ? 'checked' : ''; }; const filterBy = Object.entries(filters).map(([field, values]) => { return `${field}:=[${values.join(',')}]`; }).join(' && '); const facetFilter = (facet:string) => { const otherFilters = Object.entries(filters) .filter(([field]) => field !== facet) .map(([field, values]) => `${field}:=[${values.join(',')}]`) .join(' && '); return `sealed:false${otherFilters ? ` && ${otherFilters}` : ''}`; }; // primary search values (for cards) let searchArray = [{ collection: 'cards', filter_by: `sealed:false${filterBy ? ` && ${filterBy}` : ''}`, per_page: 20, facet_by: Object.keys(facetFields).join(','), page: Math.floor(start / 20) + 1, sort_by: '_text_match:asc, releaseDate:desc, number:asc', }]; // on first load (start === 0) we want to get the facets for the filters if (start === 0) { for (const facet of Object.keys(facetFields)) { searchArray.push({ collection: 'cards', filter_by: facetFilter(facet), per_page: 0, facet_by: facet, page: 1, sort_by: '' }); } } const searchRequests = { searches: searchArray }; const commonSearchParams = { q: query, query_by: 'productLineName,productName,setName,number,rarityName,Artist', }; // use typesense to search for cards matching the query and return the productIds of the results const searchResults = await client.multiSearch.perform(searchRequests, commonSearchParams); //console.log(searchResults); const cardResults = searchResults.results[0] as any; const cardIds = cardResults.hits?.map((hit: any) => hit.document.cardId) ?? []; const totalHits = cardResults?.found; //const facets = cardResults?.facet_counts; // get pokemon data with prices and set info using searchResults and then query the database for each card to get the prices and set info const pokemon = await db.query.cards.findMany({ where: { cardId: { in: cardIds, }, }, with: { prices: true, set: true, } }); // format price to 2 decimal places (or 0 if price >=100) and adds a $ sign, if the price is null it returns "–" const formatPrice = (price:any) => { if (price === null) { return "—"; } price = Number(price); if (price > 99.99) return `$${Math.round(price)}`; return `$${price.toFixed(2)}`; }; const conditionOrder = ["Near Mint", "Lightly Played", "Moderately Played", "Heavily Played", "Damaged"]; const conditionShort = (condition:string) => { return { "Near Mint": "NM", "Lightly Played": "LP", "Moderately Played": "MP", "Heavily Played": "HP", "Damaged": "DMG" }[condition] || condition.split(' ').map((w) => w[0]).join(''); } const facetNames = (name:string) => { return facetFields[name] || name; } if (start === 0) { var facets = searchResults.results.slice(1).map((result: any) => { return result.facet_counts[0]; }); console.log(facets); } --- {(start === 0) &&
{facets.map((facet) => (
{facetNames(facet.field_name)}
{facet.counts.map((count) => (
))}
))}
} {pokemon.map((card) => (
+/-
{card.productName}
{card.prices .slice() .sort((a, b) => conditionOrder.indexOf(a.condition) - conditionOrder.indexOf(b.condition)) .filter((price, index, arr) => arr.findIndex(p => p.condition === price.condition) === index ) .map((price) => (
{ conditionShort(price.condition) }
{formatPrice(price.marketPrice)}
))}
{card.productName}
{card.set?.setCode}
{card.number}
{card.variant}{card.productId}
))} {start + 20 < totalHits &&
Loading...
}