Skip to content
This repository was archived by the owner on Dec 22, 2022. It is now read-only.

Commit a42d20b

Browse files
authored
Merge pull request #6 from joshwcorbett/devin
2 parents 2da1585 + ddc9d47 commit a42d20b

File tree

3 files changed

+147
-56
lines changed

3 files changed

+147
-56
lines changed

api/cart/mutations/index.ts

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
1-
import { cartLinesAdd } from './cartLinesAdd'
2-
import { cartLinesRemove } from './cartLinesRemove'
3-
import { cartCreate } from './cartCreate'
4-
import { cartLinesUpdate } from './cartLinesUpdate'
5-
6-
export {
7-
cartLinesAdd,
8-
cartLinesRemove,
9-
cartCreate,
10-
cartLinesUpdate
11-
}
1+
export { cartLinesAdd } from './cartLinesAdd'
2+
export { cartLinesRemove } from './cartLinesRemove'
3+
export { cartCreate } from './cartCreate'
4+
export { cartLinesUpdate } from './cartLinesUpdate'

stores/cart.ts

Lines changed: 90 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,110 @@
11
import { defineStore } from 'pinia'
22
import { useClient } from '@/utilities/apollo-client'
3-
import { CartLineInput, CartLineUpdateInput } from '@/types'
3+
import { CartLineInput, Cart, CurrencyCode, CartLineUpdateInput } from '@/types'
44
import { formatLocalePrice } from '@/utilities/money'
55
import {
66
cartCreate,
77
cartLinesAdd,
88
cartLinesRemove,
9-
cartLinesUpdate
9+
cartLinesUpdate,
1010
} from '@/api/cart/mutations'
1111

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', {
1344
state: () => ({
1445
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: [],
1658
estimatedCost: {
1759
subtotalAmount: {
18-
amount: "",
19-
currencyCode: "USD",
60+
amount: 0,
61+
currencyCode: CurrencyCode.USD,
2062
},
2163
totalTaxAmount: {
22-
amount: "",
23-
currencyCode: "USD",
64+
amount: 0,
65+
currencyCode: CurrencyCode.USD,
2466
},
2567
totalDutyAmount: {
26-
amount: "",
27-
currencyCode: "USD",
68+
amount: 0,
69+
currencyCode: CurrencyCode.USD,
2870
},
2971
totalAmount: {
30-
amount: "",
31-
currencyCode: "USD",
72+
amount: 0,
73+
currencyCode: CurrencyCode.USD,
3274
},
3375
},
34-
id: null,
76+
id: '',
3577
lines: {
3678
edges: [],
79+
pageInfo: {
80+
hasPreviousPage: false,
81+
hasNextPage: false,
82+
},
3783
},
38-
discountCodes: [],
84+
updatedAt: '',
3985
},
4086
loading: false,
4187
}),
4288
actions: {
43-
async cartCreate () {
89+
async cartCreate() {
4490
try {
4591
this.loading = true
46-
const { data } = await useClient().mutate({
47-
mutation: cartCreate
92+
const { data } = await useClient().mutate<CartMutCreate>({
93+
mutation: cartCreate,
4894
})
4995
if (!data.cartCreate.cart.id) {
50-
throw "cartCreate: error"
96+
throw 'cartCreate: error'
5197
}
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
54100
} catch (e) {
55101
return e
56102
} finally {
57103
this.loading = false
104+
return this
58105
}
59106
},
60-
async getCart () {
61-
107+
async getCart() {
62108
if (!this.cart.id) {
63109
await this.cartCreate()
64110
}
@@ -69,40 +115,42 @@ export const useCart = defineStore('cart', {
69115
return e
70116
} finally {
71117
this.loading = false
118+
return this
72119
}
73120
},
74-
async addToCart (lines: CartLineInput[]) {
121+
async addToCart(lines) {
75122
try {
76123
this.loading = true
77124
if (!this.cart.id) {
78125
await this.cartCreate()
79126
}
80-
const { data } = await useClient().mutate({
127+
const { data } = await useClient().mutate<CartMutAdd>({
81128
mutation: cartLinesAdd,
82129
variables: {
83130
cartId: this.cart.id,
84131
lines,
85-
}
132+
},
86133
})
87134
if (!data.cartLinesAdd) {
88135
throw 'cartLinesAdd: error'
89136
}
90-
this.cart = data?.cartLinesAdd?.cart
137+
this.cart = data.cartLinesAdd.cart
91138
} catch (e) {
92139
return e
93140
} finally {
94141
this.loading = false
142+
return this
95143
}
96144
},
97-
async removeFromCart (lines: string[]) {
145+
async removeFromCart(lines) {
98146
try {
99147
this.loading = true
100-
const { data } = await useClient().mutate({
148+
const { data } = await useClient().mutate<CartMutRemove>({
101149
mutation: cartLinesRemove,
102150
variables: {
103151
cartId: this.cart.id,
104152
lineIds: lines,
105-
}
153+
},
106154
})
107155
if (!data.cartLinesRemove) {
108156
throw 'cartLinesRemove: error'
@@ -112,9 +160,10 @@ export const useCart = defineStore('cart', {
112160
return e
113161
} finally {
114162
this.loading = false
163+
return this
115164
}
116165
},
117-
async updateCartItem (lines: CartLineUpdateInput[]) {
166+
async updateCartItem(lines: CartLineUpdateInput[]) {
118167
try {
119168
this.loading = true
120169
if (!this.cart.id) {
@@ -125,46 +174,47 @@ export const useCart = defineStore('cart', {
125174
variables: {
126175
cartId: this.cart.id,
127176
lines,
128-
}
177+
},
129178
})
130179
this.cart = data?.cartLinesUpdate?.cart
131180
} catch (e) {
132181
return e
133182
} finally {
134183
this.loading = false
184+
return this
135185
}
136-
}
186+
},
137187
},
138188
getters: {
139-
lineItems: (state) => state.cart?.lines?.edges,
189+
lineItems: (state) => state.cart.lines.edges,
140190
lineItemsCount() {
141191
return this.lineItems.length
142192
},
143193
subtotalAmount: (state): string => {
144194
const amount = +state.cart.estimatedCost.subtotalAmount.amount
145195
const code = state.cart.estimatedCost.subtotalAmount.currencyCode
146-
return formatLocalePrice(+amount, "en-US", code)
196+
return formatLocalePrice(+amount, 'en-US', code)
147197
},
148198
totalDutyAmount: (state): string => {
149-
if (!state?.cart?.estimatedCost?.totalDutyAmount) {
199+
if (!state.cart.estimatedCost.totalDutyAmount) {
150200
return null
151201
}
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)
155205
},
156206
totalTaxAmount: (state): string => {
157207
if (!state?.cart?.estimatedCost?.totalTaxAmount) {
158208
return null
159209
}
160210
const amount = +state.cart.estimatedCost.totalTaxAmount.amount
161211
const code = state.cart.estimatedCost.totalTaxAmount.currencyCode
162-
return formatLocalePrice(+amount, "en-US", code)
212+
return formatLocalePrice(+amount, 'en-US', code)
163213
},
164214
totalAmount: (state): string => {
165215
const amount = +state.cart.estimatedCost.totalAmount.amount
166216
const code = state.cart.estimatedCost.totalAmount.currencyCode
167-
return formatLocalePrice(+amount, "en-US", code)
217+
return formatLocalePrice(+amount, 'en-US', code)
168218
},
169219
},
170220
persist: true,

types.ts

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,15 @@ export enum UnitSystem {
596596
IMPERIAL_SYSTEM = 'IMPERIAL_SYSTEM',
597597
METRIC_SYSTEM = 'METRIC_SYSTEM',
598598
}
599+
600+
export enum DeliveryMethodType {
601+
LOCAL = 'LOCAL',
602+
NONE = 'NONE',
603+
PICKUP_POINT = 'PICKUP_POINT',
604+
PICK_UP = 'PICK_UP',
605+
RETAIL = 'RETAIL',
606+
SHIPPING = 'SHIPPING',
607+
}
599608
// Globals
600609
export interface Language {
601610
endonymName: string
@@ -620,9 +629,14 @@ export interface QueryRoot {
620629
collections: CollectionConnection
621630
cart: Cart
622631
}
632+
633+
export interface Connection<T> {
634+
edges: T[]
635+
pageInfo: PageInfo
636+
}
623637
export interface MoneyV2 {
624638
amount: number
625-
currencyCode: CurrencyCode
639+
currencyCode: keyof typeof CurrencyCode
626640
}
627641
export interface SEO {
628642
title: string
@@ -838,15 +852,38 @@ export interface CollectionConnection {
838852
pageInfo: PageInfo
839853
}
840854
// Cart
855+
856+
export interface CartDiscountCode {
857+
applicable: boolean
858+
code: string
859+
}
860+
export interface CartDeliveryOption {
861+
code?: string
862+
deliveryMethodType: keyof typeof DeliveryMethodType
863+
description?: string
864+
estimatedCost: MoneyV2
865+
title?: string
866+
}
867+
868+
export interface CartDeliveryGroup {
869+
deliveryAddress: MailingAddress
870+
deliveryOptions: Array<CartDeliveryOption>
871+
id: string
872+
}
841873
export interface Cart {
842-
lines: CartLineConnection
874+
attributes: Array<Attribute>
875+
buyerIdentity: CartBuyerIdentity
876+
lines: Connection<CartLine>
843877
checkoutUrl: string
844878
createdAt: string
845879
estimatedCost: CartEstimatedCost
846880
id: string
847-
note: string
881+
note?: string
848882
updatedAt: string
883+
discountCodes: Array<CartDiscountCode>
884+
deliveryGroups: Connection<CartDeliveryGroup>
849885
}
886+
850887
export interface CartLineConnection {
851888
edges: CartLineEdge[]
852889
}
@@ -861,6 +898,10 @@ export interface CartLineEdge {
861898
node: CartLine
862899
}
863900

901+
export interface Attribute {
902+
key: string
903+
value?: string
904+
}
864905
export interface AttributeInput {
865906
key: string
866907
value: string
@@ -880,7 +921,7 @@ export interface CartLineUpdateInput {
880921
}
881922
export type Merchandise = ProductVariant<'thumbnail'>
882923
export interface CartEstimatedCost {
883-
subTotalAmount: MoneyV2
924+
subtotalAmount: MoneyV2
884925
totalAmount: MoneyV2
885926
totalDutyAmount: MoneyV2
886927
totalTaxAmount: MoneyV2
@@ -1002,11 +1043,18 @@ export interface CustomerAccessTokenDeletePayload {
10021043
userErrors: UserError[]
10031044
}
10041045
export interface CartBuyerIdentityInput {
1005-
countryCode?: string
1046+
countryCode?: keyof typeof CountryCode
10061047
customerAccessToken?: string
10071048
email?: string
10081049
phone?: string
10091050
}
1051+
1052+
export interface CartBuyerIdentity {
1053+
countryCode?: keyof typeof CountryCode
1054+
customer?: Customer
1055+
email?: string
1056+
phone?: string
1057+
}
10101058
export interface CartBuyerIdentityPayload {
10111059
cart: Cart
10121060
userErrors: UserError[]

0 commit comments

Comments
 (0)