Home Reference Source

application/components/common/quips.js

  1. import React from 'react';
  2. import _ from 'lodash';
  3.  
  4. import QUIPS from './../../data/quips';
  5.  
  6. /**
  7. * Quip component, mostly for fun and personality.
  8. *
  9. * Quips are stored in data/quips.json.
  10. */
  11. class Quips extends React.Component {
  12. constructor(props) {
  13. super(props);
  14. this.displayName = 'Quips';
  15.  
  16. this.handleClick = this.handleClick.bind(this);
  17.  
  18. this.interval;
  19. this.last;
  20. }
  21.  
  22. componentWillMount() {
  23. let key = _.random(0, QUIPS.length-1);
  24. if (this.last) {
  25. while (key === this.last) {
  26. key = _.random(0, QUIPS.length-1);
  27. }
  28. }
  29.  
  30. const quip = QUIPS[key];
  31.  
  32. this.last = key;
  33.  
  34. this.setState({quip: quip, activeClass: ''});
  35.  
  36. if (this.props.randomize) {
  37. this.interval = setInterval(this.onUpdate.bind(this), this.props.randomize);
  38. }
  39. }
  40.  
  41. onUpdate() {
  42. let key = _.random(0, QUIPS.length-1);
  43. while (key === this.last) {
  44. key = _.random(0, QUIPS.length-1);
  45. }
  46.  
  47. this.last = key;
  48. const quip = QUIPS[key]
  49.  
  50. this.setState({activeClass: 'fader'});
  51.  
  52. setTimeout(() => {
  53. this.setState({quip: quip, activeClass: ''});
  54. }, 400);
  55. }
  56.  
  57. handleClick(e) {
  58. if (this.interval) {
  59. clearInterval(this.interval);
  60. this.interval = setInterval(this.onUpdate.bind(this), this.props.randomize);
  61. }
  62.  
  63. this.onUpdate();
  64. }
  65.  
  66. render() {
  67. const dhanjerruz = () => {
  68. return {
  69. '__html': this.state.quip
  70. };
  71. };
  72. return <div className={this.props.className ? this.props.className : 'quip'} onClick={this.handleClick}><span className={this.state.activeClass} dangerouslySetInnerHTML={dhanjerruz()} /></div>
  73. }
  74. }
  75.  
  76. export default Quips;