|
1 | | -import { ID, type Models } from "appwrite"; |
2 | | -import { type FC, useEffect, useState } from "react"; |
3 | | -import { COLLECTION_ID, DATABASE_ID, getDatabase } from "../lib/Auth.ts"; |
4 | | -import type { CollectionRow } from "../types"; |
5 | | -import A1 from "../../../cards/A1.json"; |
6 | | -import type { Card as CardType } from "../types"; |
| 1 | +import type { CollectionRow } from '@/types' |
| 2 | +import { ID, type Models } from 'appwrite' |
| 3 | +import { type FC, useEffect, useState } from 'react' |
| 4 | +import A1 from '../../assets/cards/A1.json' |
| 5 | +import { COLLECTION_ID, DATABASE_ID, getDatabase } from '../lib/Auth.ts' |
| 6 | +import type { Card as CardType } from '../types' |
7 | 7 |
|
8 | 8 | interface Props { |
9 | | - user: Models.User<Models.Preferences>; |
| 9 | + user: Models.User<Models.Preferences> |
10 | 10 | } |
11 | 11 |
|
12 | 12 | export const Cards: FC<Props> = ({ user }) => { |
13 | | - const [ownedCards, setOwnedCards] = useState<CollectionRow[]>([]); |
| 13 | + const [ownedCards, setOwnedCards] = useState<CollectionRow[]>([]) |
14 | 14 |
|
15 | 15 | useEffect(() => { |
16 | | - fetchCollection(); |
17 | | - }, []); |
| 16 | + fetchCollection() |
| 17 | + }, []) |
18 | 18 |
|
19 | 19 | const fetchCollection = async () => { |
20 | | - const db = await getDatabase(); |
21 | | - const { documents } = await db.listDocuments(DATABASE_ID, COLLECTION_ID); |
22 | | - console.log("documents", documents); |
23 | | - setOwnedCards(documents as unknown as CollectionRow[]); |
24 | | - }; |
| 20 | + const db = await getDatabase() |
| 21 | + const { documents } = await db.listDocuments(DATABASE_ID, COLLECTION_ID) |
| 22 | + console.log('documents', documents) |
| 23 | + setOwnedCards(documents as unknown as CollectionRow[]) |
| 24 | + } |
25 | 25 |
|
26 | 26 | const updateCardCount = async (cardId: string, increment: number) => { |
27 | | - console.log(`${cardId} button clicked`); |
28 | | - const db = await getDatabase(); |
29 | | - const ownedCard = ownedCards.find((c) => c.card_id === cardId); |
| 27 | + console.log(`${cardId} button clicked`) |
| 28 | + const db = await getDatabase() |
| 29 | + const ownedCard = ownedCards.find((c) => c.card_id === cardId) |
30 | 30 |
|
31 | 31 | if (ownedCard) { |
32 | | - console.log("updating", ownedCard); |
33 | | - ownedCard.amount_owned = Math.max(0, ownedCard.amount_owned + increment); |
34 | | - setOwnedCards([...ownedCards]); |
| 32 | + console.log('updating', ownedCard) |
| 33 | + ownedCard.amount_owned = Math.max(0, ownedCard.amount_owned + increment) |
| 34 | + setOwnedCards([...ownedCards]) |
35 | 35 | await db.updateDocument(DATABASE_ID, COLLECTION_ID, ownedCard.$id, { |
36 | 36 | amount_owned: ownedCard.amount_owned, |
37 | | - }); |
38 | | - await fetchCollection(); |
| 37 | + }) |
| 38 | + await fetchCollection() |
39 | 39 | } else if (!ownedCard && increment > 0) { |
40 | | - console.log("adding new card", cardId); |
| 40 | + console.log('adding new card', cardId) |
41 | 41 | await db.createDocument(DATABASE_ID, COLLECTION_ID, ID.unique(), { |
42 | 42 | email: user.email, |
43 | 43 | card_id: cardId, |
44 | 44 | amount_owned: increment, |
45 | | - }); |
46 | | - await fetchCollection(); |
| 45 | + }) |
| 46 | + await fetchCollection() |
47 | 47 | } |
48 | | - }; |
| 48 | + } |
49 | 49 |
|
50 | | - const A1_Cards: CardType[] = A1 as unknown as CardType[]; |
| 50 | + const A1_Cards: CardType[] = A1 as unknown as CardType[] |
51 | 51 |
|
52 | 52 | const Card = ({ cardId }: { cardId: string }) => { |
53 | | - const Card_info = A1_Cards.find((c) => c.id === cardId); |
| 53 | + const Card_info = A1_Cards.find((c) => c.id === cardId) |
54 | 54 | return ( |
55 | 55 | <div className="flex flex-col items-center gap-y-4 w-fit border p-4 rounded-lg shadow-md hover:shadow-lg transition duration-200 group"> |
56 | | - <img |
57 | | - className="w-40 rounded-lg object-cover transform group-hover:scale-105 transition duration-200" |
58 | | - src={Card_info?.image} |
59 | | - alt={Card_info?.name} |
60 | | - /> |
| 56 | + <img className="w-40 rounded-lg object-cover transform group-hover:scale-105 transition duration-200" src={Card_info?.image} alt={Card_info?.name} /> |
61 | 57 | <div className="flex items-center gap-x-4 mt-2"> |
62 | 58 | <button |
63 | 59 | type="button" |
64 | 60 | onClick={() => updateCardCount(cardId, -1)} |
65 | 61 | className="flex items-center justify-center w-8 h-8 rounded-full bg-red-500 text-white hover:bg-red-400 transition duration-200 focus:outline-none" |
66 | 62 | > |
67 | | - <svg |
68 | | - className="w-4 h-4" |
69 | | - viewBox="0 0 20 20" |
70 | | - fill="currentColor" |
71 | | - aria-hidden="true" |
72 | | - > |
| 63 | + <svg className="w-4 h-4" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true"> |
73 | 64 | <path d="M5 10a.75.75 0 0 1 .75-.75h8.5a.75.75 0 0 1 0 1.5h-8.5A.75.75 0 0 1 5 10z" /> |
74 | 65 | </svg> |
75 | 66 | </button> |
76 | | - <span className="text-lg font-semibold"> |
77 | | - {ownedCards.find((c) => c.card_id === cardId)?.amount_owned || 0} |
78 | | - </span> |
| 67 | + <span className="text-lg font-semibold">{ownedCards.find((c) => c.card_id === cardId)?.amount_owned || 0}</span> |
79 | 68 | <button |
80 | 69 | type="button" |
81 | 70 | onClick={() => updateCardCount(cardId, 1)} |
82 | 71 | className="flex items-center justify-center w-8 h-8 rounded-full bg-green-500 text-white hover:bg-green-400 transition duration-200 focus:outline-none" |
83 | 72 | > |
84 | | - <svg |
85 | | - className="w-4 h-4" |
86 | | - viewBox="0 0 20 20" |
87 | | - fill="currentColor" |
88 | | - aria-hidden="true" |
89 | | - > |
| 73 | + <svg className="w-4 h-4" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true"> |
90 | 74 | <path d="M10.75 4.75a.75.75 0 0 0-1.5 0v4.5h-4.5a.75.75 0 0 0 0 1.5h4.5v4.5a.75.75 0 0 0 1.5 0v-4.5h4.5a.75.75 0 0 0 0-1.5h-4.5v-4.5Z" /> |
91 | 75 | </svg> |
92 | 76 | </button> |
93 | 77 | </div> |
94 | 78 | </div> |
95 | | - ); |
96 | | - }; |
| 79 | + ) |
| 80 | + } |
97 | 81 |
|
98 | 82 | return ( |
99 | 83 | <ul className="grid grid-cols-2 md:grid-cols-3 lg:grid-cols-4 gap-4 justify-center"> |
100 | | - {A1_Cards.map((cardId) => ( |
101 | | - <li className="mx-auto"> |
102 | | - <Card cardId={cardId.id} /> |
| 84 | + {A1_Cards.map((card) => ( |
| 85 | + <li key={card.id} className="mx-auto"> |
| 86 | + <Card cardId={card.id} /> |
103 | 87 | </li> |
104 | 88 | ))} |
105 | 89 | </ul> |
106 | | - ); |
107 | | -}; |
| 90 | + ) |
| 91 | +} |
0 commit comments