payment-cards.ts
1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// src/stores/cards.ts
import { defineStore } from 'pinia'
import { sleep } from '../services/utils'
import { PaymentSystemType, PaymentCard } from '../pages/payments/types' // adjust the import path accordingly
// Simulated fetch function
const fetchPaymentCards = async () => {
await sleep(1000)
return [
{
id: '1',
name: 'Main card',
isPrimary: true,
paymentSystem: PaymentSystemType.Visa,
cardNumberMasked: '****1679',
expirationDate: '0924',
},
{
id: '2',
name: 'Online shopping',
isPrimary: false,
paymentSystem: PaymentSystemType.MasterCard,
cardNumberMasked: '****8921',
expirationDate: '1123',
},
{
id: '3',
name: 'Backup Visa',
isPrimary: false,
paymentSystem: PaymentSystemType.MasterCard,
cardNumberMasked: '****4523',
expirationDate: '1222',
},
]
}
export const usePaymentCardsStore = defineStore({
id: 'paymentCards',
state: () => ({
paymentCards: [] as PaymentCard[],
loading: false,
}),
getters: {
currentPaymentCard: (state): PaymentCard | undefined => state.paymentCards.find((card) => card.isPrimary),
allPaymentCards: (state) => state.paymentCards,
},
actions: {
async load() {
this.loading = true
this.paymentCards = await fetchPaymentCards()
this.loading = false
},
create(card: PaymentCard) {
this.paymentCards.unshift(card)
},
update(card: PaymentCard) {
const index = this.paymentCards.findIndex((existingCard) => existingCard.id === card.id)
if (index !== -1) {
this.paymentCards.splice(index, 1, card)
}
},
remove(cardId: string) {
this.paymentCards = this.paymentCards.filter((card) => card.id !== cardId)
},
},
})