Skip to content

Commit

Permalink
cmmc: +CommerceConfig (#101)
Browse files Browse the repository at this point in the history
  • Loading branch information
artemis-prime committed Jun 4, 2024
1 parent 5e209f8 commit 6ac9d14
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 23 deletions.
15 changes: 4 additions & 11 deletions packages/commerce/context/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ import React, {
import { enableStaticRendering } from 'mobx-react-lite'
enableStaticRendering(typeof window === "undefined")

import type { ServiceOptions } from '..'
import type CommerceService from '../types/commerce-service'
import getInstance from '../service/get-instance'
import type { Family, CategoryNode, SelectionUISpecifier } from '../types'
import type { CommerceConfig } from '../types'

const CommerceContext = createContext<CommerceService | undefined>(undefined)

Expand All @@ -22,20 +21,14 @@ const useCommerce = (): CommerceService => {
}

const CommerceProvider: React.FC<PropsWithChildren & {
families: Family[]
rootNode: CategoryNode
options?: ServiceOptions
uiSpecs?: Record<string, SelectionUISpecifier>
config: CommerceConfig
}> = ({
children,
families,
rootNode,
options,
uiSpecs
config
}) => {

// TODO: Inject Promo fixture here from siteDef
const service = getInstance(families, rootNode, options, uiSpecs)
const service = getInstance(config)
const valueRef = useRef<CommerceService>(service)

return (
Expand Down
2 changes: 1 addition & 1 deletion packages/commerce/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@hanzo/commerce",
"version": "7.0.15",
"version": "7.1.0",
"description": "e-commerce framework.",
"publishConfig": {
"registry": "https://registry.npmjs.org/",
Expand Down
22 changes: 11 additions & 11 deletions packages/commerce/service/impls/standalone/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { enableStaticRendering } from 'mobx-react-lite'

import type { CommerceService, Family, CategoryNode, SelectionUISpecifier } from '../../../types'
import type { CommerceService, Family, CategoryNode, SelectionUISpecifier, CommerceConfig } from '../../../types'
import StandaloneService, {type StandaloneServiceOptions} from './standalone-service'
import { initSelectionUI } from '../../../util'

Expand All @@ -18,20 +18,20 @@ const _log = (s: string) => {

let instance: StandaloneService | undefined = undefined

export const getInstance = (
families: Family[],
rootNode: CategoryNode,
options?: StandaloneServiceOptions,
uiSpecs?: Record<string, SelectionUISpecifier>
): CommerceService => {
export const getInstance = ({
families,
rootNode,
options,
uiSpecifiers
}: CommerceConfig) : CommerceService => {

if (!options) {
throw new Error('cmmc getInstance(): Standalone Commerce Service requires config options!')
}

if (typeof window === "undefined") {
if (uiSpecs) {
initSelectionUI(uiSpecs)
if (uiSpecifiers) {
initSelectionUI(uiSpecifiers)
}
//_log("NEW INSTANCE FOR SERVER")
return new StandaloneService(
Expand All @@ -43,8 +43,8 @@ export const getInstance = (

// Client side, create the store only once in the client
if (!instance) {
if (uiSpecs) {
initSelectionUI(uiSpecs)
if (uiSpecifiers) {
initSelectionUI(uiSpecifiers)
}
//_log("NEW INSTANCE FOR CLIENT")
const snapShot = readSnapshot()
Expand Down
13 changes: 13 additions & 0 deletions packages/commerce/types/commerce-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import type { ServiceOptions } from '..'
import type { CategoryNode } from './category-node'
import type { Family } from './family'
import type { SelectionUISpecifier } from './selection-ui-specifier'

interface CommerceConfig {
families: Family[]
rootNode: CategoryNode
options?: ServiceOptions
uiSpecifiers?: Record<string, SelectionUISpecifier>
}

export { type CommerceConfig as default }
1 change: 1 addition & 0 deletions packages/commerce/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

export type { default as CommerceService } from './commerce-service'
export type { default as CommerceConfig } from './commerce-config'
export type { default as MultiFamilySelectorProps } from './multi-family-selector-props'
export type { default as Product } from './product'
export type { default as Promo } from './promo'
Expand Down

0 comments on commit 6ac9d14

Please sign in to comment.