Overview
Reseñas Gastronómicas features an intuitive star rating system that allows reviewers to rate dishes on a scale of 1 to 10. The system provides real-time visual feedback and supports independent ratings from multiple reviewers.The application uses a 10-star system (1-10 scale) rather than the traditional 5-star system, allowing for more granular rating precision.
StarRating Module
The StarRating module manages all rating interactions and state:Multi-Reviewer Support
The system supports independent ratings from two reviewers (Gian and Yami):- Separate rating state
- Independent star displays
- Individual rating text display
Setting Ratings
When a user clicks on a star, the rating is set:- Clamp rating between 0 and 10
- Update internal state
- Update text display (e.g., “8/10”)
- Update visual star display
Rating Validation
- Minimum: 0
- Maximum: 10
Visual Feedback
The star rating system provides rich visual feedback through hover effects and click interactions.Hover Effect
When a user hovers over stars, they light up to preview the rating:- Stars 1-7: Add
star-filledclass (highlighted) - Stars 8-10: Add
star-emptyclass (dim)
Hover Reset
When the mouse leaves the star area, the display reverts to the current rating:CSS Classes for Stars
CSS Classes for Stars
The system uses two CSS classes to style stars:
star-filled: Bright, highlighted star (active/selected)star-empty: Dim, unhighlighted star (inactive)
Update Star Display
The core method that updates the visual appearance of stars:Event Handling
The star rating system uses three event listeners per star:Click Event
Mouseover Event
Mouseout Event
The index is 0-based, so we add 1 when setting the rating to convert to a 1-10 scale.
Reset Functionality
The system can reset all ratings to zero:- After submitting a review
- When canceling review creation
- When clearing the form
Integration with Form
The star rating system integrates seamlessly with the review form:HTML Structure
The star rating system expects a specific HTML structure:- Container with
.star-ratingclass data-reviewerattribute to identify the reviewer- 10 star elements with
.starclass - Rating display element with ID
{reviewer}Rating
Data Flow
Dynamic Rating Display
The system displays the current rating in text form:- 0 stars: “0/10”
- 5 stars: “5/10”
- 10 stars: “10/10”
Reviewer-Specific Selectors
The system uses data attributes for precise targeting:- Each reviewer’s stars are controlled independently
- No cross-contamination between reviewers
- Clean, maintainable code
Why data-reviewer Attribute?
Why data-reviewer Attribute?
Using
data-reviewer allows us to:- Have multiple rating systems on the same page
- Target specific reviewer’s stars without complex selectors
- Easily identify which reviewer a rating belongs to
- Scale to additional reviewers if needed in the future
Accessibility Considerations
For better accessibility, consider adding:- Screen readers can announce the rating purpose
- Keyboard navigation support
- ARIA states communicate selection
Edge Cases
Rating of 0
A rating of 0 means “not rated yet”:Missing Elements
The code includes safety checks:Performance
The star rating system is highly performant:- Uses event delegation concepts with
querySelectorAll - Minimal DOM manipulation (only updates changed classes)
- No complex calculations
- State stored in simple object
Fast Updates
Visual feedback is instant with no perceptible lag
Lightweight
Minimal memory footprint and CPU usage
Scalable
Can handle multiple rating systems on one page
No Dependencies
Pure JavaScript with no external libraries required
Best Practices
Always Validate Ratings
Always Validate Ratings
Before submitting to the database, validate that ratings are within 1-10:
Reset After Submission
Reset After Submission
Always reset the star rating after successfully creating a review:
Handle Optional Reviewers
Handle Optional Reviewers
Check if a reviewer is active before requiring their rating:
Future Enhancements
Potential improvements to the star rating system:- Half-star support (e.g., 7.5 stars)
- Keyboard navigation for accessibility
- Touch gesture support for mobile devices
- Animated transitions when rating changes
- Rating explanations (1-3: Poor, 4-6: Average, 7-9: Good, 10: Excellent)
Code Reference
Key files for the star rating system:starrating.js:1-54- Complete StarRating modulestarrating.js:17-23- Rating setter methodstarrating.js:25-43- Visual feedback methodsform.js:38-50- Integration with form submission