-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathindex.jsx
105 lines (86 loc) · 2.44 KB
/
index.jsx
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
95
96
97
98
99
100
101
102
103
104
105
/** @format */
/**
* External dependencies
*/
import PropTypes from 'prop-types';
import { Component } from 'react';
import { connect } from 'react-redux';
import { debounce, isEqual } from 'lodash';
/**
* Internal dependencies.
*/
import { getDocumentHeadFormattedTitle } from 'state/document-head/selectors';
import {
setDocumentHeadTitle as setTitle,
setDocumentHeadLink as setLink,
setDocumentHeadMeta as setMeta,
setDocumentHeadUnreadCount as setUnreadCount,
} from 'state/document-head/actions';
import TranslatableString from 'components/translatable/proptype';
class DocumentHead extends Component {
componentWillMount() {
const { title, unreadCount } = this.props;
if ( this.props.title !== undefined ) {
this.props.setTitle( title );
}
if ( this.props.unreadCount !== undefined ) {
this.props.setUnreadCount( unreadCount );
}
if ( this.props.link !== undefined ) {
this.props.setLink( this.props.link );
}
if ( this.props.meta !== undefined ) {
this.props.setMeta( this.props.meta );
}
}
componentDidMount() {
this.setFormattedTitle( this.props.formattedTitle );
}
componentWillReceiveProps( nextProps ) {
if ( nextProps.title !== undefined && this.props.title !== nextProps.title ) {
this.props.setTitle( nextProps.title );
}
if ( nextProps.unreadCount !== undefined && this.props.unreadCount !== nextProps.unreadCount ) {
this.props.setUnreadCount( nextProps.unreadCount );
}
if ( nextProps.link !== undefined && ! isEqual( this.props.link, nextProps.link ) ) {
this.props.setLink( nextProps.link );
}
if ( nextProps.meta !== undefined && ! isEqual( this.props.meta, nextProps.meta ) ) {
this.props.setMeta( nextProps.meta );
}
if ( nextProps.formattedTitle !== this.props.formattedTitle ) {
this.setFormattedTitle( nextProps.formattedTitle );
}
}
componentWillUnmount() {
this.setFormattedTitle.cancel();
}
setFormattedTitle = debounce( title => {
document.title = title;
} );
render() {
return null;
}
}
DocumentHead.propTypes = {
title: TranslatableString,
unreadCount: PropTypes.number,
link: PropTypes.array,
meta: PropTypes.array,
setTitle: PropTypes.func.isRequired,
setLink: PropTypes.func.isRequired,
setMeta: PropTypes.func.isRequired,
setUnreadCount: PropTypes.func.isRequired,
};
export default connect(
state => ( {
formattedTitle: getDocumentHeadFormattedTitle( state ),
} ),
{
setTitle,
setLink,
setMeta,
setUnreadCount,
}
)( DocumentHead );