-
Notifications
You must be signed in to change notification settings - Fork 147
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
Fix Page Designer ImageWithText Link component #1092
Merged
+21
−3
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
464c389
Fix PD ImageWithText Link component by using Chakra UI Link
adamraya b9c89c0
Use isAbsoluteURL to use react-router Link or Chakra Link component
adamraya 77f0dc9
PR Feedback
adamraya bf0d7bd
Clean up
adamraya fa203e1
Update packages/template-retail-react-app/app/page-designer/assets/im…
adamraya 205f320
Remove temporal page-viewer page to facilitate review
adamraya File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
packages/template-retail-react-app/app/page-designer/assets/utils.js
adamraya marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/* | ||
* Copyright (c) 2023, Salesforce, Inc. | ||
* All rights reserved. | ||
* 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 | ||
*/ | ||
|
||
/** | ||
* Determines whether the specified URL is absolute | ||
* | ||
* @param {string} url The URL to test | ||
* @returns {boolean} True if the specified URL is absolute, otherwise false | ||
*/ | ||
export const isAbsoluteURL = (url) => /^([a-z][a-z\d+\-.]*:)?\/\//i.test(url) |
61 changes: 61 additions & 0 deletions
61
packages/template-retail-react-app/app/pages/page-viewer/index.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Copyright (c) 2023, Salesforce, Inc. | ||
* All rights reserved. | ||
* 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 | ||
*/ | ||
import React from 'react' | ||
import {Box} from '@chakra-ui/react' | ||
import {Page, pageType} from '../../page-designer' | ||
import {ImageTile, ImageWithText} from '../../page-designer/assets' | ||
import { | ||
Carousel, | ||
MobileGrid1r1c, | ||
MobileGrid2r1c, | ||
MobileGrid2r2c, | ||
MobileGrid2r3c, | ||
MobileGrid3r1c, | ||
MobileGrid3r2c | ||
} from '../../page-designer/layouts' | ||
|
||
import {HTTPError, HTTPNotFound} from 'pwa-kit-react-sdk/ssr/universal/errors' | ||
|
||
const PAGEDESIGNER_TO_COMPONENT = { | ||
'commerce_assets.photoTile': ImageTile, | ||
'commerce_assets.imageAndText': ImageWithText, | ||
'commerce_layouts.carousel': Carousel, | ||
'commerce_layouts.mobileGrid1r1c': MobileGrid1r1c, | ||
'commerce_layouts.mobileGrid2r1c': MobileGrid2r1c, | ||
'commerce_layouts.mobileGrid2r2c': MobileGrid2r2c, | ||
'commerce_layouts.mobileGrid2r3c': MobileGrid2r3c, | ||
'commerce_layouts.mobileGrid3r1c': MobileGrid3r1c, | ||
'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) | ||
} | ||
|
||
return {page} | ||
} | ||
|
||
PageViewer.displayName = 'PageViewer' | ||
|
||
PageViewer.propTypes = { | ||
page: pageType.isRequired | ||
} | ||
|
||
export default PageViewer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you look at the Link component from the template components folder you'll see that that component has similar logic built into it that you are duplicating here maybe? 🤔
I think there is a prop called useNavLink that you might be able to take advantage of.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think that is the case.
Reading our custom link component uses a react-router Link or NavLink component (used for the Breadcrumbs menu) with the Chakra Link styles.
pwa-kit/packages/template-retail-react-app/app/components/link/index.jsx
Lines 18 to 19 in 3396472
There's no logic to assert if the
url
string is absolute, and if so, use a Chakra UI Link component instead of react-router component.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The prop
useNavLink
only determines if we use react-router's Link vs NavLink component.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've resolved a similar issue recently by using the
as
prop. Since the...props
spread is after theas={useNavLink ? ...}
prop (L21 in the snippet below), you can override it by passing a customas
to theLink
component.pwa-kit/packages/template-retail-react-app/app/components/link/index.jsx
Lines 13 to 26 in 6b9d436
Meaning you could possibly adjust your current logic to something like:
Additionally, I added a
LinkComponent?: ComponentWithAs<'a'>
prop to our<Link/>
, which has a default value of<ChakraLink />
. This allows us to override the UI of the link (e.g. using Chakra UI's<LinkOverlay />
component) while retaining the react-router link logic from theas
prop.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @breadadams for the feedback. I played with the idea and you're right, we can override our custom react-router
<Link />
component logic with theChakraLink
logic by usingas={ChakraLink}
. The declaredas
prop passed to the<Link />
component will override theas
prop with...props
spread.But then we need to modify the
<Link />
to conditionally usehref
orto
props depending on if we useChakraLink
or a react-router-likeLink
component.The
LinkComponent
idea to override styles and keep the react-router Link logic makes sense as well, but today we use thelayerStyle
prop pattern to override styles.The scope of the PR is to fix the Link in the
ImageWithText
Page designer component. We plan to extract the Page designer code from the template in the future. In this context, I think it's better to solve the problem in theImageWithText
component file. But both ideas are good suggestions to make our custom<Link />
component more flexible in future extensibility work.