Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.10 KB | None | 0 0
  1. import React, { Component, PropTypes } from 'react';
  2. import { DataSourceType } from '../../constants/Constants';
  3. import moment from 'moment';
  4. import _ from 'lodash';
  5.  
  6. export default class DataSourceLogin extends Component {
  7. static propTypes = {
  8. children: PropTypes.node,
  9. disabled: PropTypes.bool,
  10. accessUrl: PropTypes.string,
  11. type: PropTypes.string,
  12. styling: PropTypes.string,
  13. onSuccess: PropTypes.func,
  14. onError: PropTypes.func
  15. };
  16.  
  17. static defaultProps = {
  18. disabled: false,
  19. styling: 'width=584,height=600'
  20. };
  21.  
  22. showPopup = () => {
  23. if (this.props.disabled) return;
  24.  
  25. const { accessUrl, styling, type, onSuccess, onError } = this.props;
  26. const popupWindow = window.open(accessUrl, '', styling);
  27.  
  28. const parseUrl = url => {
  29. // Ik stuur gewoon letterlijk 'error' terug in de url want in mijn geval sluit de popup toch meteen
  30. if (_.includes('error', url)) {
  31. return onError(url);
  32. }
  33.  
  34. // Haal data op van url die je nodig hebt en parse die onzin
  35. const splitted = _.split(url, '/');
  36. const length = splitted.length;
  37.  
  38. if (type === DataSourceType.MAIL_CHIMP) {
  39. return {
  40. accessToken: splitted[length - 2],
  41. dataCenter: splitted[length - 1],
  42. dateCreated: moment.utc().format()
  43. };
  44. }
  45.  
  46. return {
  47. accessToken: splitted[length - 2].replace('TYRON', '/'),
  48. refreshToken: splitted[length - 1].replace('TYRON', '/'),
  49. dateCreated: moment.utc().format()
  50. };
  51. };
  52.  
  53. // Open popup -> check url verandering -> verandering? -> handle success || error
  54. try {
  55. const interval = setInterval(() => {
  56. const href = popupWindow.location.href;
  57.  
  58. if (href) {
  59. popupWindow.close();
  60. clearInterval(interval);
  61. onSuccess(parseUrl(href));
  62. }
  63. }, 1000);
  64. } catch (error) {
  65. // Ignore error
  66. }
  67. };
  68.  
  69. render () {
  70. const { children } = this.props;
  71.  
  72. return (
  73. <div onClick={this.showPopup}>
  74. {children}
  75. </div>
  76. );
  77. }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement