@hanzo/commerce 6.4.3 → 6.4.5

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.
@@ -14,7 +14,7 @@ import type {
14
14
  CategoryNode,
15
15
  } from '../../types'
16
16
 
17
- import { useCommerce } from '../../service/context'
17
+ import { useCommerce } from '../../context'
18
18
  import * as pathUtils from '../../service/path-utils'
19
19
  import { getFacetValuesMutator, ObsStringMutator } from '../../util'
20
20
 
@@ -22,7 +22,7 @@ import type {
22
22
  MultiFamilySelectorOptions,
23
23
  } from '../../types'
24
24
 
25
- import { useCommerce } from '../../service/context'
25
+ import { useCommerce } from '../../context'
26
26
  import { getSelectionUISpecifier } from '../../util'
27
27
 
28
28
  import { CarouselItemSelector, ButtonItemSelector } from '../item-selector'
@@ -12,7 +12,7 @@ import {
12
12
  } from '@hanzo/ui/primitives'
13
13
 
14
14
  import { formatCurrencyValue } from '../../util'
15
- import { useCommerce } from '../../service/context'
15
+ import { useCommerce } from '../../context'
16
16
 
17
17
  import CartPanel from './cart-panel'
18
18
 
@@ -9,7 +9,7 @@ import { Image } from '@hanzo/ui/primitives'
9
9
  import type { LineItem } from '../../../types'
10
10
  import { formatCurrencyValue } from '../../../util'
11
11
  import AddToCartWidget from '../../buy/add-to-cart-widget'
12
- import { useCommerce } from '../../../service/context'
12
+ import { useCommerce } from '../../../context'
13
13
 
14
14
  const DEF_IMG_SIZE=40
15
15
 
@@ -5,7 +5,7 @@ import { observer } from 'mobx-react-lite'
5
5
  import { Button, ScrollArea } from '@hanzo/ui/primitives'
6
6
  import { cn } from '@hanzo/ui/util'
7
7
 
8
- import { useCommerce } from '../../../service/context'
8
+ import { useCommerce } from '../../../context'
9
9
  import { formatCurrencyValue } from '../../../util'
10
10
  import { sendFBEvent, sendGAEvent } from '../../../util/analytics'
11
11
 
@@ -10,7 +10,7 @@ import { observer } from 'mobx-react-lite'
10
10
 
11
11
  import { Button, Form, FormControl, FormField, FormItem, Input } from '@hanzo/ui/primitives'
12
12
 
13
- import { useCommerce } from '../../../service/context'
13
+ import { useCommerce } from '../../../context'
14
14
  import type { Promo } from '../../../types'
15
15
  import getPromoFromApi from '../../../util/promo-codes'
16
16
 
@@ -9,7 +9,7 @@ import { useForm } from 'react-hook-form'
9
9
  import { Tabs, TabsContent, TabsList, TabsTrigger } from '@hanzo/ui/primitives'
10
10
  import { useAuth } from '@hanzo/auth/service'
11
11
 
12
- import { useCommerce } from '../../../service/context'
12
+ import { useCommerce } from '../../../context'
13
13
  import { sendFBEvent, sendGAEvent } from '../../../util/analytics'
14
14
  import type { CheckoutStepComponentProps, TransactionStatus } from '../../../types'
15
15
 
