-
Notifications
You must be signed in to change notification settings - Fork 64
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: rating summary #2682
Open
Guilera
wants to merge
1
commit into
feat/reviews-and-ratings
Choose a base branch
from
feat/rating-summary
base: feat/reviews-and-ratings
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+461
−8
Open
feat: rating summary #2682
Changes from all commits
Commits
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
44 changes: 44 additions & 0 deletions
44
packages/components/src/organisms/RatingSummary/RatingDistribution.tsx
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,44 @@ | ||||||
import React, { forwardRef, type HTMLAttributes } from 'react' | ||||||
import RatingDistributionItem from './RatingDistributionItem' | ||||||
|
||||||
export interface RatingDistributionProps | ||||||
extends HTMLAttributes<HTMLOListElement> { | ||||||
/** | ||||||
* The rating distribution | ||||||
*/ | ||||||
distribution: { | ||||||
starsOne: number | ||||||
starsTwo: number | ||||||
starsThree: number | ||||||
starsFour: number | ||||||
starsFive: number | ||||||
} | ||||||
/** | ||||||
* Optional test ID for testing. | ||||||
*/ | ||||||
testId?: string | ||||||
} | ||||||
|
||||||
export const RatingDistribution = forwardRef< | ||||||
HTMLOListElement, | ||||||
RatingDistributionProps | ||||||
>(function ProgressStatus( | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should it be
Suggested change
|
||||||
{ | ||||||
distribution: { starsFive, starsFour, starsThree, starsTwo, starsOne }, | ||||||
testId = 'fs-rating-distribution', | ||||||
...props | ||||||
}, | ||||||
ref | ||||||
) { | ||||||
return ( | ||||||
<ol ref={ref} data-fs-rating-distribution data-testid={testId} {...props}> | ||||||
<RatingDistributionItem ratingIndex={5} value={starsFive} /> | ||||||
<RatingDistributionItem ratingIndex={4} value={starsFour} /> | ||||||
<RatingDistributionItem ratingIndex={3} value={starsThree} /> | ||||||
<RatingDistributionItem ratingIndex={2} value={starsTwo} /> | ||||||
<RatingDistributionItem ratingIndex={1} value={starsOne} /> | ||||||
</ol> | ||||||
) | ||||||
}) | ||||||
|
||||||
export default RatingDistribution |
50 changes: 50 additions & 0 deletions
50
packages/components/src/organisms/RatingSummary/RatingDistributionItem.tsx
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,50 @@ | ||
import React, { forwardRef, type HTMLAttributes } from 'react' | ||
import Icon from '../../atoms/Icon' | ||
|
||
export interface RatingDistributionItemProps | ||
extends HTMLAttributes<HTMLLIElement> { | ||
ratingIndex: number | ||
/* Current value of the progress */ | ||
value: number | ||
/* Optional test ID for testing*/ | ||
testId?: string | ||
} | ||
|
||
const ItemStar = ({ ratingIndex }: { ratingIndex: number }) => ( | ||
<span data-fs-rating-distribution-item-star> | ||
<p>{ratingIndex}</p> | ||
<Icon data-fs-rating-distribution-item-star-icon name="Star" /> | ||
</span> | ||
) | ||
|
||
const ItemProgressBar = ({ value }: { value: number }) => ( | ||
<div data-fs-rating-distribution-item-progress-bar> | ||
<div data-fs-rating-distribution-item-progress-bar-track> | ||
<div | ||
data-fs-rating-distribution-item-progress-bar-fill | ||
style={{ width: `${value}%` }} | ||
/> | ||
</div> | ||
</div> | ||
) | ||
|
||
const ItemPercentage = ({ percentage }: { percentage: number }) => ( | ||
<p data-fs-rating-distribution-item-percentage>{percentage}%</p> | ||
) | ||
|
||
const RatingDistributionItem = forwardRef< | ||
HTMLLIElement, | ||
RatingDistributionItemProps | ||
>(function RatingDistributionItem( | ||
{ ratingIndex, value, testId = 'fs-rating-distribution-item' }, | ||
ref | ||
) { | ||
return ( | ||
<li ref={ref} data-fs-distribution-item data-testid={testId}> | ||
<ItemStar ratingIndex={ratingIndex} /> | ||
<ItemProgressBar value={value} /> | ||
<ItemPercentage percentage={value} /> | ||
</li> | ||
) | ||
}) | ||
export default RatingDistributionItem |
124 changes: 124 additions & 0 deletions
124
packages/components/src/organisms/RatingSummary/RatingSummary.tsx
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,124 @@ | ||
import React, { forwardRef, type HTMLAttributes } from 'react' | ||
import Rating from '../../molecules/Rating' | ||
import Button from '../../atoms/Button' | ||
import RatingDistribution from './RatingDistribution' | ||
|
||
export interface RatingSummaryProps extends HTMLAttributes<HTMLDivElement> { | ||
/** | ||
* The average rating of the product | ||
*/ | ||
average: number | ||
/** | ||
* The total number of reviews of the product | ||
*/ | ||
totalCount: number | ||
/** | ||
* The distribution of the ratings | ||
*/ | ||
distribution: { | ||
starsOne: number | ||
starsTwo: number | ||
starsThree: number | ||
starsFour: number | ||
starsFive: number | ||
} | ||
textLabels?: { | ||
ratingCounter?: { | ||
noReviewsText?: string | ||
singleReviewText?: string | ||
multipleReviewsText?: string | ||
} | ||
createReviewButton?: { | ||
noReviewsText?: string | ||
defaultText?: string | ||
} | ||
} | ||
/** | ||
* Optional test ID for testing. | ||
*/ | ||
testId?: string | ||
} | ||
|
||
const RatingSummaryHeader = ({ | ||
average, | ||
totalCount, | ||
noReviewsText, | ||
singleReviewText, | ||
multipleReviewsText, | ||
}: { | ||
average: number | ||
totalCount: number | ||
noReviewsText: string | ||
singleReviewText: string | ||
multipleReviewsText: string | ||
}) => { | ||
const formattedAverage = average > 0 ? average.toPrecision(2) : '' | ||
const totalCountText = | ||
totalCount > 0 | ||
? `${totalCount} ${totalCount === 1 ? singleReviewText : multipleReviewsText}` | ||
: noReviewsText | ||
|
||
return ( | ||
<div data-fs-rating-summary-header> | ||
<h2 data-fs-rating-summary-header-average>{formattedAverage}</h2> | ||
<Rating value={average} /> | ||
<p data-fs-rating-summary-header-total-count>{totalCountText}</p> | ||
</div> | ||
) | ||
} | ||
|
||
export const RatingSummary = forwardRef<HTMLDivElement, RatingSummaryProps>( | ||
function RatingSummary( | ||
{ | ||
average, | ||
totalCount, | ||
distribution, | ||
textLabels: { | ||
ratingCounter: { | ||
noReviewsText: ratingCounterNoReviewsText = 'No reviews yet', | ||
singleReviewText: ratingCounterSingleReviewText = 'Review', | ||
multipleReviewsText: ratingCounterMultipleReviewsText = 'Reviews', | ||
} = {}, | ||
createReviewButton: { | ||
noReviewsText: | ||
createReviewButtonNoReviewsText = 'Write the first review', | ||
defaultText: createReviewButtonDefaultText = 'Write a review', | ||
} = {}, | ||
} = {}, | ||
testId = 'fs-rating-summary', | ||
...props | ||
}, | ||
ref | ||
) { | ||
const buttonText = | ||
totalCount > 0 | ||
? createReviewButtonDefaultText | ||
: createReviewButtonNoReviewsText | ||
|
||
return ( | ||
<div ref={ref} data-fs-rating-summary data-testid={testId} {...props}> | ||
<RatingSummaryHeader | ||
average={average} | ||
totalCount={totalCount} | ||
noReviewsText={ratingCounterNoReviewsText} | ||
singleReviewText={ratingCounterSingleReviewText} | ||
multipleReviewsText={ratingCounterMultipleReviewsText} | ||
/> | ||
<Button | ||
variant="secondary" | ||
onClick={() => alert('Write a review button clicked!')} | ||
> | ||
{buttonText} | ||
</Button> | ||
{totalCount > 0 && ( | ||
<RatingDistribution | ||
data-fs-rating-summary-distribution | ||
distribution={distribution} | ||
/> | ||
)} | ||
</div> | ||
) | ||
} | ||
) | ||
|
||
export default RatingSummary |
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 @@ | ||
export { default, type RatingSummaryProps } from './RatingSummary' |
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
just to keep the pattern