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

add star rating play #513

Merged
merged 10 commits into from
Sep 2, 2022
27 changes: 27 additions & 0 deletions src/plays/star-rating/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Star rating

A star rating component is commonly used to give feedback and reviews on e-commerce websites

## Play Demographic

- Language: js
- Level: Beginner

## Creator Information

- User: frankiefab100
- Github Link: https://github.com/frankiefab100
- Blog: https://frankiefab.hashnode.dev/how-to-build-a-star-rating-using-usestate-hook
- Video:

## Implementation Details

Update your implementation idea and details here

## Consideration

Update all considerations(if any)
koustov marked this conversation as resolved.
Show resolved Hide resolved

## Resources
koustov marked this conversation as resolved.
Show resolved Hide resolved

Update external resources(if any)
82 changes: 82 additions & 0 deletions src/plays/star-rating/StarRating.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import PlayHeader from "common/playlists/PlayHeader";
import { useState } from "react";
import { FaStar } from "react-icons/fa";
import "./styles.css";

// WARNING: Do not change the entry componenet name
function StarRating(props) {
koustov marked this conversation as resolved.
Show resolved Hide resolved
// Your Code Start below.

const [rating, setRating] = useState(null);
const [hoverFill, setHoverFill] = useState(null);

const getReviewLabel = (rating) => {
switch (rating) {
case 1:
return `Very bad ${String.fromCodePoint("0x1F922")}`;
koustov marked this conversation as resolved.
Show resolved Hide resolved
case 2:
return `Bad ${String.fromCodePoint("0x1F97A")}`;
case 3:
return `Okay ${String.fromCodePoint("0x1F60C")}`;
case 4:
return `Good ${String.fromCodePoint("0x1F60A")}`;
case 5:
return `Excellent ${String.fromCodePoint("0x1F929")}`;

default:
return "";
}
};

return (
<>
<div className="play-details">
<PlayHeader play={props} />
<div className="play-details-body">
{/* Your Code Starts Here */}

<div className="star-wrapper">
<h2 className="review-text">
{rating
? getReviewLabel(rating)
koustov marked this conversation as resolved.
Show resolved Hide resolved
koustov marked this conversation as resolved.
Show resolved Hide resolved
: hoverFill
? getReviewLabel(hoverFill)
: null}
</h2>

<div className="star">
{[...Array(5)].map((_, index) => {
const ratingValue = index + 1;

return (
<button
key={index}
onMouseEnter={() => setHoverFill(ratingValue)}
onMouseLeave={() => setHoverFill(null)}
onClick={() => setRating(ratingValue)}
>
<FaStar
className="FaStar"
size={80}
style={{
color:
ratingValue <= (hoverFill || rating)
? "#ffe101"
: "#ccc",
}}
onChange={() => setRating(ratingValue)}
value={ratingValue}
/>
</button>
);
})}
</div>
</div>
{/* Your Code Ends Here */}
</div>
</div>
</>
);
}

export default StarRating;
Binary file added src/plays/star-rating/cover.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 30 additions & 0 deletions src/plays/star-rating/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/* enter stlyes here */
.play-details .play-details-body {
koustov marked this conversation as resolved.
Show resolved Hide resolved
display: flex;
justify-content: center;
align-items: center;
width: 100vw;
height: 100vh;
Copy link
Member

Choose a reason for hiding this comment

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

100vh and 100vw are creating scroll to the container.
Replace the height and width with

height: 100%
width: 100%

It will solve the scroll issue.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, thanks.

I wonder why it looks okay from my end.

overflow: hidden;
}

.star-wrapper {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
text-align: center;
}

.star > button {
background-color: transparent;
border: none;
cursor: pointer;
outline: none;
}

@media screen and (max-width: 450px) {
.FaStar {
width: 40px;
}
}