27 lines
777 B
TypeScript
27 lines
777 B
TypeScript
import { defineStore } from "pinia";
|
|
import type { Card } from "~/types/Card";
|
|
|
|
export const useCardStore = defineStore("cardStore", {
|
|
state: () => ({
|
|
collections: [] as string[],
|
|
cards: [] as Card[],
|
|
}),
|
|
actions: {
|
|
async fetchCollections() {
|
|
const { data: collections } =
|
|
await useFetch<string[]>("/api/collections");
|
|
this.collections = collections.value ?? [];
|
|
},
|
|
async fetchCardsByCollection(collectionName: string) {
|
|
const response = useFetch<Card[]>(`/api/cards/${collectionName}`);
|
|
this.cards = response.data.value ?? [];
|
|
},
|
|
removeCard(id: number) {
|
|
const index = this.cards.findIndex((card) => card.id === id);
|
|
if (index !== -1) {
|
|
this.cards.splice(index, 1);
|
|
}
|
|
},
|
|
},
|
|
});
|