Files
pokemon/src/components/Card.astro

66 lines
2.5 KiB
Plaintext
Raw Normal View History

2026-02-17 13:07:29 -05:00
---
//import { eq } from 'drizzle-orm';
import { isConditionalExpression } from 'typescript';
2026-02-17 13:27:48 -05:00
import { client } from '../db/typesense.ts';
2026-02-17 13:07:29 -05:00
import { db } from '../db';
//import * as schema from '../db/schema.ts';
2026-02-20 09:23:11 -05:00
import RarityIcon from './RarityIcon.astro';
2026-02-17 13:07:29 -05:00
2026-02-17 13:27:48 -05:00
const { query } = Astro.props;
const searchResults = await client.collections('cards').documents().search({
q: query,
query_by: 'productLineName,productName,setName,number,rarityName,Artist',
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,
}
});
const formatPrice = (price:any) => {
if (price === null) {
return "";
}
price = Number(price);
if (price >= 99.99) {
return `${Math.round(price)}`;
}
return `${price.toFixed(2)}`;
};
const order = ["Near Mint", "Lightly Played", "Moderately Played", "Heavily Played", "Damaged"];
---
{pokemon.map((card) => (
2026-02-20 11:54:05 -05:00
<div hx-get={`/partials/card-modal?productId=${card.productId}`} hx-target="#cardModal" hx-trigger="click" data-bs-toggle="modal" data-bs-target="#cardModal">
<div class="col tcg-card">
<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 class="row row-cols-5 gx-1 price-row mb-2">
2026-02-17 13:07:29 -05:00
{card.prices
.slice()
.sort((a, b) => order.indexOf(a.condition) - order.indexOf(b.condition))
.filter((price, index, arr) =>
arr.findIndex(p => p.condition === price.condition) === index
)
.map((price) => (
<div class="col price-label ps-xxl-2 ps-1">
2026-02-17 13:07:29 -05:00
{price.condition.split(' ').map((w) => w[0]).join('')}
<br />${formatPrice(price.marketPrice)}
</div>
))}
</div>
<div class="h5 my-0">{card.productName}</div>
<div class="d-flex flex-row lh-1 mt-1">
<div class="copy-small flex-grow-1">{card.set?.setCode}</div>
<div class="copy-small">{card.number}</div>
<span class="ps-2 small-icon"><RarityIcon rarity={card.rarityName} /></span>
</div>
2026-02-17 13:07:29 -05:00
</div>
</div>
2026-02-17 13:07:29 -05:00
))}