Skip to content

Commit 66b0345

Browse files
fix: declare RatingSummary in section components (#2707)
## What's the purpose of this pull request? This pull request includes some changes to the `RatingSummary` component and its integration within the `ReviewsAndRatings` section, like adding a callback for the create review button, dynamically importing the `RatingSummary` component, and updating the `ReviewsAndRatings` section to use the new properties and overrides. ## How it works? Enhancements to `RatingSummary` component: * Added `onCreateReviewClick` callback property to `RatingSummaryProps` to handle create review button clicks. [[1]](diffhunk://#diff-3842b6542c4617b9089bcfe95aae46869139ddb93e8dcd66334f2e63bea0f669R43-R46) [[2]](diffhunk://#diff-3842b6542c4617b9089bcfe95aae46869139ddb93e8dcd66334f2e63bea0f669R96) [[3]](diffhunk://#diff-3842b6542c4617b9089bcfe95aae46869139ddb93e8dcd66334f2e63bea0f669L110-R115) Integration of `RatingSummary` in `ReviewsAndRatings` section: * Dynamically imported `RatingSummary` component in `ReviewsAndRatingsDefaultComponents`. * Updated `ReviewsAndRatingsSection` to pass new properties (`ratingSummary`, `addReviewModal`) to `ReviewsAndRatings` component. * Modified `ReviewsAndRatings` component to accept and use new properties (`ratingSummary`, `addReviewModal`) and to use the overridden `RatingSummary` component. [[1]](diffhunk://#diff-3240d26fcff836d6cf071f404185609676c1bd28d2adaad248e94c88efae1e86R4-R27) [[2]](diffhunk://#diff-3240d26fcff836d6cf071f404185609676c1bd28d2adaad248e94c88efae1e86L21-R45) Type definitions update: * Added `RatingSummaryProps` to overrides type definitions and updated `SectionsOverrides` to include `RatingSummary` component. [[1]](diffhunk://#diff-493fb3077e5ff0ebee6d765533eda8dd478cbb7ef439d8fbc526fd9bed7daa7cR45) [[2]](diffhunk://#diff-493fb3077e5ff0ebee6d765533eda8dd478cbb7ef439d8fbc526fd9bed7daa7cL363-R369) ## How to test it? - You can check the starter build on the starter preview of the branch bellow that is already using this changes: > - #2695 --------- Co-authored-by: Fanny Chien <[email protected]>
1 parent 87d1131 commit 66b0345

File tree

5 files changed

+44
-16
lines changed

5 files changed

+44
-16
lines changed

packages/components/src/organisms/RatingSummary/RatingSummary.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ export interface RatingSummaryProps extends HTMLAttributes<HTMLDivElement> {
4040
* ID to find this component in testing tools (e.g.: cypress, testing library, and jest).
4141
*/
4242
testId?: string
43+
/**
44+
* Callback to be called when the create review button is clicked.
45+
*/
46+
onCreateReviewClick?: () => void
4347
}
4448

4549
const RatingSummaryHeader = ({
@@ -89,6 +93,7 @@ export const RatingSummary = forwardRef<HTMLDivElement, RatingSummaryProps>(
8993
} = {},
9094
} = {},
9195
testId = 'fs-rating-summary',
96+
onCreateReviewClick,
9297
...props
9398
},
9499
ref
@@ -107,10 +112,7 @@ export const RatingSummary = forwardRef<HTMLDivElement, RatingSummaryProps>(
107112
singleReviewText={ratingCounterSingleReviewText}
108113
multipleReviewsText={ratingCounterMultipleReviewsText}
109114
/>
110-
<Button
111-
variant="secondary"
112-
onClick={() => alert('Write a review button clicked!')}
113-
>
115+
<Button variant="secondary" onClick={onCreateReviewClick}>
114116
{buttonText}
115117
</Button>
116118
{totalCount > 0 && <RatingDistribution distribution={distribution} />}
Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1+
import dynamic from 'next/dynamic'
2+
3+
const UIRatingSummary = dynamic(() =>
4+
/* webpackChunkName: "UIRatingSummary" */
5+
import('@faststore/ui').then((module) => module.RatingSummary)
6+
)
7+
18
export const ReviewsAndRatingsDefaultComponents = {
2-
// TODO: Update this with the components that will be used in ReviewsAndRatings section
3-
// Olhar o packages/core/src/components/sections/ProductGallery/DefaultComponents.ts
4-
// ou o packages/core/src/components/sections/ProductShelf/DefaultComponents.ts
5-
// para se basear
9+
RatingSummary: UIRatingSummary,
610
} as const

packages/core/src/components/sections/ReviewsAndRatings/ReviewsAndRatings.tsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
11
import { useMemo } from 'react'
22

3-
import ReviewsAndRatings from '../../ui/ReviewsAndRatings'
3+
import ReviewsAndRatings, {
4+
type ReviewsAndRatingsProps,
5+
} from '../../ui/ReviewsAndRatings'
46
import styles from '../ReviewsAndRatings/section.module.scss'
57
import Section from '../Section'
68
import { ReviewsAndRatingsDefaultComponents } from './DefaultComponents'
79
import { getOverridableSection } from '../../../sdk/overrides/getOverriddenSection'
810

911
interface Props {
10-
title: string
12+
title: ReviewsAndRatingsProps['title']
13+
ratingSummary: ReviewsAndRatingsProps['ratingSummary']
1114
}
12-
const ReviewsAndRatingsSection = ({ title }: Props) => {
15+
const ReviewsAndRatingsSection = (props: Props) => {
1316
return (
1417
<Section
1518
id="reviews-and-ratings"
1619
className={`${styles.section} section-reviews-and-ratings layout__section`}
1720
>
18-
<ReviewsAndRatings title={title} />
21+
<ReviewsAndRatings {...props} />
1922
</Section>
2023
)
2124
}

packages/core/src/components/ui/ReviewsAndRatings/ReviewsAndRatings.tsx

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
1-
import { RatingSummary } from '@faststore/ui'
21
import { usePDP } from 'src/sdk/overrides/PageProvider'
32
import useScreenResize from 'src/sdk/ui/useScreenResize'
3+
import type { RatingSummaryProps } from '@faststore/components'
4+
import { useOverrideComponents } from 'src/sdk/overrides/OverrideContext'
45

56
export type ReviewsAndRatingsProps = {
67
title: string
8+
ratingSummary: {
9+
ratingCounter: RatingSummaryProps['textLabels']['ratingCounter']
10+
createReviewButton: RatingSummaryProps['textLabels']['createReviewButton']
11+
}
712
}
813

9-
function ReviewsAndRatings({ title, ...otherProps }: ReviewsAndRatingsProps) {
14+
function ReviewsAndRatings({ title, ratingSummary }: ReviewsAndRatingsProps) {
1015
const context = usePDP()
16+
const { RatingSummary } = useOverrideComponents<'ReviewsAndRatings'>()
1117
const { isDesktop } = useScreenResize()
1218

1319
const rating = context?.data?.product?.rating
@@ -18,7 +24,14 @@ function ReviewsAndRatings({ title, ...otherProps }: ReviewsAndRatingsProps) {
1824
<h2 className="text__title-section layout__content">{title}</h2>
1925
<div data-fs-content>
2026
{(isDesktop || rating?.totalCount > 0) && (
21-
<RatingSummary {...rating} {...otherProps} />
27+
<RatingSummary.Component
28+
{...RatingSummary.props}
29+
textLabels={{ ...ratingSummary }}
30+
// Dynamic props shouldn't be overridable
31+
// This decision can be reviewed later if needed
32+
{...rating}
33+
onCreateReviewClick={() => alert('Create review')}
34+
/>
2235
)}
2336
</div>
2437
</>

packages/core/src/typings/overrides.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import type {
4242
SKUMatrixProps,
4343
SKUMatrixTriggerProps,
4444
SKUMatrixSidebarProps,
45+
RatingSummaryProps,
4546
} from '@faststore/ui'
4647

4748
import type {
@@ -360,7 +361,12 @@ export type SectionsOverrides = {
360361
ReviewsAndRatings: {
361362
Section: typeof ReviewsAndRatings
362363
// TODO: Add components
363-
components: {}
364+
components: {
365+
RatingSummary: ComponentOverrideDefinition<
366+
RatingSummaryProps,
367+
RatingSummaryProps
368+
>
369+
}
364370
}
365371
RegionBar: {
366372
Section: typeof RegionBar

0 commit comments

Comments
 (0)