Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. import React, { Component } from 'react';
  2. import { PropTypes } from 'prop-types';
  3.  
  4. const randomKey = () => Math.random().toString(36).substring(7);
  5.  
  6. export class Findify extends Component{
  7. container = undefined;
  8. findifyKey = undefined;
  9.  
  10. state = {
  11. isLoaded: false,
  12. }
  13.  
  14. static propTypes = {
  15. widgetKey: PropTypes.string,
  16. type: PropTypes.oneOf(['search', 'recommendation', 'autocomplete']),
  17. options: PropTypes.object
  18. }
  19.  
  20. componentWillUnmount() {
  21. if (this.findifyKey) {
  22. window.findify.widgets.detach(this.findifyKey);
  23. }
  24. }
  25.  
  26. handleFirstResponse = (items) => {
  27. this.setState({ isLoaded: true });
  28. window.findify.widgets.get(this.findifyKey).agent.off(this.handleFirstResponse)
  29. }
  30.  
  31. register = (container) => {
  32. if (this.container || !container) return;
  33. this.container = container;
  34. this.renderFindify(container);
  35. }
  36.  
  37. renderFindify = (container) => {
  38. const { widgetKey, type, options } = this.props;
  39. this.findifyKey = widgetKey || randomKey();
  40. window.findify.widgets.attach(container, type, { ...options, widgetKey: this.findifyKey });
  41. window.findify.widgets.get(this.findifyKey).agent.on('change:items', this.handleFirstResponse);
  42. }
  43.  
  44. render() {
  45. const { isLoaded } = this.state;
  46.  
  47. return (
  48. <>
  49. { !isLoaded && <div className='YOUR_LOADER' /> }
  50. <div ref={this.register} />
  51. </>
  52. )
  53. }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement