-
Notifications
You must be signed in to change notification settings - Fork 214
Expand file tree
/
Copy pathhelpers.ts
More file actions
76 lines (73 loc) · 3.1 KB
/
helpers.ts
File metadata and controls
76 lines (73 loc) · 3.1 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
68
69
70
71
72
73
74
75
76
/*
* Copyright (c) 2023, Salesforce, Inc.
* All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import {useCustomerId, useShopperBasketsMutation} from '../index'
import {useCustomerBaskets} from '../ShopperCustomers'
import {ApiClients, Argument} from '../types'
import {ShopperBasketsTypes} from 'commerce-sdk-isomorphic'
import {CLIENT_KEYS} from '../../constant'
const CLIENT_KEY = CLIENT_KEYS.SHOPPER_BASKETS
type Client = NonNullable<ApiClients[typeof CLIENT_KEY]>
type Basket = ShopperBasketsTypes.Basket
/**
* This is a helper function for Basket Mutations.
* useShopperBasketsMutationHelper.addItemToNewOrExistingBasket: is responsible for managing the process of adding an item to a basket.
* - If a basket already exists, add the item to the basket immediately.
* - If a basket does not exist, create a new basket using the createBasket mutation
* and then add the item to the newly created basket using the addItemToBasket mutation.
*
* @example
* import useShopperBasketsMutationHelper from '@salesforce/commerce-sdk-react'
*
* const Page = () => {
* const helpers = useShopperBasketsMutationHelper()
*
* const addToCart = async () => {
* const productItems = [{id: 123, quantity: 2}]
* await basketMutationHelpers.addItemToNewOrExistingBasket(productItems)
* }
*
* }
*/
export function useShopperBasketsMutationHelper() {
const customerId = useCustomerId()
const {data: basketsData} = useCustomerBaskets(
{parameters: {customerId}},
{
enabled: !!customerId
}
)
const createBasket = useShopperBasketsMutation('createBasket')
const addItemToBasketMutation = useShopperBasketsMutation('addItemToBasket')
return {
addItemToNewOrExistingBasket: async (
productItem: Argument<Client['addItemToBasket']> extends {body: infer B} ? B : undefined
): Promise<Basket> => {
if (basketsData && basketsData.total > 0) {
// we know that if basketData.total > 0, current basket will always be available
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const currentBasket = basketsData?.baskets && basketsData.baskets[0]!
return await addItemToBasketMutation.mutateAsync({
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
parameters: {basketId: currentBasket!.basketId!},
body: productItem
})
} else {
const data = await createBasket.mutateAsync({
body: {}
})
if (!data || !data.basketId) {
throw Error('Something is wrong. Please try again')
} else {
return await addItemToBasketMutation.mutateAsync({
parameters: {basketId: data.basketId},
body: productItem
})
}
}
}
}
}