@@ -15,7 +15,7 @@ import {
15
15
 
16
16
  import { cn } from '@hanzo/ui/util'
17
17
 
18
- import { useCommerce } from '../../../../service/context'
18
+ import { useCommerce } from '../../../../context'
19
19
  import { processSquareCardPayment } from '../../../../util'
20
20
  import type { PaymentMethodComponentProps } from '../../../../types'
21
21
  import { sendFBEvent, sendGAEvent } from '../../../../util/analytics'
@@ -19,7 +19,7 @@ import {
19
19
  } from '@hanzo/ui/primitives'
20
20
 
21
21
  import Eth from '../crypto-icons/eth'
22
- import { useCommerce } from '../../../../service/context'
22
+ import { useCommerce } from '../../../../context'
23
23
  import type { PaymentMethodComponentProps } from '../../../../types'
24
24
  import { sendFBEvent, sendGAEvent } from '../../../../util/analytics'
25
25
 
@@ -19,7 +19,7 @@ import {
19
19
  SelectValue
20
20
  } from '@hanzo/ui/primitives'
21
21
 
22
- import { useCommerce } from '../../service/context'
22
+ import { useCommerce } from '../../context'
23
23
 
24
24
  import countries from '../../util/countries'
25
25
  import { sendGAEvent } from '../../util/analytics'
@@ -0,0 +1,61 @@
1
+ import {
2
+ action,
3
+ computed,
4
+ makeObservable,
5
+ observable,
6
+ } from 'mobx'
7
+
8
+ interface CommerceUI {
9
+ showBuyOptions: (skuPath: string) => void
10
+ hideBuyOptions: () => void
11
+
12
+ get buyOptionsShowing(): boolean
13
+
14
+ get skuPath(): string | undefined
15
+ clearSkuPath: () => void
16
+ }
17
+
18
+ class CommerceUIStore implements CommerceUI {
19
+
20
+ _skuPath: string | undefined = undefined
21
+ _optionsShowing: boolean = false
22
+
23
+ constructor() {
24
+ makeObservable(this, {
25
+ _skuPath: observable,
26
+ _optionsShowing: observable,
27
+ showBuyOptions: action,
28
+ hideBuyOptions: action,
29
+ buyOptionsShowing: computed,
30
+ skuPath: computed,
31
+ clearSkuPath: action
32
+ })
33
+ }
34
+
35
+ showBuyOptions = (skuPath: string): void => {
36
+ this._skuPath = skuPath
37
+ this._optionsShowing = true
38
+ }
39
+
40
+ hideBuyOptions = (): void => {
41
+ this._optionsShowing = false
42
+ }
43
+
44
+ get buyOptionsShowing(): boolean {
45
+ return this._optionsShowing
46
+ }
47
+
48
+ get skuPath(): string | undefined {
49
+ return this._skuPath
50
+ }
51
+
52
+ clearSkuPath = (): void => {
53
+ this._skuPath = undefined
54
+ this._optionsShowing = false
55
+ }
56
+ }
57
+
58
+ export {
59
+ CommerceUIStore,
60
+ type CommerceUI
61
+ }
@@ -0,0 +1,63 @@
1
+ 'use client'
2
+ import React, {
3
+ createContext,
4
+ useContext,
5
+ useRef,
6
+ type PropsWithChildren
7
+ } from 'react'
8
+
9
+ // https://dev.to/ivandotv/mobx-server-side-rendering-with-next-js-4m18
10
+ import { enableStaticRendering } from 'mobx-react-lite'
11
+ enableStaticRendering(typeof window === "undefined")
12
+
13
+ import type { ServiceOptions } from '..'
14
+ import type CommerceService from '../types/commerce-service'
15
+ import getInstance from '../service/get-instance'
16
+ import type { Family, CategoryNode, SelectionUISpecifier } from '../types'
17
+ import { type CommerceUI, CommerceUIStore } from './commerce-ui'
18
+
19
+ type CommerceContextValue = {
20
+ service: CommerceService
21
+ ui: CommerceUI
22
+ }
23
+
24
+ const CommerceContext = createContext<CommerceContextValue | undefined>(undefined)
25
+
26
+ const useCommerce = (): CommerceService => {
27
+ return (useContext(CommerceContext) as CommerceContextValue)?.service
28
+ }
29
+
30
+ const useCommerceUI = (): CommerceUI => {
31
+ return (useContext(CommerceContext) as CommerceContextValue)?.ui
32
+ }
33
+
34
+ const CommerceProvider: React.FC<PropsWithChildren & {
35
+ families: Family[]
36
+ rootNode: CategoryNode
37
+ options?: ServiceOptions
38
+ uiSpecs?: Record<string, SelectionUISpecifier>
39
+ }> = ({
40
+ children,
41
+ families,
42
+ rootNode,
43
+ options,
44
+ uiSpecs
45
+ }) => {
46
+
47
+ // TODO: Inject Promo fixture here from siteDef
48
+ const serviceRef = useRef<CommerceContextValue>({
49
+ service: getInstance(families, rootNode, options, uiSpecs),
50
+ ui: new CommerceUIStore()
51
+ })
52
+ return (
53
+ <CommerceContext.Provider value={serviceRef.current}>
54
+ {children}
55
+ </CommerceContext.Provider>
56
+ )
57
+ }
58
+
59
+ export {
60
+ useCommerce,
61
+ useCommerceUI,
62
+ CommerceProvider
63
+ }
package/index.ts CHANGED
@@ -1,4 +1,4 @@
1
- export * from './service/context'
1
+ export * from './context'
2
2
  export * from './components'
3
3
  export type { StandaloneServiceOptions as ServiceOptions } from './service/impls/standalone/standalone-service'
4
4
  export {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hanzo/commerce",
3
- "version": "6.4.3",
3
+ "version": "6.4.5",
4
4
  "description": "e-commerce framework.",
5
5
  "type": "module",
6
6
  "publishConfig": {
@@ -40,7 +40,7 @@
40
40
  "square": "^35.0.0"
41
41
  },
42
42
  "peerDependencies": {
43
- "@hanzo/auth": "^2.4.3",
43
+ "@hanzo/auth": "^2.4.4",
44
44
  "@hanzo/ui": "^3.6.3",
45
45
  "@hookform/resolvers": "^3.3.4",
46
46
  "@radix-ui/react-radio-group": "^1.1.3",
@@ -8,7 +8,7 @@ import {
8
8
  parseAsBoolean,
9
9
  } from 'next-usequerystate'
10
10
 
11
- import { useCommerce } from '../service/context'
11
+ import { useCommerce } from '../context'
12
12
  import type { SelectedPaths } from '../types'
13
13
  import sep from '../service/sep'
14
14
 
@@ -1,44 +0,0 @@
1
- 'use client'
2
- import { createContext, useContext, useRef, type PropsWithChildren, useEffect } from 'react'
3
-
4
- // https://dev.to/ivandotv/mobx-server-side-rendering-with-next-js-4m18
5
- import { enableStaticRendering } from 'mobx-react-lite'
6
- enableStaticRendering(typeof window === "undefined")
7
-
8
-
9
- import type CommerceService from '../types/commerce-service'
10
- import type { ServiceOptions } from '..'
11
- import getInstance from './get-instance'
12
- import type { Family, CategoryNode, SelectionUISpecifier } from '../types'
13
-
14
- const CommerceServiceContext = createContext<CommerceService | undefined>(undefined)
15
-
16
- const useCommerce = (): CommerceService => {
17
- return useContext(CommerceServiceContext) as CommerceService
18
- }
19
-
20
- const CommerceServiceProvider: React.FC<PropsWithChildren & {
21
- productsByFamily: Family[]
22
- rootNode: CategoryNode
23
- options?: ServiceOptions
24
- uiSpecs?: Record<string, SelectionUISpecifier>
25
- }> = ({
26
- children,
27
- productsByFamily,
28
- rootNode,
29
- options,
30
- uiSpecs
31
- }) => {
32
-
33
- // TODO: Inject Promo fixture here
34
- const serviceRef = useRef<CommerceService>(getInstance(productsByFamily, rootNode, options, uiSpecs))
35
- return (
36
- <CommerceServiceContext.Provider value={serviceRef.current}>
37
- {children}
38
- </CommerceServiceContext.Provider>
39
- )
40
- }
41
-
42
- export {
43
- useCommerce, CommerceServiceProvider
44
- }