-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathuse-current-customer.js
More file actions
33 lines (31 loc) · 999 Bytes
/
use-current-customer.js
File metadata and controls
33 lines (31 loc) · 999 Bytes
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
/*
* Copyright (c) 2022, 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 {useCustomer, useCustomerId, useCustomerType} from '@salesforce/commerce-sdk-react'
/**
* A hook that returns the current customer.
* @param {Array<string>} [expand] - Optional array of fields to expand in the customer query
*/
export const useCurrentCustomer = (expand) => {
const customerId = useCustomerId()
const {isRegistered, isGuest, customerType} = useCustomerType()
const parameters = {
customerId,
...(expand && {expand})
}
const query = useCustomer({parameters}, {enabled: !!customerId && isRegistered})
const value = {
...query,
data: {
...query.data,
customerType,
customerId,
isRegistered,
isGuest
}
}
return value
}