Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[V3] Update Page Designer #1128

Merged
merged 11 commits into from
Apr 26, 2023
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion packages/commerce-sdk-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"files": [
"CHANGELOG.md",
"LICENSE",
"+(auth|hooks|scripts)/**/!(*.test*).{ts,js}",
"+(auth|components|hooks|scripts)/**/!(*.test*).{ts,js}",
"*.{js,d.ts}",
"!*.test*.{js,d.ts}",
"!test*.*",
Expand Down Expand Up @@ -75,6 +75,9 @@
"react": "^17",
"react-helmet": "6"
},
"optionalDependencies": {
"prop-types": "^15.8.1"
},
"engines": {
"node": "^16.0.0 || ^18.0.0",
"npm": "^7.0.0 || ^8.0.0 || ^9.0.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,18 @@
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import React from 'react'
import {Region as RegionType} from '../types'
import PropTypes from 'prop-types'
import {Component} from '../Component'
import {componentPropType} from '../prop-types'
import {Region as RegionType} from '../types'

/**
* This PropType represents a `region` object from the ShopperExperience API.
*/
export const propType = PropTypes.shape({
id: PropTypes.string,
components: PropTypes.arrayOf(componentPropType)
})

interface RegionProps extends React.ComponentProps<'div'> {
region: RegionType
Expand Down Expand Up @@ -36,4 +46,9 @@ export const Region = (props: RegionProps) => {

Region.displayName = 'Region'

Region.propTypes = {
region: propType.isRequired,
className: PropTypes.string
}

export default Region
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* SPDX-License-Identifier: BSD-3-Clause
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/

export * from './Component'
export * from './Page'
export * from './Region'
export * from './Page'
export * from './prop-types'
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import PropTypes from 'prop-types'
/**
* This PropType represents a `component` object from the ShopperExperience API.
*/
export const componentType = PropTypes.shape({
export const componentPropType = PropTypes.shape({
data: PropTypes.object,
id: PropTypes.string,
typeId: PropTypes.string
Expand All @@ -18,9 +18,9 @@ export const componentType = PropTypes.shape({
/**
* This PropType represents a `region` object from the ShopperExperience API.
*/
export const regionType = PropTypes.shape({
export const regionPropType = PropTypes.shape({
id: PropTypes.string,
components: PropTypes.arrayOf(componentType)
components: PropTypes.arrayOf(componentPropType)
})

/**
Expand All @@ -31,6 +31,6 @@ export const pageType = PropTypes.shape({
description: PropTypes.string,
id: PropTypes.string,
name: PropTypes.string,
regions: PropTypes.arrayOf(regionType),
regions: PropTypes.arrayOf(regionPropType),
typeId: PropTypes.string
})
43 changes: 19 additions & 24 deletions packages/template-retail-react-app/app/page-designer/README.md
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: At the end.

Using the local development server, you can now see Page Designer pages rendered in React.js at `http://localhost:3000/page-viewer/:pageid` by providing their `pageid`.

...by providing their pageid defined in Bussiness Manager.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thats clearer. Thanks

Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@

---

This folder contains React components and utilities that render pages from [Page Designer](https://documentation.b2c.commercecloud.salesforce.com/DOC2/topic/com.demandware.dochelp/content/b2c_commerce/topics/page_designer/b2c_creating_pd_pages.html).
This folder contains the React components used when rendering the pages from [Page Designer](https://documentation.b2c.commercecloud.salesforce.com/DOC2/topic/com.demandware.dochelp/content/b2c_commerce/topics/page_designer/b2c_creating_pd_pages.html).

Use this folder to add React components that can render Page Designer components that have been serialized to JSON.

> NOTE: If you are creating components that do not already exist in Page Designer, follow [this](https://documentation.b2c.commercecloud.salesforce.com/DOC1/index.jsp) guide to first create your Page Designer components before creating their matching PWA-Kit React components.

This folder includes components for layout and visualization of images, grids, and carousels.

**By default, Page Designer integration is not enabled in the Retail React App.** Additionally, to utilize the `shopperExperience`
Expand All @@ -20,8 +22,7 @@ for more information on configuring your SLAS client.

## Folder Structure

- **`/core`** - Base components for rendering: `<Page>`, `<Region>`, and `<Component>`. Use `<Page>` to render Page Designer content. Use `<Region>` and `<Component>` for creating new assets.
- **`/assets`** - Non-visual components used in Page Designer. Includes `<Image>` and `<ImageWithText>` as well as any other Page Designer assets that you want to use in your PWA-Kit app. If you need to visualize a component, add it here.
- **`/assets`** - Visual components used in Page Designer. Includes `<Image>` and `<ImageWithText>` as well as any other Page Designer assets that you want to use in your PWA-Kit app. If you need to visualize a component, add it here.
- **`/layouts`** - Components responsible for layout. Includes various grids and a `<Carousel>` component.

## Sample Usage
Expand All @@ -32,8 +33,10 @@ Create a new file called `app/pages/page-viewer/index.jsx`, and add the followin
// app/pages/page-viewer/index.jsx

import React from 'react'
import {useParams} from 'react-router-dom'
import {Box} from '@chakra-ui/react'
import {Page, pageType} from '../../page-designer'
import {usePage} from 'commerce-sdk-react-preview'
import {Page} from 'commerce-sdk-react-preview/components'
import {ImageTile, ImageWithText} from '../../page-designer/assets'
import {
Carousel,
Expand All @@ -59,32 +62,24 @@ const PAGEDESIGNER_TO_COMPONENT = {
'commerce_layouts.mobileGrid3r2c': MobileGrid3r2c
}

const PageViewer = ({page}) => (
<Box layerStyle={'page'}>
<Page page={page} components={PAGEDESIGNER_TO_COMPONENT} />
</Box>
)

PageViewer.getProps = async ({api, params}) => {
const {pageId} = params
const page = await api.shopperExperience.getPage({
parameters: {pageId}
})

if (page.isError) {
let ErrorClass = page.type?.endsWith('page-not-found') ? HTTPNotFound : HTTPError
throw new ErrorClass(page.detail)
const PageViewer = () => {
const {pageId} = useParams()
const {data: page, error}= usePage({parameters: {pageId}})

if (error) {
let ErrorClass = error.response?.status === 404 ? HTTPNotFound : HTTPError
throw new ErrorClass(error.response?.statusText)
}

return {page}
return (
<Box layerStyle={'page'}>
<Page page={page} components={PAGEDESIGNER_TO_COMPONENT} />
</Box>
)
}

PageViewer.displayName = 'PageViewer'

PageViewer.propTypes = {
page: pageType.isRequired
}

export default PageViewer
```

Copy link
Collaborator

@adamraya adamraya Apr 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: You may want to mention, for people unfamiliar with react-router, the route needs to be added before the catch-all route.

{
path: '*',
component: PageNotFound
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 I feel like at this point in time the developer should be aware as to what a router is and how it's used by going through the doc's on developer.salesforce.com. I'm hesitant to add router specific documentation outside of the context of the router incase it ever changes we might have to go back and change all the other doc's instead of only changing one place.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,5 @@
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/

export * from './component'
export * from './page'
export * from './region'
export * from './types'
export * from './image-tile'
export * from './image-with-text'

This file was deleted.

This file was deleted.

This file was deleted.

Loading