Advertisement
kotvalera83

Untitled

Jan 22nd, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. import React, { Component } from 'react';
  2. import { logger } from '@shopgate/pwa-core/helpers';
  3. import getConfig from '../getConfig';
  4. import './style.css';
  5. /**
  6. * @return {JSX}
  7. */
  8. class ReviewDisplayLists extends Component {
  9. static get config() {
  10. return getConfig();
  11. }
  12. constructor() {
  13. super();
  14. this.getReviewFromApi = this.getReviewFromApi.bind(this);
  15. // set default rating
  16. this.state = {rating: 0};
  17. }
  18. componentDidMount() {
  19. this.getReviewFromApi();
  20. }
  21. // get rating from POWERREVIEWS API
  22. async getReviewFromApi() {
  23. try {
  24. let response = await fetch('//readservices-b2c.powerreviews.com/m/'+ this.constructor.config.merchantId +'/l/'+ this.constructor.config.locale +'/product/' + this.props.productId + '/reviews?image_only=false&apikey=' + this.constructor.config.apiKey, {cors: true});
  25. let responseJson = await response.json();
  26. if(responseJson.results[0].reviews.length > 0){
  27. this.setState({rating: responseJson.results[0].rollup.average_rating});
  28. }
  29. } catch(error) {
  30. console.error(error);
  31. }
  32. }
  33. // Generate stars
  34. createStar = () => {
  35. let star = '';
  36. for (let i = 0; i < 5; i++) {
  37. if(i < this.state.rating){
  38. if( (i + 0.5) == this.state.rating){
  39. star += `<div role="radio" class="pr-star-v4-50-filled" aria-checked="false" tabindex="-1"></div>`;
  40. } else {
  41. if((i + 0.5) < this.state.rating && (i + 1) > this.state.rating){
  42. star += `<div role="radio" class="pr-star-v4-75-filled" aria-checked="false" tabindex="-1"></div>`;
  43. } else {
  44. if((i + 0.5) > this.state.rating && this.state.rating != i){
  45. star += `<div role="radio" class="pr-star-v4-25-filled" aria-checked="false" tabindex="-1"></div>`;
  46. } else {
  47. star += `<div role="radio" class="pr-star-v4-100-filled" aria-checked="false" tabindex="-1"></div>`;
  48. }
  49. }
  50. }
  51. } else {
  52. star += `<div role="radio" class="pr-star-v4-0-filled" aria-checked="false" tabindex="-1"></div>`;
  53. }
  54. }
  55. return {__html: star};
  56. }
  57.  
  58. render () {
  59. return (
  60. <div className="pr-snippet-stars-container-list" dangerouslySetInnerHTML={this.createStar()}></div>
  61. );
  62. }
  63. }
  64. export default ReviewDisplayLists;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement