Ratings React.JS Component by #HexiPi
Installation:
Note: This module requires bootstrap & font awesome for optimal CSS styling.
npm install ratings-hexipi bootstrap reactstrap font-awesome --save
OR
yarn add ratings-hexipi bootstrap reactstrap font-awesome
Example of Usage:
import React, { Component } from 'react';
import RatingSection, { RatingsSummary, CommentRes,
defaultRatingPercentages } from 'ratings-hexipi';
import 'bootstrap/dist/css/bootstrap.css';
import 'font-awesome/css/font-awesome.min.css';
import './App.css';
class App extends Component {
state = {
comments: [],
commentSubmitRes: CommentRes.NONE,
};
calculateRatingPercentages = () => {
const ratingsCount = { ...defaultRatingPercentages };
const ratingPercentages = { ...defaultRatingPercentages };
const totalNumberOfComments = this.state.comments.length;
this.state.comments.forEach(comment => {
const rating = comment.commenter_rating.toString();
ratingsCount[rating] = Number(ratingsCount[rating]) + 1;
});
Object.keys(ratingsCount).forEach((key, _) => {
const ratingCount = ratingsCount[key];
const percentageCalc =
(Number(ratingCount) / totalNumberOfComments) * 100;
ratingPercentages[key] = (percentageCalc) ? percentageCalc : 0;
});
return ratingPercentages;
}
addComment = commentInfo => {
try {
const comments = this.state.comments;
commentInfo.timestamp = new Date().getTime();
comments.push(commentInfo);
this.setState({
comments: comments,
commentSubmitRes: CommentRes.OK
});
}
catch(e) {
this.setState({
commentSubmitRes: CommentRes.ERROR,
});
}
}
commentSubmitResultReset = () =>
this.setState({ commentSubmitRes: CommentRes.NONE });
render() {
return(
<div>
<RatingsSummary
ratingPercentages={this.calculateRatingPercentages()}
numberOfReviews={this.state.comments.length}
/>
<RatingSection
submitMethod="post"
commentsData={this.state.comments}
addComment={this.addComment}
commentSubmitResult={this.state.commentSubmitRes}
commentSubmitResultReset={this.commentSubmitResultReset}
/>
</div>
);
}
}
export default App;
Attributes & Data Types for RatingSection:
Below is a list of all the available attributes:
interface RatingSectionProps {
submitMethod: 'get' | 'post',
heading?: string,
subHeading?: string,
commentsData: CommentData[],
commentSubmitOKMsg?: string,
commentSubmitErrorMsg?: string,
commentSubmitResult: CommentRes,
backgroundColor?: string,
isAdmin?: Boolean,
addComment: (comment: CommentData) => void,
commentSubmitResultReset: () => void,
onCommentDelete?: (comment_data: CommentData) => void
}
Note: Most attributes are technically optional since they already have default values assigned to them. However the ones that are actually optional (marked with a "?") will not be shown or used by default (with the exception of the default headings). All callback functions are required if you actually want the ratings form to work properly.
Below are all the available values of the RatingSectionProps default values:
RatingSectionProps.defaultProps = {
heading: 'Add a Review...',
subHeading: 'Let us know what you think!',
commentsData: [],
commentSubmitOKMsg: 'Review Submitted!',
commentSubmitErrorMsg: 'An error has occurred (why???)!' +
'😥 Please try again later.',
commentSubmitResult: CommentRes.NONE,
backgroundColor: 'black',
isAdmin: false
}
enum CommentRes {
OK,
ERROR,
NONE
}
interface CommentData {
commenter_name: string,
commenter_rating: number,
comment_content: string,
timestamp: number
}
Attributes & Data Types for RatingsSummary:
Below are all the available values of the RatingsSummaryProps interface:
interface RatingsSummaryProps {
ratingPercentages: RatingPercentages,
numberOfReviews: number,
backgroundColor?: string
}
Below are all the available values of the RatingsSummaryProps default values:
RatingsSummary.defaultProps = {
ratingPercentages: { '3': 30, '4': 20, '5': 50 },
numberOfReviews: 200,
backgroundColor: 'black'
}
Below are all the available values of the RatingPercentages interface:
interface RatingPercentages {
'0.5'?: number,
'1'?: number,
'1.5'?: number,
'2'?: number,
'2.5'?: number,
'3'?: number,
'3.5'?: number,
'4'?: number,
'4.5'?: number,
'5'?: number
};
Below are all the available values of the defaultRatingPercentages JSON Object:
const defaultRatingPercentages: RatingPercentages = {
'0.5': 0,
'1': 0,
'1.5': 0,
'2': 0,
'2.5': 0,
'3': 0,
'3.5': 0,
'4': 0,
'4.5': 0,
'5': 0
};