2026-02-17 13:07:29 -05:00
|
|
|
|
---
|
2026-02-22 11:00:30 -05:00
|
|
|
|
import { client } from '../../db/typesense.ts';
|
|
|
|
|
|
import { db } from '../../db';
|
|
|
|
|
|
import RarityIcon from '../../components/RarityIcon.astro';
|
2026-02-17 13:07:29 -05:00
|
|
|
|
|
2026-02-22 11:00:30 -05:00
|
|
|
|
export const prerender = false;
|
|
|
|
|
|
|
|
|
|
|
|
// get the query from post request using form data
|
|
|
|
|
|
const formData = await Astro.request.formData();
|
|
|
|
|
|
const query = formData.get('q')?.toString() || '';
|
|
|
|
|
|
|
|
|
|
|
|
// use typesense to search for cards matching the query and return the productIds of the results
|
2026-02-17 13:27:48 -05:00
|
|
|
|
const searchResults = await client.collections('cards').documents().search({
|
|
|
|
|
|
q: query,
|
2026-02-22 11:00:30 -05:00
|
|
|
|
filter_by: 'sealed:false',
|
2026-02-21 16:26:34 -05:00
|
|
|
|
query_by: 'productLineName,productName,setName,number,rarityName',
|
2026-02-17 13:27:48 -05:00
|
|
|
|
per_page: 250,
|
|
|
|
|
|
});
|
2026-02-17 13:42:00 -05:00
|
|
|
|
const productIds = searchResults.hits?.map((hit: any) => hit.document.productId) ?? [];
|
2026-02-17 13:27:48 -05:00
|
|
|
|
|
|
|
|
|
|
// 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
|
2026-02-17 13:07:29 -05:00
|
|
|
|
const pokemon = await db.query.cards.findMany({
|
2026-02-17 13:27:48 -05:00
|
|
|
|
where: { productId: { in: productIds, }, },
|
2026-02-17 13:07:29 -05:00
|
|
|
|
with: {
|
|
|
|
|
|
prices: true,
|
|
|
|
|
|
set: true,
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2026-02-22 11:00:30 -05:00
|
|
|
|
// format price to 2 decimal places (or 0 if price >=100) and adds a $ sign, if the price is null it returns "–"
|
2026-02-17 13:07:29 -05:00
|
|
|
|
const formatPrice = (price:any) => {
|
2026-02-23 07:53:57 -05:00
|
|
|
|
if (price === null) {
|
|
|
|
|
|
return "—";
|
|
|
|
|
|
}
|
2026-02-17 13:07:29 -05:00
|
|
|
|
price = Number(price);
|
2026-02-22 11:00:30 -05:00
|
|
|
|
if (price > 99.99) return `$${Math.round(price)}`;
|
|
|
|
|
|
return `$${price.toFixed(2)}`;
|
2026-02-17 13:07:29 -05:00
|
|
|
|
};
|
|
|
|
|
|
|
2026-02-22 11:00:30 -05:00
|
|
|
|
const conditionOrder = ["Near Mint", "Lightly Played", "Moderately Played", "Heavily Played", "Damaged"];
|
2026-02-17 13:07:29 -05:00
|
|
|
|
---
|
|
|
|
|
|
{pokemon.map((card) => (
|
2026-02-21 16:26:34 -05:00
|
|
|
|
<div class="col">
|
|
|
|
|
|
<div hx-get={`/partials/card-modal?productId=${card.productId}`} hx-target="#cardModal" hx-trigger="click" data-bs-toggle="modal" data-bs-target="#cardModal">
|
|
|
|
|
|
<img src={`/cards/${card.productId}.jpg`} alt={card.productName} loading="lazy" decoding="async" class="img-fluid rounded-3 mb-2 card-image w-100" onerror="this.onerror=null;this.src='/cards/noImage.webp'"/>
|
|
|
|
|
|
</div>
|
2026-02-19 07:55:18 -05:00
|
|
|
|
<div class="row row-cols-5 gx-1 price-row mb-2">
|
2026-02-17 13:07:29 -05:00
|
|
|
|
{card.prices
|
|
|
|
|
|
.slice()
|
2026-02-22 11:00:30 -05:00
|
|
|
|
.sort((a, b) => conditionOrder.indexOf(a.condition) - conditionOrder.indexOf(b.condition))
|
2026-02-17 13:07:29 -05:00
|
|
|
|
.filter((price, index, arr) =>
|
|
|
|
|
|
arr.findIndex(p => p.condition === price.condition) === index
|
|
|
|
|
|
)
|
|
|
|
|
|
.map((price) => (
|
2026-02-21 16:26:34 -05:00
|
|
|
|
<div class="col price-label ps-1">
|
2026-02-17 13:07:29 -05:00
|
|
|
|
{price.condition.split(' ').map((w) => w[0]).join('')}
|
2026-02-22 11:00:30 -05:00
|
|
|
|
<br />{formatPrice(price.marketPrice)}
|
2026-02-17 13:07:29 -05:00
|
|
|
|
</div>
|
|
|
|
|
|
))}
|
|
|
|
|
|
</div>
|
2026-02-19 07:55:18 -05:00
|
|
|
|
<div class="h5 my-0">{card.productName}</div>
|
2026-02-19 13:46:42 -05:00
|
|
|
|
<div class="d-flex flex-row lh-1 mt-1">
|
2026-02-21 16:26:34 -05:00
|
|
|
|
<div class="text-secondary flex-grow-1">{card.set?.setCode}</div>
|
|
|
|
|
|
<div class="text-secondary">{card.number}</div>
|
2026-02-20 09:11:36 -05:00
|
|
|
|
<span class="ps-2 small-icon"><RarityIcon rarity={card.rarityName} /></span>
|
2026-02-19 07:55:18 -05:00
|
|
|
|
</div>
|
2026-02-17 13:07:29 -05:00
|
|
|
|
</div>
|
2026-02-22 11:00:30 -05:00
|
|
|
|
))}
|