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

feat: add reviews count to ProductTitle component #2655

Merged
Merged
Show file tree
Hide file tree
Changes from 12 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
52 changes: 48 additions & 4 deletions packages/components/src/molecules/ProductTitle/ProductTitle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,33 @@ export interface ProductTitleProps
*/
refNumber?: string
/**
* The current value of the rating, a number from 0 to 5.
* Object containing the rating value and reviews count
*/
ratingValue?: number
reviewsAndRating?: {
/**
* The current value of the rating, a number from 0 to 5.
*/
ratingValue?: number
/**
* The amount of reviews for the product.
*/
reviewsCount?: number
/**
* Text to display when there aren't reviews.
* @default "No reviews yet".
*/
noReviewsText?: string
/**
* Text to display when there is only one review.
* @default "review".
*/
singleReviewText?: string
/**
* Text to display when there are multiple reviews.
* @default "reviews".
*/
multipleReviewsText?: string
}
}

const ProductTitle = forwardRef<HTMLElement, ProductTitleProps>(
Expand All @@ -39,11 +63,19 @@ const ProductTitle = forwardRef<HTMLElement, ProductTitleProps>(
refTag = 'Ref.: ',
refNumber,
testId = 'fs-product-title',
ratingValue,
reviewsAndRating,
...otherProps
},
ref
) {
const {
ratingValue,
reviewsCount,
noReviewsText = 'No reviews yet',
singleReviewText = 'review',
multipleReviewsText = 'reviews',
} = reviewsAndRating || {}

return (
<header
ref={ref}
Expand All @@ -58,7 +90,19 @@ const ProductTitle = forwardRef<HTMLElement, ProductTitleProps>(

{(refNumber || ratingValue !== undefined) && (
<div data-fs-product-title-addendum>
{ratingValue !== undefined && <Rating value={ratingValue} />}
<div data-fs-product-title-rating>
{ratingValue !== undefined && <Rating value={ratingValue} />}

{reviewsCount !== undefined && (
<a href="#reviews-and-ratings" data-fs-product-title-reviews>
Copy link
Contributor

Choose a reason for hiding this comment

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

just double checking that this is the fixed id, right?

{reviewsCount === 0 && `(${noReviewsText})`}
{reviewsCount === 1 &&
`(${reviewsCount} ${singleReviewText})`}
{reviewsCount > 1 &&
`(${reviewsCount} ${multipleReviewsText})`}
</a>
)}
</div>
{refNumber && (
<>
{refTag} {refNumber}
Expand Down
21 changes: 21 additions & 0 deletions packages/core/cms/faststore/sections.json
Original file line number Diff line number Diff line change
Expand Up @@ -1507,6 +1507,27 @@
"title": "Show Reference Number?",
"type": "boolean",
"default": false
},
"rating": {
"title": "Rating",
"type": "object",
"properties": {
"noReviewsText": {
"title": "No reviews text",
"type": "string",
"default": "No reviews yet"
},
"multipleReviewsText": {
"title": "Multiple reviews text",
"type": "string",
"default": "reviews"
},
"singleReviewText": {
"title": "Single review text",
"type": "string",
"default": "review"
}
}
}
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ export interface ProductDetailsProps {
size: 'big' | 'small'
showDiscountBadge: boolean
}
rating: {
noReviewsText: string
singleReviewText: string
multipleReviewsText: string
}
}
buyButton: {
title: string
Expand Down Expand Up @@ -88,6 +93,7 @@ function ProductDetails({
productTitle: {
refNumber: showRefNumber,
discountBadge: { showDiscountBadge, size: discountBadgeSize },
rating: { noReviewsText, multipleReviewsText, singleReviewText },
},
buyButton: { icon: buyButtonIcon, title: buyButtonTitle },
shippingSimulator: {
Expand Down Expand Up @@ -199,9 +205,6 @@ function ProductDetails({
// Maybe now it's worth to make title always a h1 and receive only the name, as it would be easier for users to override.
title={<h1>{name}</h1>}
{...ProductTitle.props}
ratingValue={
apiConfig.reviewsAndRatings ? rating.average : undefined
}
label={
showDiscountBadge && (
<DiscountBadge.Component
Expand All @@ -223,6 +226,17 @@ function ProductDetails({
)
}
refNumber={showRefNumber && productId}
reviewsAndRating={{
ratingValue: apiConfig.reviewsAndRatings
? rating.average
: undefined,
reviewsCount: apiConfig.reviewsAndRatings
? rating.totalCount
: undefined,
noReviewsText,
multipleReviewsText,
singleReviewText,
}}
/>
</header>
<ImageGallery.Component
Expand Down
53 changes: 38 additions & 15 deletions packages/ui/src/components/molecules/ProductTitle/styles.scss
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,43 @@
// --------------------------------------------------------

// Default properties
--fs-product-title-text-size : var(--fs-text-size-title-product);
--fs-product-title-text-weight : var(--fs-text-weight-regular);
--fs-product-title-line-height : 1.12;
--fs-product-title-column-gap : var(--fs-spacing-2);
--fs-product-title-row-gap : var(--fs-spacing-3);
--fs-product-title-text-size : var(--fs-text-size-title-product);
--fs-product-title-text-weight : var(--fs-text-weight-regular);
--fs-product-title-line-height : 1.12;
--fs-product-title-column-gap : var(--fs-spacing-2);
--fs-product-title-row-gap : var(--fs-spacing-3);

--fs-product-title-addendum-color : var(--fs-color-text-light);
--fs-product-title-addendum-size : var(--fs-text-size-1);
--fs-product-title-addendum-line-height : 1.7;
// Addendum
--fs-product-title-addendum-color : var(--fs-color-text-light);
--fs-product-title-addendum-size : var(--fs-text-size-1);
--fs-product-title-addendum-line-height : 1.7;

// Reviews
--fs-product-title-reviews-color : var(--fs-color-text-light);
--fs-product-title-reviews-size : var(--fs-text-size-1);
--fs-product-title-reviews-line-height : 1.42;

// --------------------------------------------------------
// Structural Styles
// --------------------------------------------------------

[data-fs-product-title-header] {
display: flex;
justify-content: space-between;
max-width: 42ch;
flex-wrap: wrap;
column-gap: var(--fs-product-title-column-gap);
row-gap: var(--fs-product-title-row-gap);
column-gap: var(--fs-product-title-column-gap);
justify-content: space-between;
max-width: 42ch;

// Title prop
& > *:first-child {
*:first-child {
font-size: var(--fs-product-title-text-size);
font-weight: var(--fs-product-title-text-weight);
line-height: var(--fs-product-title-line-height);
}

[data-fs-badge] { white-space: nowrap; }
[data-fs-badge] {
white-space: nowrap;
}

@include media(">=tablet", "<notebook") {
flex-direction: column;
Expand All @@ -43,11 +50,27 @@

[data-fs-product-title-addendum] {
display: flex;
justify-content: space-between;
flex-wrap: wrap;
align-items: center;
justify-content: space-between;
margin-top: var(--fs-product-title-row-gap);
font-size: var(--fs-product-title-addendum-size);
line-height: var(--fs-product-title-addendum-line-height);
color: var(--fs-product-title-addendum-color);
}

[data-fs-product-title-rating] {
display: flex;
flex-wrap: wrap;
gap: var(--fs-product-title-column-gap);
align-items: center;
}

[data-fs-product-title-reviews] {
font-size: inherit;
line-height: inherit;
color: inherit;
text-decoration: none;
cursor: pointer;
}
}
Loading