@hanzo/commerce 1.1.1 → 1.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
export * from './types'
|
|
2
2
|
export * from './service/context'
|
|
3
3
|
export type { StandaloneServiceOptions as ServiceOptions } from './service/impls/standalone/standalone-service'
|
|
4
|
-
export {
|
|
4
|
+
export { useSyncSkuParamWithCurrentItem } from './util'
|
package/package.json
CHANGED
|
@@ -207,13 +207,15 @@ class StandaloneService
|
|
|
207
207
|
const keysStr = Object.keys(this._facetsDesc)
|
|
208
208
|
const keysNum = keysStr.map((key) => (parseInt(key)))
|
|
209
209
|
keysNum.forEach((key) => {
|
|
210
|
+
if (partial[key]) {
|
|
211
|
+
// If present, filter out the bad values (the one's that don't exist in the Desc)
|
|
212
|
+
const filtered = partial[key].filter((fv) => (this._facetsDesc[key].find((fvDesc) => (fvDesc.value === fv))))
|
|
213
|
+
result[key] = filtered
|
|
214
|
+
}
|
|
210
215
|
// if not present, assume the facet is "off" and allow all (include all in the set).
|
|
211
|
-
|
|
216
|
+
else {
|
|
212
217
|
result[key] = this._facetsDesc[key].map((fv) => (fv.value))
|
|
213
218
|
}
|
|
214
|
-
// If present, filter out the bad values if any
|
|
215
|
-
const filtered = partial[key].filter((fv) => this._facetsDesc[key].find((fvDesc) => (fvDesc.value === fv)))
|
|
216
|
-
result[key] = filtered
|
|
217
219
|
})
|
|
218
220
|
return result
|
|
219
221
|
}
|
package/util/index.ts
CHANGED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
import { useEffect } from 'react'
|
|
3
|
+
import { reaction } from 'mobx'
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
useQueryState,
|
|
7
|
+
parseAsString,
|
|
8
|
+
parseAsBoolean,
|
|
9
|
+
} from 'next-usequerystate'
|
|
10
|
+
|
|
11
|
+
import { useCommerce } from '../service/context'
|
|
12
|
+
import type { FacetsValue } from '../types'
|
|
13
|
+
|
|
14
|
+
const PLEASE_SELECT_FACETS = 'Please select an option from each group above.'
|
|
15
|
+
|
|
16
|
+
const useSyncSkuParamWithCurrentItem = (
|
|
17
|
+
categoryLevel: number,
|
|
18
|
+
setMessage: (m: string | undefined) => void,
|
|
19
|
+
setLoading?: (l: boolean) => void
|
|
20
|
+
) => {
|
|
21
|
+
|
|
22
|
+
const cmmc = useCommerce()
|
|
23
|
+
|
|
24
|
+
const [skuParam, setSkuParam] = useQueryState('sku',
|
|
25
|
+
parseAsString.withDefault('').withOptions({ clearOnDefault: true })
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
const [addParam, setAddParam] = useQueryState('add',
|
|
29
|
+
parseAsBoolean.withDefault(false).withOptions({ clearOnDefault: true })
|
|
30
|
+
)
|
|
31
|
+
|
|
32
|
+
useEffect(() => {
|
|
33
|
+
|
|
34
|
+
return reaction(() => ({
|
|
35
|
+
specifiedCat: cmmc.specifiedCategories.length === 1 ? cmmc.specifiedCategories[0] : undefined,
|
|
36
|
+
currentItem: cmmc.currentItem
|
|
37
|
+
}),
|
|
38
|
+
({specifiedCat, currentItem}) => {
|
|
39
|
+
//console.log("REACTION: " + `CAT ID: ${category?.id} SKU: ${item?.sku}`)
|
|
40
|
+
if (currentItem) {
|
|
41
|
+
// if we set the currentItem w sku, then user selects other facets
|
|
42
|
+
if (specifiedCat && currentItem.categoryId != specifiedCat.id ) {
|
|
43
|
+
cmmc.setCurrentItem(specifiedCat.products[0].sku)
|
|
44
|
+
}
|
|
45
|
+
setSkuParam(cmmc.currentItem!.sku)
|
|
46
|
+
}
|
|
47
|
+
else if (specifiedCat) {
|
|
48
|
+
cmmc.setCurrentItem(specifiedCat.products[0].sku)
|
|
49
|
+
setSkuParam(cmmc.currentItem!.sku)
|
|
50
|
+
}
|
|
51
|
+
})
|
|
52
|
+
}, [])
|
|
53
|
+
|
|
54
|
+
useEffect(() => {
|
|
55
|
+
|
|
56
|
+
const setCurrentCategoryFromSku = (sku: string) => {
|
|
57
|
+
const toks: string[] = sku.split('-')
|
|
58
|
+
const fv: FacetsValue = {}
|
|
59
|
+
for (let i = 1; i < categoryLevel; i++) {
|
|
60
|
+
fv[i] = [toks[i]]
|
|
61
|
+
}
|
|
62
|
+
cmmc.setFacets(fv)
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// setCI returns true if it's a recognized sku
|
|
66
|
+
if (skuParam && cmmc.setCurrentItem(skuParam)) {
|
|
67
|
+
if (addParam) {
|
|
68
|
+
if (cmmc.currentItem!.quantity === 0) {
|
|
69
|
+
cmmc.currentItem!.increment()
|
|
70
|
+
}
|
|
71
|
+
setAddParam(false)
|
|
72
|
+
}
|
|
73
|
+
setCurrentCategoryFromSku(skuParam)
|
|
74
|
+
setMessage(undefined)
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
setMessage(PLEASE_SELECT_FACETS)
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
setLoading && setLoading(false)
|
|
81
|
+
}, [skuParam])
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export default useSyncSkuParamWithCurrentItem
|
|
@@ -1,170 +0,0 @@
|
|
|
1
|
-
"use client"
|
|
2
|
-
import {
|
|
3
|
-
useEffect,
|
|
4
|
-
useRef,
|
|
5
|
-
useState,
|
|
6
|
-
} from 'react'
|
|
7
|
-
import { autorun, toJS, type IReactionDisposer } from 'mobx'
|
|
8
|
-
|
|
9
|
-
import {
|
|
10
|
-
useQueryState,
|
|
11
|
-
parseAsString,
|
|
12
|
-
parseAsBoolean,
|
|
13
|
-
} from 'next-usequerystate'
|
|
14
|
-
|
|
15
|
-
import type { FacetsValue, StringMutator } from '../types'
|
|
16
|
-
import { useCommerce } from '../service/context'
|
|
17
|
-
|
|
18
|
-
const PLEASE_SELECT_FACETS = 'Please select an option from each group above.'
|
|
19
|
-
|
|
20
|
-
interface GetMutator {
|
|
21
|
-
(level: 1 | 2): StringMutator
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
const useSkuAndFacetParams = (
|
|
25
|
-
setMessage: (m: string | undefined) => void,
|
|
26
|
-
setLoading?: (l: boolean) => void
|
|
27
|
-
) => {
|
|
28
|
-
|
|
29
|
-
const cmmc = useCommerce()
|
|
30
|
-
|
|
31
|
-
const encRef = useRef<{
|
|
32
|
-
usingSkuMode: boolean
|
|
33
|
-
autoRunDisposer: IReactionDisposer | undefined
|
|
34
|
-
}>({
|
|
35
|
-
usingSkuMode: false,
|
|
36
|
-
autoRunDisposer: undefined,
|
|
37
|
-
})
|
|
38
|
-
|
|
39
|
-
const [level1, setLevel1] = useQueryState('lev1',
|
|
40
|
-
parseAsString.withDefault('').withOptions({ clearOnDefault: true })
|
|
41
|
-
)
|
|
42
|
-
const [level2, setLevel2] = useQueryState('lev2',
|
|
43
|
-
parseAsString.withDefault('').withOptions({ clearOnDefault: true })
|
|
44
|
-
)
|
|
45
|
-
|
|
46
|
-
const [skuParam, setSkuParam] = useQueryState('sku',
|
|
47
|
-
parseAsString.withDefault('').withOptions({ clearOnDefault: true })
|
|
48
|
-
)
|
|
49
|
-
|
|
50
|
-
const [addParam, setAddParam] = useQueryState('add',
|
|
51
|
-
parseAsBoolean.withDefault(false).withOptions({ clearOnDefault: true })
|
|
52
|
-
)
|
|
53
|
-
|
|
54
|
-
const directMutator: GetMutator = (level: 1 | 2): StringMutator => {
|
|
55
|
-
|
|
56
|
-
const setLevel = (value: string, level: 1 | 2 ): void => {
|
|
57
|
-
const facets = cmmc.facetsValue
|
|
58
|
-
facets[level] = [value]
|
|
59
|
-
cmmc.setFacets(facets)
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
const getLevelValueSafe = (level: 1 | 2): string | null => {
|
|
63
|
-
const facets = cmmc.facetsValue
|
|
64
|
-
if (!(level in facets) || facets[level].length === 0 ) {
|
|
65
|
-
return null
|
|
66
|
-
}
|
|
67
|
-
return facets[level][0]
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
return {
|
|
71
|
-
get: () => (getLevelValueSafe(level)),
|
|
72
|
-
set: (v: string) => {setLevel(v, level)}
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
const paramsMutator: GetMutator = (level: 1 | 2): StringMutator => {
|
|
77
|
-
return level === 1 ? {
|
|
78
|
-
get: () => (level1),
|
|
79
|
-
set: setLevel1
|
|
80
|
-
} : {
|
|
81
|
-
get: () => (level2),
|
|
82
|
-
set: setLevel2
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
let message: string | undefined = undefined
|
|
87
|
-
|
|
88
|
-
useEffect(() => {
|
|
89
|
-
|
|
90
|
-
const setCurrentCategoryFromSku = (sku: string) => {
|
|
91
|
-
const toks: string[] = sku.split('-')
|
|
92
|
-
cmmc.setFacets({
|
|
93
|
-
1: [toks[1]],
|
|
94
|
-
2: [toks[2]]
|
|
95
|
-
})
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
if (skuParam) {
|
|
99
|
-
// true if a valid sku
|
|
100
|
-
if (cmmc.setCurrentItem(skuParam)) {
|
|
101
|
-
if (addParam && cmmc.currentItem!.quantity === 0) {
|
|
102
|
-
cmmc.currentItem!.increment()
|
|
103
|
-
setAddParam(false)
|
|
104
|
-
}
|
|
105
|
-
// Marks that we've been here so no need to
|
|
106
|
-
// create an autorun() instance twice.
|
|
107
|
-
if (!encRef.current.usingSkuMode) {
|
|
108
|
-
setCurrentCategoryFromSku(skuParam)
|
|
109
|
-
encRef.current.autoRunDisposer = autorun(() => {
|
|
110
|
-
if (cmmc.currentItem && cmmc.currentItem.sku !== skuParam) {
|
|
111
|
-
setSkuParam(cmmc.currentItem.sku)
|
|
112
|
-
}
|
|
113
|
-
})
|
|
114
|
-
}
|
|
115
|
-
encRef.current.usingSkuMode = true
|
|
116
|
-
}
|
|
117
|
-
else {
|
|
118
|
-
|
|
119
|
-
setSkuParam('')
|
|
120
|
-
// if sent here w an invalid sku,
|
|
121
|
-
// it will effectively put us in facet params mode
|
|
122
|
-
message = 'Invalid sku. ' + PLEASE_SELECT_FACETS
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
setLoading && setLoading(false)
|
|
127
|
-
}, [skuParam])
|
|
128
|
-
|
|
129
|
-
// supposed to be when component unmounts
|
|
130
|
-
/*
|
|
131
|
-
useEffect(() => (() => {
|
|
132
|
-
if (encRef.current.autoRunDisposer) {
|
|
133
|
-
//encRef.current.autoRunDisposer() // no idea why this seems to be called prematurely and so many times <shrug>
|
|
134
|
-
}
|
|
135
|
-
}), [])
|
|
136
|
-
*/
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
// Level Params Mode
|
|
140
|
-
useEffect(() => {
|
|
141
|
-
|
|
142
|
-
if (encRef.current.usingSkuMode) return
|
|
143
|
-
|
|
144
|
-
const facets: FacetsValue = { }
|
|
145
|
-
if (level1) { facets[1] = [level1] }
|
|
146
|
-
if (level2) { facets[2] = [level2] }
|
|
147
|
-
//console.log(`LEV1: ${level1}, LEV2: ${level2}`)
|
|
148
|
-
if (level1 && level2) {
|
|
149
|
-
const categories = cmmc.setFacets(facets)
|
|
150
|
-
if (categories && categories.length > 0) {
|
|
151
|
-
cmmc.setCurrentItem(categories[0].products[0].sku)
|
|
152
|
-
}
|
|
153
|
-
else {
|
|
154
|
-
cmmc.setCurrentItem(undefined)
|
|
155
|
-
setMessage('Unrecognized facets. ' + PLEASE_SELECT_FACETS)
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
else {
|
|
159
|
-
setMessage(message ?? PLEASE_SELECT_FACETS)
|
|
160
|
-
}
|
|
161
|
-
setLoading && setLoading(false)
|
|
162
|
-
}, [level1 , level2])
|
|
163
|
-
|
|
164
|
-
return {
|
|
165
|
-
getMutator: encRef.current.usingSkuMode ?
|
|
166
|
-
directMutator : paramsMutator
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
export default useSkuAndFacetParams
|