sop-nuxt/store/Card.ts
2025-07-13 18:13:22 +02:00

41 lines
1.0 KiB
TypeScript

import { defineStore } from "pinia";
import type { Card } from "~/types/Card";
import type { Collection } from "~/types/Collection";
export const useCardStore = defineStore("cardStore", {
state: () => ({
collections: [] as Collection[],
cards: [] as Card[],
smashList: [] as Card[],
passList: [] as Card[],
}),
actions: {
async fetchCollections() {
const data = await $fetch<Collection[]>("/api/collections/", {
baseURL: useApiBase(),
});
this.collections = data ?? [];
},
async fetchCardsByCollection(collectionName: string) {
const data = await $fetch<Card[]>(
`/api/cards/?collection__name=${collectionName}`,
{
baseURL: useApiBase(),
},
);
this.cards =
data.map((card, index) => ({
...card,
id: index,
})) ?? [];
},
removeCard(id: number) {
const index = this.cards.findIndex((card) => card.id === id);
if (index !== -1) {
this.cards.splice(index, 1);
}
},
},
});