-
Notifications
You must be signed in to change notification settings - Fork 337
/
ActionContainer.js
58 lines (52 loc) · 1.73 KB
/
ActionContainer.js
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
import PropTypes from 'prop-types';
import React, { Component } from 'react';
import Action from 'universal/components/Action/Action';
import injectGlobals from 'universal/styles/hepha';
import globalStyles from 'universal/styles/theme/globalStyles';
import {segmentEventPage} from 'universal/redux/segmentActions';
import {withRouter} from 'react-router-dom';
const updateAnalyticsPage = (dispatch, lastPage, nextPage) => {
if (typeof document === 'undefined' || typeof window.analytics === 'undefined') return;
const name = document && document.title || '';
const properties = {
title: name,
referrer: lastPage,
path: nextPage
};
dispatch(segmentEventPage(name, null, properties));
};
@withRouter
export default class ActionContainer extends Component {
static contextTypes = {
store: PropTypes.object
};
static propTypes = {
dispatch: PropTypes.func,
location: PropTypes.shape({
pathname: PropTypes.string.isRequired
}).isRequired,
params: PropTypes.object
};
componentWillMount() {
const {dispatch} = this.context.store;
const {location: {pathname: nextPage}} = this.props;
updateAnalyticsPage(dispatch, '', nextPage);
injectGlobals(globalStyles);
}
componentDidUpdate(prevProps) {
const {location: {pathname: lastPage}} = prevProps;
const {location: {pathname: nextPage}} = this.props;
if (lastPage !== nextPage) {
const {dispatch} = this.context.store;
/*
* Perform page update after component renders. That way,
* document.title will be current after any child <Helmet />
* element(s) are rendered.
*/
updateAnalyticsPage(dispatch, lastPage, nextPage);
}
}
render() {
return <Action {...this.props} />;
}
}