Advertisement
Guest User

Untitled

a guest
Oct 17th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.68 KB | None | 0 0
  1. import React from 'react';
  2. import styled from 'styled-components';
  3. import Stars from '../../atoms/stars/Stars';
  4. import RatesInfos from '../../atoms/rates-infos/RatesInfos';
  5. import Ticks from '../../atoms/ticks/Ticks';
  6.  
  7. const Div = styled.div`
  8. display: inline-flex;
  9. .stars {
  10. display: flex;
  11. .star {
  12. margin: 5px;
  13. }
  14. }
  15. .averageBlock, .userBlock {
  16. padding: 5px;
  17. display: flex;
  18.  
  19. .right {
  20. margin-left: 10px;
  21. }
  22. }
  23. .userBlock {
  24. background: #ededed;
  25. border-radius: 4px;
  26. }
  27. `;
  28.  
  29. class RatingStars extends React.Component {
  30. /*
  31. static propTypes = {
  32. average: PropTypes.number.isRequired,
  33. votesCount: PropTypes.number.isRequired
  34. }
  35. */
  36.  
  37. state = {
  38. whatToDisplay: 'averageStars',
  39. averageStars: [
  40. {
  41. value: 1,
  42. color: 'grey'
  43. },
  44. {
  45. value: 2,
  46. color: 'grey'
  47. },
  48. {
  49. value: 3,
  50. color: 'grey'
  51. },
  52. {
  53. value: 4,
  54. color: 'grey'
  55. },
  56. {
  57. value: 5,
  58. color: 'grey'
  59. }
  60. ],
  61. userRate: 0,
  62. userStars: [
  63. {
  64. value: 1,
  65. color: 'grey'
  66. },
  67. {
  68. value: 2,
  69. color: 'grey'
  70. },
  71. {
  72. value: 3,
  73. color: 'grey'
  74. },
  75. {
  76. value: 4,
  77. color: 'grey'
  78. },
  79. {
  80. value: 5,
  81. color: 'grey'
  82. }
  83. ],
  84. hasvoted: 0
  85. }
  86.  
  87. componentWillMount() {
  88. const fullStars = Math.trunc(this.state.average);
  89. const hasHalfStar = (this.state.average - fullStars) >= 0.5;
  90. const averageStars = this.state.averageStars;
  91. averageStars.map(elt => {
  92. if (elt.value <= fullStars) {
  93. elt.color = "yellow";
  94. }
  95. if (hasHalfStar && elt.value === (fullStars + 1)) {
  96. elt.color = "half";
  97. }
  98. return elt;
  99. });
  100. this.setState({ averageStars });
  101. }
  102.  
  103. handleMouseEnter = () => {
  104. this.setState({ whatToDisplay: 'userStars' });
  105. }
  106.  
  107. handleMouseLeave = () => {
  108. this.setState({ whatToDisplay: 'averageStars' });
  109. }
  110.  
  111. handleMouseEnterStar = (value) => {
  112. const userStars = this.state.userStars;
  113. userStars.map(elt => {
  114. if(elt.value <= value) {
  115. elt.color = 'blue';
  116. } else {
  117. elt.color = 'grey';
  118. }
  119. return elt;
  120. });
  121. this.setState({ userStars, userRate: value });
  122. }
  123.  
  124. handleClickStar = (value) => {
  125. //const { onStarClicked } = this.props;
  126. console.log(value);
  127. //onStarClicked && onStarClicked(value);
  128. //this.setState({ whatToDisplay: 'averageStars', hasvoted: 1 });
  129. }
  130.  
  131. render() {
  132.  
  133. return (
  134.  
  135. <Div
  136. onMouseEnter={() => this.handleMouseEnter()}
  137. onMouseLeave={() => this.handleMouseLeave()}
  138. >
  139. {this.state.whatToDisplay === 'averageStars' &&
  140. <div className="averageBlock">
  141. <div className="left">
  142. <div className="stars">
  143. {this.state.averageStars.map(rate => (
  144. <Stars
  145. className="star"
  146. key={rate.value}
  147. color={rate.color}
  148. />
  149. ))}
  150. </div>
  151. </div>
  152. <div className="right">
  153. {this.state.hasvoted === 0 &&
  154. <RatesInfos
  155. className="rates-infos"
  156. average={this.state.average}
  157. votesCount={this.state.votesCount}
  158. />
  159. }
  160. {this.state.hasvoted === 1 &&
  161. <Ticks/>
  162. }
  163. </div>
  164. </div>
  165. }
  166. {this.state.whatToDisplay === 'userStars' &&
  167. <div className="userBlock">
  168. <div className="left">
  169. <div className="stars">
  170. {this.state.userStars.map(rate => (
  171. <Stars
  172. className="star"
  173. key={rate.value}
  174. color={rate.color}
  175. onMouseEnter={() => this.handleMouseEnterStar(rate.value)}
  176. onClick={() => this.handleClickStar(rate.value)}
  177. />
  178. ))}
  179. </div>
  180. <p>Ma note est de {this.state.userRate} sur 5</p>
  181. </div>
  182. <div className="right"></div>
  183. </div>
  184. }
  185. </Div>
  186.  
  187. );
  188. }
  189. }
  190.  
  191. export default RatingStars;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement