File size: 2,964 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// @flow
import React from 'react';
import compose from 'recompose/compose';
import { withRouter, type Location, type History } from 'react-router';
import querystring from 'querystring';
import { connect } from 'react-redux';
import type { Dispatch } from 'redux';
import { addToastWithTimeout } from 'src/actions/toasts';

type Props = {
  location: Location,
  history: History,
  dispatch: Dispatch<Object>,
};

class QueryParamToastDispatcher extends React.Component<Props> {
  getParams = (props: Props) => {
    return querystring.parse(props.location.search.replace('?', ''));
  };

  componentDidMount() {
    const params = this.filterToastParams(this.getParams(this.props));
    if (this.hasValidToastParams(params)) {
      this.props.dispatch(
        addToastWithTimeout(params.toastType, params.toastMessage)
      );
      return this.cleanLocation();
    }
  }

  componentDidUpdate(prevProps: Props) {
    const currProps = this.props;
    const prevParams = this.filterToastParams(this.getParams(prevProps));
    const currParams = this.filterToastParams(this.getParams(currProps));

    const currValid = this.hasValidToastParams(currParams);
    if (!currValid) return;

    if (prevParams.toastMessage !== currParams.toastMessage) {
      currProps.dispatch(
        addToastWithTimeout(currParams.toastType, currParams.toastMessage)
      );
      return this.cleanLocation();
    }
  }

  hasValidToastParams = (params: Object) => {
    const validToastTypes = ['success', 'error', 'neutral'];
    return (
      params.toastType &&
      validToastTypes.indexOf(params.toastType) >= 0 &&
      params.toastMessage
    );
  };

  /*
    There could be many parameters besides toastMessage and toastType.
    This component only cares about changes to these two specific params though,
    so we can filter down to an object with only what we need and use that to
    determine whether we need to dispatch a new toast or not
  */
  filterToastParams = (params: Object) => ({
    toastMessage: params['toastMessage'],
    toastType: params['toastType'],
  });

  cleanLocation = () => {
    const params = this.getParams(this.props);
    const clean = {};
    Object.keys(params).map(key => {
      if (!key || key === 'toastMessage' || key === 'toastType') return null;
      clean[key] = params[key];
      return null;
    });
    const cleanParams = querystring.stringify(clean);
    /*
      We decode the cleanParams in order to preserver special characters in the url
      For example, the url /spectrum/general/another-thread~thread-2?m=MTQ4MzIyNTIwMDAwMg== 
      has two equals signs at the end. If we don't decode the cleanParams it will become
      spectrum/general/another-thread~thread-2?m=MTQ4MzIyNTIwMDAwMg%3D%3D
    */
    return this.props.history.push({ search: decodeURIComponent(cleanParams) });
  };

  render() {
    return null;
  }
}

export default compose(
  withRouter,
  connect()
)(QueryParamToastDispatcher);