Skip to content

Commit

Permalink
fix: improve headless API (#525)
Browse files Browse the repository at this point in the history
* fix: change items array structure to be similar to bc docs

* fix: add more routes for openPage function

* feat: change internal obj structure of quote.addProducts params

* refactor: update way to calculate prices for cart items

* fix: filter products without sku

* fix: update way to transform data

* fix: restore unnecessary change
  • Loading branch information
bc-marco authored and BrianJiang2021 committed Jul 11, 2023
1 parent f368577 commit e38df72
Show file tree
Hide file tree
Showing 6 changed files with 314 additions and 187 deletions.
17 changes: 10 additions & 7 deletions apps/storefront/src/components/HeadlessController.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,27 @@ import {
B3SStorage,
endMasquerade,
getCurrentCustomerInfo,
LineItems,
startMasquerade,
} from '@/utils'

interface HeadlessControllerProps {
setOpenPage: Dispatch<SetStateAction<OpenPageState>>
}

const transformOptionSelectionsToAttributes = (items: CustomFieldItems[]) =>
const transformOptionSelectionsToAttributes = (items: LineItems[]) =>
items.map((product) => {
const { optionSelections } = product

return {
...product,
optionSelections: optionSelections.map(
({ optionId, optionValue }: Record<string, string | number>) => ({
optionId: `attribute[${optionId}]`,
optionValue,
})
optionSelections: optionSelections?.reduce(
(accumulator: Record<string, number>, { optionId, optionValue }) => {
accumulator[`attribute[${optionId}]`] = optionValue

return accumulator
},
{}
),
}
})
Expand Down Expand Up @@ -112,7 +115,7 @@ export default function HeadlessController({
quote: {
addProductFromPage: addProductFromPageToQuoteRef.current,
addProductsFromCart: () => addProductsFromCart(),
addProducts: (items) => addProductsToDraftQuote(items),
addProducts: (items) => addProductsToDraftQuote(items, setOpenPage),
},
user: {
getProfile: () => ({ ...customerRef.current, role }),
Expand Down
8 changes: 8 additions & 0 deletions apps/storefront/src/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ export enum HeadlessRoutes {
REGISTER_ACCOUNT = '/registered',
DRAFT_QUOTE = '/quoteDraft',
SHOPPING_LISTS = '/shoppingLists',
DASHBOARD = '/dashboard',
ORDERS = '/orders',
COMPANY_ORDERS = '/company-orders',
QUOTES = '/quotes',
PURCHASED_PRODUCTS = '/purchased-products',
ADDRESSES = '/addresses',
USER_MANAGEMENT = '/user-management',
ACCOUNT_SETTINGS = '/accountSettings',
}

export type HeadlessRoute = keyof typeof HeadlessRoutes
Expand Down
Loading

0 comments on commit e38df72

Please sign in to comment.