1
1
import { defineStore } from 'pinia'
2
2
import { useClient } from '@/utilities/apollo-client'
3
- import { CartLineInput , CartLineUpdateInput } from '@/types'
3
+ import { CartLineInput , Cart , CurrencyCode , CartLineUpdateInput } from '@/types'
4
4
import { formatLocalePrice } from '@/utilities/money'
5
5
import {
6
6
cartCreate ,
7
7
cartLinesAdd ,
8
8
cartLinesRemove ,
9
- cartLinesUpdate
9
+ cartLinesUpdate ,
10
10
} from '@/api/cart/mutations'
11
11
12
- export const useCart = defineStore ( 'cart' , {
12
+ type CartMutCreate = {
13
+ cartCreate : {
14
+ cart : Cart
15
+ }
16
+ }
17
+
18
+ type CartMutAdd = {
19
+ cartLinesAdd : {
20
+ cart : Cart
21
+ }
22
+ }
23
+
24
+ type CartMutRemove = {
25
+ cartLinesRemove : {
26
+ cart : Cart
27
+ }
28
+ }
29
+
30
+ interface CartState {
31
+ cart : Cart
32
+ loading : boolean
33
+ }
34
+
35
+ interface CartActions {
36
+ cartCreate ( ) : Promise < CartState >
37
+ getCart ( ) : Promise < CartState >
38
+ addToCart ( lines : CartLineInput [ ] ) : Promise < CartState >
39
+ removeFromCart ( lines : string [ ] ) : Promise < CartState >
40
+ updateCartItem ( lines : CartLineUpdateInput [ ] ) : Promise < CartState >
41
+ }
42
+
43
+ export const useCart = defineStore < 'cart' , CartState , { } , CartActions > ( 'cart' , {
13
44
state : ( ) => ( {
14
45
cart : {
15
- checkoutUrl : null ,
46
+ attributes : [ ] ,
47
+ buyerIdentity : { } ,
48
+ createdAt : '' ,
49
+ checkoutUrl : '' ,
50
+ deliveryGroups : {
51
+ edges : [ ] ,
52
+ pageInfo : {
53
+ hasPreviousPage : false ,
54
+ hasNextPage : false ,
55
+ } ,
56
+ } ,
57
+ discountCodes : [ ] ,
16
58
estimatedCost : {
17
59
subtotalAmount : {
18
- amount : "" ,
19
- currencyCode : " USD" ,
60
+ amount : 0 ,
61
+ currencyCode : CurrencyCode . USD ,
20
62
} ,
21
63
totalTaxAmount : {
22
- amount : "" ,
23
- currencyCode : " USD" ,
64
+ amount : 0 ,
65
+ currencyCode : CurrencyCode . USD ,
24
66
} ,
25
67
totalDutyAmount : {
26
- amount : "" ,
27
- currencyCode : " USD" ,
68
+ amount : 0 ,
69
+ currencyCode : CurrencyCode . USD ,
28
70
} ,
29
71
totalAmount : {
30
- amount : "" ,
31
- currencyCode : " USD" ,
72
+ amount : 0 ,
73
+ currencyCode : CurrencyCode . USD ,
32
74
} ,
33
75
} ,
34
- id : null ,
76
+ id : '' ,
35
77
lines : {
36
78
edges : [ ] ,
79
+ pageInfo : {
80
+ hasPreviousPage : false ,
81
+ hasNextPage : false ,
82
+ } ,
37
83
} ,
38
- discountCodes : [ ] ,
84
+ updatedAt : '' ,
39
85
} ,
40
86
loading : false ,
41
87
} ) ,
42
88
actions : {
43
- async cartCreate ( ) {
89
+ async cartCreate ( ) {
44
90
try {
45
91
this . loading = true
46
- const { data } = await useClient ( ) . mutate ( {
47
- mutation : cartCreate
92
+ const { data } = await useClient ( ) . mutate < CartMutCreate > ( {
93
+ mutation : cartCreate ,
48
94
} )
49
95
if ( ! data . cartCreate . cart . id ) {
50
- throw " cartCreate: error"
96
+ throw ' cartCreate: error'
51
97
}
52
- this . cart . id = data ? .cartCreate . cart ?. id ?? null
53
- this . cart . checkoutUrl = data ?. cartCreate . cart ?. checkoutUrl ?? null
98
+ const { cart } = data . cartCreate
99
+ this . cart = cart
54
100
} catch ( e ) {
55
101
return e
56
102
} finally {
57
103
this . loading = false
104
+ return this
58
105
}
59
106
} ,
60
- async getCart ( ) {
61
-
107
+ async getCart ( ) {
62
108
if ( ! this . cart . id ) {
63
109
await this . cartCreate ( )
64
110
}
@@ -69,40 +115,42 @@ export const useCart = defineStore('cart', {
69
115
return e
70
116
} finally {
71
117
this . loading = false
118
+ return this
72
119
}
73
120
} ,
74
- async addToCart ( lines : CartLineInput [ ] ) {
121
+ async addToCart ( lines ) {
75
122
try {
76
123
this . loading = true
77
124
if ( ! this . cart . id ) {
78
125
await this . cartCreate ( )
79
126
}
80
- const { data } = await useClient ( ) . mutate ( {
127
+ const { data } = await useClient ( ) . mutate < CartMutAdd > ( {
81
128
mutation : cartLinesAdd ,
82
129
variables : {
83
130
cartId : this . cart . id ,
84
131
lines,
85
- }
132
+ } ,
86
133
} )
87
134
if ( ! data . cartLinesAdd ) {
88
135
throw 'cartLinesAdd: error'
89
136
}
90
- this . cart = data ? .cartLinesAdd ? .cart
137
+ this . cart = data . cartLinesAdd . cart
91
138
} catch ( e ) {
92
139
return e
93
140
} finally {
94
141
this . loading = false
142
+ return this
95
143
}
96
144
} ,
97
- async removeFromCart ( lines : string [ ] ) {
145
+ async removeFromCart ( lines ) {
98
146
try {
99
147
this . loading = true
100
- const { data } = await useClient ( ) . mutate ( {
148
+ const { data } = await useClient ( ) . mutate < CartMutRemove > ( {
101
149
mutation : cartLinesRemove ,
102
150
variables : {
103
151
cartId : this . cart . id ,
104
152
lineIds : lines ,
105
- }
153
+ } ,
106
154
} )
107
155
if ( ! data . cartLinesRemove ) {
108
156
throw 'cartLinesRemove: error'
@@ -112,9 +160,10 @@ export const useCart = defineStore('cart', {
112
160
return e
113
161
} finally {
114
162
this . loading = false
163
+ return this
115
164
}
116
165
} ,
117
- async updateCartItem ( lines : CartLineUpdateInput [ ] ) {
166
+ async updateCartItem ( lines : CartLineUpdateInput [ ] ) {
118
167
try {
119
168
this . loading = true
120
169
if ( ! this . cart . id ) {
@@ -125,46 +174,47 @@ export const useCart = defineStore('cart', {
125
174
variables : {
126
175
cartId : this . cart . id ,
127
176
lines,
128
- }
177
+ } ,
129
178
} )
130
179
this . cart = data ?. cartLinesUpdate ?. cart
131
180
} catch ( e ) {
132
181
return e
133
182
} finally {
134
183
this . loading = false
184
+ return this
135
185
}
136
- }
186
+ } ,
137
187
} ,
138
188
getters : {
139
- lineItems : ( state ) => state . cart ? .lines ? .edges ,
189
+ lineItems : ( state ) => state . cart . lines . edges ,
140
190
lineItemsCount ( ) {
141
191
return this . lineItems . length
142
192
} ,
143
193
subtotalAmount : ( state ) : string => {
144
194
const amount = + state . cart . estimatedCost . subtotalAmount . amount
145
195
const code = state . cart . estimatedCost . subtotalAmount . currencyCode
146
- return formatLocalePrice ( + amount , " en-US" , code )
196
+ return formatLocalePrice ( + amount , ' en-US' , code )
147
197
} ,
148
198
totalDutyAmount : ( state ) : string => {
149
- if ( ! state ? .cart ? .estimatedCost ? .totalDutyAmount ) {
199
+ if ( ! state . cart . estimatedCost . totalDutyAmount ) {
150
200
return null
151
201
}
152
- const amount = + state ? .cart ? .estimatedCost ? .totalDutyAmount ? .amount
153
- const code = state ? .cart ? .estimatedCost ? .totalDutyAmount ? .currencyCode
154
- return formatLocalePrice ( + amount , " en-US" , code )
202
+ const amount = + state . cart . estimatedCost . totalDutyAmount . amount
203
+ const code = state . cart . estimatedCost . totalDutyAmount . currencyCode
204
+ return formatLocalePrice ( + amount , ' en-US' , code )
155
205
} ,
156
206
totalTaxAmount : ( state ) : string => {
157
207
if ( ! state ?. cart ?. estimatedCost ?. totalTaxAmount ) {
158
208
return null
159
209
}
160
210
const amount = + state . cart . estimatedCost . totalTaxAmount . amount
161
211
const code = state . cart . estimatedCost . totalTaxAmount . currencyCode
162
- return formatLocalePrice ( + amount , " en-US" , code )
212
+ return formatLocalePrice ( + amount , ' en-US' , code )
163
213
} ,
164
214
totalAmount : ( state ) : string => {
165
215
const amount = + state . cart . estimatedCost . totalAmount . amount
166
216
const code = state . cart . estimatedCost . totalAmount . currencyCode
167
- return formatLocalePrice ( + amount , " en-US" , code )
217
+ return formatLocalePrice ( + amount , ' en-US' , code )
168
218
} ,
169
219
} ,
170
220
persist : true ,
0 commit comments