79 lines
2.2 KiB
TypeScript
79 lines
2.2 KiB
TypeScript
import { defineStore } from 'pinia';
|
|
import { ref } from 'vue';
|
|
|
|
// Interface pour la carte tirée
|
|
interface DrawnCard {
|
|
id: number;
|
|
name: string;
|
|
image_url: string;
|
|
orientation: 'upright' | 'reversed';
|
|
description: string;
|
|
description_upright?: string;
|
|
description_reversed?: string;
|
|
symbolism?: Record<string, string>;
|
|
created_at: string;
|
|
}
|
|
|
|
export const useTarotStore = defineStore(
|
|
'tarot',
|
|
() => {
|
|
// State
|
|
const freeDrawsRemaining = ref(1);
|
|
const paidDrawsRemaining = ref(0);
|
|
const freeDrawCard = ref<DrawnCard | null>(null);
|
|
|
|
// Actions
|
|
function useFreeDraw() {
|
|
if (freeDrawsRemaining.value > 0) {
|
|
freeDrawsRemaining.value--;
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
// Store the free draw card for persistence
|
|
function setFreeDrawCard(card: DrawnCard) {
|
|
freeDrawCard.value = card;
|
|
}
|
|
|
|
// Clear the free draw card (e.g., for a new session)
|
|
function clearFreeDrawCard() {
|
|
freeDrawCard.value = null;
|
|
freeDrawsRemaining.value = 1;
|
|
}
|
|
|
|
// Modified usePaidDraw to handle the correct number of cards and reset the state
|
|
function usePaidDraw(count: number) {
|
|
if (paidDrawsRemaining.value >= count) {
|
|
// Since the draws are 'used', we set the remaining to 0.
|
|
// This assumes a user pays for a set number of cards in one go.
|
|
paidDrawsRemaining.value = 0;
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
function addPaidDraws(count: number) {
|
|
paidDrawsRemaining.value += count;
|
|
}
|
|
|
|
return {
|
|
freeDrawsRemaining,
|
|
paidDrawsRemaining,
|
|
freeDrawCard,
|
|
useFreeDraw,
|
|
setFreeDrawCard,
|
|
clearFreeDrawCard,
|
|
usePaidDraw,
|
|
addPaidDraws,
|
|
};
|
|
},
|
|
{
|
|
persist: {
|
|
key: 'tarot-storage',
|
|
storage: typeof window !== 'undefined' ? localStorage : undefined,
|
|
pick: ['freeDrawsRemaining', 'freeDrawCard'],
|
|
},
|
|
}
|
|
);
|