-
Notifications
You must be signed in to change notification settings - Fork 214
Expand file tree
/
Copy pathuse-shopper-context.tsx
More file actions
140 lines (129 loc) · 3.81 KB
/
use-shopper-context.tsx
File metadata and controls
140 lines (129 loc) · 3.81 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
* Copyright (c) 2022, salesforce.com, 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 React from 'react'
import {
useShopperContext,
useShopperContextsMutation,
ShopperContextsMutation
} from '@salesforce/commerce-sdk-react'
import Json from '../components/Json'
const renderQueryHook = (name: string, {data, isLoading, error}: any) => {
if (isLoading) {
return (
<div key={name}>
<h1 id={name}>{name}</h1>
<hr />
<h2 style={{background: 'aqua'}}>Loading...</h2>
</div>
)
}
return (
<div key={name}>
<h2 id={name}>{name}</h2>
<h3>{data?.name}</h3>
<hr />
<h3 style={{color: error ? 'red' : 'inherit'}}>Returned data</h3>
<Json data={{isLoading, error, data}} />
</div>
)
}
const renderMutationHook = ({name, hook, body, parameters}: any) => {
return (
<div key={name}>
<h2 id={name}>{name}</h2>
<hr />
<button
onClick={() =>
hook.mutate({
body,
parameters
})
}
>
{name}
</button>
<br />
<br />
{hook.error?.message && <p style={{color: 'red'}}>Error: {hook.error?.message}</p>}
<div>
<Json data={hook} />
</div>
</div>
)
}
function UseShopperContext() {
const usid = localStorage.getItem('RefArchGlobal_usid') || ''
const mutationHooks = [
{
action: 'createShopperContext',
body: {
effectiveDateTime: '2020-12-20T00:00:00Z',
customQualifiers: {
deviceType: 'mobile'
},
assignmentQualifiers: {
store: 'boston'
}
},
parameters: {usid}
},
{
action: 'updateShopperContext',
body: {
effectiveDateTime: new Date().toISOString().replace(/\.\d\d\dZ/, 'Z'),
customQualifiers: {
deviceType: 'desktop'
},
assignmentQualifiers: {
store: 'vancouver'
}
},
parameters: {usid}
},
{
action: 'deleteShopperContext',
body: {},
parameters: {usid}
}
].map(({action, body, parameters}) => {
return {
name: action,
// This is essentially a shorthand to avoid writing out a giant object;
// it *technically* violates the rules of hooks, but not in an impactful way.
// eslint-disable-next-line react-hooks/rules-of-hooks
hook: useShopperContextsMutation(action as ShopperContextsMutation),
body,
parameters
}
})
const queryHooks = [
{
name: 'useShopperContext',
hook: useShopperContext({
parameters: {usid}
})
}
]
return (
<>
<div>
<h1>Query hooks</h1>
{queryHooks.map(({name, hook}) => {
return renderQueryHook(name, {...hook})
})}
</div>
<div>
<h1>Mutation hooks</h1>
{mutationHooks.map((mutation) => {
return renderMutationHook({...mutation})
})}
</div>
</>
)
}
UseShopperContext.getTemplateName = () => 'UseShopperContext'
export default UseShopperContext