Skip to content

Commit

Permalink
feat: Add new sdk to VariantReducer and ItemReducer
Browse files Browse the repository at this point in the history
  • Loading branch information
acasazza committed Nov 22, 2021
1 parent 2df5d67 commit 3ea29f8
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 25 deletions.
6 changes: 3 additions & 3 deletions src/reducers/ItemReducer.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { BaseUnsetState } from '#typings/index'
import { SkuCollection, PriceCollection } from '@commercelayer/js-sdk'
import { Sku, Price } from '@commercelayer/sdk'
import { Dispatch } from 'react'
import baseReducer from '#utils/baseReducer'

export interface Items {
[skuCode: string]: SkuCollection
[skuCode: string]: Sku
}

export interface ItemPrices {
[skuCode: string]: PriceCollection
[skuCode: string]: Price
}

export interface ItemQuantity {
Expand Down
1 change: 0 additions & 1 deletion src/reducers/PriceReducer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { BaseAction, LoaderType } from '#typings'
import CLayer, { PriceCollection } from '@commercelayer/js-sdk'
import Sdk, { Price } from '@commercelayer/sdk'
import getPrices from '#utils/getPrices'
import { CommerceLayerConfig } from '#context/CommerceLayerContext'
Expand Down
103 changes: 85 additions & 18 deletions src/reducers/VariantReducer.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import CLayer, { SkuCollection } from '@commercelayer/js-sdk'
import Sdk, { Sku } from '@commercelayer/sdk'
import { VariantOptions } from '#components/VariantSelector'
import { Dispatch } from 'react'
import baseReducer from '#utils/baseReducer'
import getErrorsByCollection from '#utils/getErrorsByCollection'
import getSkus from '#utils/getSkus'
import { CommerceLayerConfig } from '#context/CommerceLayerContext'
import { Items, CustomLineItem, SetCustomLineItems } from './ItemReducer'
import { BaseError } from '#typings/errors'
import { isEmpty, has } from 'lodash'
import getOrganizationSlug from '#utils/organization'

type SetSkuCodeVariantParams = {
code: string
Expand All @@ -32,7 +32,7 @@ export interface SetVariantSkuCodes {
}

export interface VariantsObject {
[key: string]: SkuCollection
[key: string]: Sku
}

export type SetSkuCode = (
Expand All @@ -50,7 +50,7 @@ export interface VariantPayload {
currentSkuId?: string
currentSkuInventory?: any
currentQuantity?: number
currentPrices?: SkuCollection[]
currentPrices?: Sku[]
setSkuCode?: SetSkuCode
setSkuCodes?: (skuCodes: VariantOptions[]) => void
}
Expand Down Expand Up @@ -93,24 +93,46 @@ export interface UnsetVariantState {
export const setSkuCode: SetSkuCodeVariant = (params) => {
const { id, code, config, setItem, dispatch } = params
if (id) {
CLayer.Sku.withCredentials(config)
.includes('skuOptions')
.find(id)
.then((s) => {
const org = getOrganizationSlug(config.endpoint)
const sdk = Sdk({
accessToken: config.accessToken,
...org,
})
sdk.skus
.retrieve(id, { include: ['sku_options'] })
.then((sku) => {
setItem &&
setItem({
[`${code}`]: s,
[`${code}`]: sku,
})
})
.catch((c) => {
const errors = getErrorsByCollection(c, 'variant')
.catch((errors) => {
debugger
dispatch({
type: 'setErrors',
payload: {
errors,
},
})
})
// CLayer.Sku.withCredentials(config)
// .includes('skuOptions')
// .find(id)
// .then((s) => {
// setItem &&
// setItem({
// [`${code}`]: s,
// })
// })
// .catch((c) => {
// const errors = getErrorsByCollection(c, 'variant')
// dispatch({
// type: 'setErrors',
// payload: {
// errors,
// },
// })
// })
}
}

Expand All @@ -129,11 +151,20 @@ export interface GetVariants {

export const getVariants: GetVariants = (params) => {
const { config, state, skuCode, dispatch, setItem, filters } = params
CLayer.Sku.withCredentials(config)
.where({ codeIn: state.skuCodes.join(','), ...filters })
.all()
.then((r) => {
const skusObj = getSkus(r.toArray())
const org = getOrganizationSlug(config.endpoint)
const sdk = Sdk({
accessToken: config.accessToken,
...org,
})
sdk.skus
.list({
filters: {
code_in: state.skuCodes.join(','),
...filters,
},
})
.then((skus) => {
const skusObj = getSkus(skus)
if (skuCode) {
setSkuCode({
code: skusObj[skuCode].code,
Expand All @@ -156,15 +187,51 @@ export const getVariants: GetVariants = (params) => {
},
})
})
.catch((c) => {
const errors = getErrorsByCollection(c, 'variant')
.catch((errors) => {
debugger
dispatch({
type: 'setErrors',
payload: {
errors,
},
})
})
// CLayer.Sku.withCredentials(config)
// .where({ codeIn: state.skuCodes.join(','), ...filters })
// .all()
// .then((r) => {
// const skusObj = getSkus(r.toArray())
// if (skuCode) {
// setSkuCode({
// code: skusObj[skuCode].code,
// id: skusObj[skuCode].id,
// config,
// dispatch,
// setItem,
// })
// }
// dispatch({
// type: 'setVariants',
// payload: {
// variants: skusObj,
// },
// })
// dispatch({
// type: 'setLoading',
// payload: {
// loading: false,
// },
// })
// })
// .catch((c) => {
// const errors = getErrorsByCollection(c, 'variant')
// dispatch({
// type: 'setErrors',
// payload: {
// errors,
// },
// })
// })
}

export const unsetVariantState: UnsetVariantState = (dispatch) => {
Expand Down
6 changes: 3 additions & 3 deletions src/utils/getSkus.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { SkuCollection } from '@commercelayer/js-sdk'
import { Sku } from '@commercelayer/sdk'

const getSkus = (prices: SkuCollection[]): Record<string, any> => {
const getSkus = (prices: Sku[]): Record<string, any> => {
const obj: Record<string, any> = {}
prices.map((sku) => {
obj[sku.code] = sku
obj[sku.code as string] = sku
})
return obj
}
Expand Down

0 comments on commit 3ea29f8

Please sign in to comment.