Skip to content

Commit 1ff1057

Browse files
refactor: use new React context API (#1948)
1 parent 58f2b66 commit 1ff1057

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+334
-471
lines changed

src/components/CategoryList/CategoryList.react.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@
55
* This source code is licensed under the license found in the LICENSE file in
66
* the root directory of this source tree.
77
*/
8-
import PropTypes from 'lib/PropTypes';
9-
import React from 'react';
10-
import styles from 'components/CategoryList/CategoryList.scss';
11-
import { Link } from 'react-router-dom';
8+
import PropTypes from 'lib/PropTypes';
9+
import React from 'react';
10+
import styles from 'components/CategoryList/CategoryList.scss';
11+
import { Link } from 'react-router-dom';
12+
import generatePath from 'lib/generatePath';
13+
import { CurrentApp } from 'context/currentApp';
1214

1315
export default class CategoryList extends React.Component {
16+
static contextType = CurrentApp;
1417
constructor() {
1518
super();
1619
this.listWrapperRef = React.createRef();
@@ -64,7 +67,8 @@ export default class CategoryList extends React.Component {
6467
}
6568
let count = c.count;
6669
let className = id === this.props.current ? styles.active : '';
67-
let link = this.context.generatePath(
70+
let link = generatePath(
71+
this.context,
6872
(this.props.linkPrefix || '') + (c.link || id)
6973
);
7074
return (
@@ -84,7 +88,3 @@ CategoryList.propTypes = {
8488
current: PropTypes.string.describe('Id of current category to be highlighted.'),
8589
linkPrefix: PropTypes.string.describe('Link prefix used to generate link path.'),
8690
};
87-
88-
CategoryList.contextTypes = {
89-
generatePath: PropTypes.func
90-
};

src/components/Filter/Filter.react.js

+9-12
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
* This source code is licensed under the license found in the LICENSE file in
66
* the root directory of this source tree.
77
*/
8-
import * as Filters from 'lib/Filters';
9-
import { List, Map } from 'immutable';
10-
import PropTypes from 'lib/PropTypes';
11-
import React from 'react';
12-
import stringCompare from 'lib/stringCompare';
13-
import ParseApp from 'lib/ParseApp';
8+
import * as Filters from 'lib/Filters';
9+
import { List, Map } from 'immutable';
10+
import PropTypes from 'lib/PropTypes';
11+
import React from 'react';
12+
import stringCompare from 'lib/stringCompare';
13+
import { CurrentApp } from 'context/currentApp';
1414

1515
function changeField(schema, filters, index, newField) {
1616
let newFilter = new Map({
@@ -44,7 +44,8 @@ function deleteRow(filters, index) {
4444
return filters.delete(index);
4545
}
4646

47-
let Filter = ({ schema, filters, renderRow, onChange, blacklist, className }, context) => {
47+
let Filter = ({ schema, filters, renderRow, onChange, blacklist, className }) => {
48+
const currentApp = React.useContext(CurrentApp);
4849
blacklist = blacklist || [];
4950
let available = Filters.availableFilters(schema, filters);
5051
return (
@@ -60,7 +61,7 @@ let Filter = ({ schema, filters, renderRow, onChange, blacklist, className }, co
6061
}
6162

6263
// Get the column preference of the current class.
63-
const currentColumnPreference = context.currentApp.columnPreference[className];
64+
const currentColumnPreference = currentApp.columnPreference[className];
6465

6566
// Check if the preference exists.
6667
if (currentColumnPreference) {
@@ -137,7 +138,3 @@ Filter.propTypes = {
137138
'A function for rendering a row of a filter.'
138139
)
139140
};
140-
141-
Filter.contextTypes = {
142-
currentApp: PropTypes.instanceOf(ParseApp)
143-
};

src/components/Popover/Popover.react.js

-8
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,9 @@
55
* This source code is licensed under the license found in the LICENSE file in
66
* the root directory of this source tree.
77
*/
8-
import PropTypes from 'lib/PropTypes';
98
import hasAncestor from 'lib/hasAncestor';
109
import React from 'react';
1110
import styles from 'components/Popover/Popover.scss';
12-
import ParseApp from 'lib/ParseApp';
1311
import { createPortal } from 'react-dom';
1412

1513
// We use this component to proxy the current tree's context
@@ -94,9 +92,3 @@ export default class Popover extends React.Component {
9492
return createPortal(this.props.children, this._popoverLayer);
9593
}
9694
}
97-
98-
Popover.contextTypes = {
99-
history: PropTypes.object,
100-
router: PropTypes.object,
101-
currentApp: PropTypes.instanceOf(ParseApp)
102-
};

src/components/PushAudienceDialog/PushAudienceDialog.react.js

+4-7
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ import Label from 'components/Label/Label.react';
1717
import Modal from 'components/Modal/Modal.react';
1818
import MultiSelect from 'components/MultiSelect/MultiSelect.react';
1919
import MultiSelectOption from 'components/MultiSelect/MultiSelectOption.react';
20-
import ParseApp from 'lib/ParseApp';
2120
import PropTypes from 'lib/PropTypes';
2221
import queryFromFilters from 'lib/queryFromFilters';
2322
import React from 'react';
2423
import styles from 'components/PushAudienceDialog/PushAudienceDialog.scss';
2524
import TextInput from 'components/TextInput/TextInput.react';
2625
import Toggle from 'components/Toggle/Toggle.react';
2726
import { List, Map } from 'immutable';
27+
import { CurrentApp } from 'context/currentApp';
2828

2929
const PARSE_SERVER_SUPPORTS_SAVED_AUDIENCES = true;
3030
const AUDIENCE_SIZE_FETCHING_ENABLED = true;
@@ -44,6 +44,7 @@ let filterFormatter = (filters, schema) => {
4444
}
4545

4646
export default class PushAudienceDialog extends React.Component {
47+
static contextType = CurrentApp;
4748
constructor() {
4849
super();
4950
this.xhrHandle = null;
@@ -113,7 +114,7 @@ export default class PushAudienceDialog extends React.Component {
113114
}
114115

115116
fetchAudienceSize() {
116-
if (!this.context || !this.context.currentApp) { //so we don't break the PIG demo
117+
if (!this.context) { //so we don't break the PIG demo
117118
return;
118119
}
119120

@@ -124,7 +125,7 @@ export default class PushAudienceDialog extends React.Component {
124125
query = parseQuery.toJSON().where || {};
125126
}
126127
query.deviceType = { $in: this.state.platforms };
127-
let {xhr, promise} = this.context.currentApp.fetchPushSubscriberCount(PushConstants.NEW_SEGMENT_ID, query);
128+
let {xhr, promise} = this.context.fetchPushSubscriberCount(PushConstants.NEW_SEGMENT_ID, query);
128129
if (this.xhrHandle) { //cancel existing xhr - prevent from stacking
129130
this.xhrHandle.abort();
130131
}
@@ -272,10 +273,6 @@ export default class PushAudienceDialog extends React.Component {
272273
}
273274
}
274275

275-
PushAudienceDialog.contextTypes = {
276-
currentApp: PropTypes.instanceOf(ParseApp)
277-
};
278-
279276
PushAudienceDialog.propTypes = {
280277
editMode: PropTypes.bool.describe(
281278
'Flag if true to be edit mode of dialog.'

src/components/PushAudiencesSelector/PushAudiencesBaseRow.react.js

+4-8
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
* This source code is licensed under the license found in the LICENSE file in
66
* the root directory of this source tree.
77
*/
8-
import PropTypes from 'lib/PropTypes';
9-
import ParseApp from 'lib/ParseApp';
108
import React from 'react';
119
import { NEW_SEGMENT_ID } from 'dashboard/Push/PushConstants';
10+
import { CurrentApp } from 'context/currentApp';
1211

1312
export default class PushAudiencesBaseRow extends React.Component {
13+
static contextType = CurrentApp;
1414
constructor() {
1515
super();
1616
this.xhrHandle = null;
@@ -31,12 +31,12 @@ export default class PushAudiencesBaseRow extends React.Component {
3131
}
3232

3333
fetchPushSubscriberCount(context) {
34-
if (!context || !context.currentApp) { //so we don't break the PIG demo
34+
if (!context) { //so we don't break the PIG demo
3535
return;
3636
}
3737
let query = this.props.id === NEW_SEGMENT_ID ? this.props.query : null;
3838
//Added count fetch logic directly to component
39-
let {xhr, promise} = context.currentApp.fetchPushSubscriberCount(this.props.id, query);
39+
let {xhr, promise} = context.fetchPushSubscriberCount(this.props.id, query);
4040
this.xhrHandle = xhr;
4141
promise.then(({ approximate, count }) => {
4242
this.setState({ approximate, count });
@@ -62,7 +62,3 @@ export default class PushAudiencesBaseRow extends React.Component {
6262
}
6363
}
6464
}
65-
66-
PushAudiencesBaseRow.contextTypes = {
67-
currentApp: PropTypes.instanceOf(ParseApp)
68-
};

src/components/PushPreview/PushPreview.react.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import React from 'react';
1111
import SegmentSelect from 'components/SegmentSelect/SegmentSelect.react';
1212
import styles from 'components/PushPreview/PushPreview.scss';
1313
import VisiblePreview from 'components/PushPreview/VisiblePreview.react';
14+
import { CurrentApp } from 'context/currentApp';
1415
import {
1516
getDateMethod,
1617
MONTHS,
@@ -37,6 +38,7 @@ let timeString = (time, isLocal) => {
3738
}
3839

3940
export default class PushPreview extends React.Component {
41+
static contextType = CurrentApp;
4042
constructor(props) {
4143
super(props);
4244

@@ -134,7 +136,7 @@ export default class PushPreview extends React.Component {
134136
type={this.state.currentPreview.toLowerCase().replace(/\s/, '')}
135137
message={previewMessage}
136138
time={previewTime || new Date()}
137-
appName={this.context.currentApp.name}
139+
appName={this.context.name}
138140
fade={isExperiment} />
139141
);
140142
if (!isExperiment && pushState.data_type === 'json') {
@@ -177,7 +179,3 @@ export default class PushPreview extends React.Component {
177179
);
178180
}
179181
}
180-
181-
PushPreview.contextTypes = {
182-
currentApp: PropTypes.instanceOf(ParseApp)
183-
};

src/components/Sidebar/Sidebar.react.js

+4-8
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,18 @@
55
* This source code is licensed under the license found in the LICENSE file in
66
* the root directory of this source tree.
77
*/
8-
import PropTypes from 'lib/PropTypes';
98
import AppsManager from 'lib/AppsManager';
109
import AppsMenu from 'components/Sidebar/AppsMenu.react';
1110
import AppName from 'components/Sidebar/AppName.react';
1211
import FooterMenu from 'components/Sidebar/FooterMenu.react';
1312
import isInsidePopover from 'lib/isInsidePopover';
14-
import ParseApp from 'lib/ParseApp';
1513
import Pin from 'components/Sidebar/Pin.react';
16-
import React, { useEffect, useState } from 'react';
14+
import React, { useEffect, useState, useContext } from 'react';
1715
import SidebarHeader from 'components/Sidebar/SidebarHeader.react';
1816
import SidebarSection from 'components/Sidebar/SidebarSection.react';
1917
import SidebarSubItem from 'components/Sidebar/SidebarSubItem.react';
2018
import styles from 'components/Sidebar/Sidebar.scss';
19+
import { CurrentApp } from 'context/currentApp';
2120

2221
const Sidebar = ({
2322
prefix,
@@ -30,7 +29,8 @@ const Sidebar = ({
3029
appSelector,
3130
primaryBackgroundColor,
3231
secondaryBackgroundColor
33-
}, { currentApp }) => {
32+
}) => {
33+
const currentApp = useContext(CurrentApp);
3434
const collapseWidth = 980;
3535
const [ appsMenuOpen, setAppsMenuOpen ] = useState(false);
3636
const [ collapsed, setCollapsed ] = useState(false);
@@ -192,8 +192,4 @@ const Sidebar = ({
192192
);
193193
}
194194

195-
Sidebar.contextTypes = {
196-
currentApp: PropTypes.instanceOf(ParseApp)
197-
};
198-
199195
export default Sidebar;

src/context/currentApp.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import React from 'react';
2+
3+
export const CurrentApp = React.createContext(null);

src/dashboard/Analytics/Explorer/Explorer.react.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,9 @@ class Explorer extends DashboardView {
8585
this.xhrHandles.forEach(xhr => xhr && xhr.abort());
8686
}
8787

88-
componentWillReceiveProps(nextProps, nextContext) {
89-
if (this.context !== nextContext) {
90-
if (this.props.params.displayType !== nextProps.params.displayType) {
91-
this.setState({ activeQueries: [], mutated: false });
92-
}
88+
componentWillReceiveProps(nextProps) {
89+
if (this.props.params.displayType !== nextProps.params.displayType) {
90+
this.setState({ activeQueries: [], mutated: false });
9391
nextProps.customQueries.dispatch(ActionTypes.LIST);
9492
nextProps.customQueries.dispatch(ActionTypes.LIST_RECENT);
9593
}
@@ -173,7 +171,7 @@ class Explorer extends DashboardView {
173171
to: this.state.dateRange.end.getTime() / 1000
174172
};
175173

176-
let abortableRequest = this.context.currentApp.getAnalyticsTimeSeries(payload);
174+
let abortableRequest = this.context.getAnalyticsTimeSeries(payload);
177175
promise = abortableRequest.promise.then((result) => {
178176
let activeQueries = this.state.activeQueries;
179177
activeQueries[i].result = result.map((point) => (

src/dashboard/Analytics/Overview/Overview.react.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,12 @@ export default class Overview extends DashboardView {
100100
}
101101

102102
componentWillMount() {
103-
this.fetchOverview(this.context.currentApp);
103+
this.fetchOverview(this.context);
104104
}
105105

106106
componentWillReceiveProps(nextProps, nextContext) {
107107
if (this.context !== nextContext) {
108-
this.fetchOverview(nextContext.currentApp);
108+
this.fetchOverview(nextContext);
109109
}
110110
}
111111

src/dashboard/Analytics/Performance/Performance.react.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ export default class Performance extends DashboardView {
102102
}
103103

104104
componentWillMount() {
105-
this.handleRunQuery(this.context.currentApp);
105+
this.handleRunQuery(this.context);
106106
}
107107

108108
componentWillUnmount() {
@@ -111,7 +111,7 @@ export default class Performance extends DashboardView {
111111

112112
componentWillReceiveProps(nextProps, nextContext) {
113113
if (this.context !== nextContext) {
114-
this.handleRunQuery(nextContext.currentApp);
114+
this.handleRunQuery(nextContext);
115115
}
116116
}
117117

@@ -187,7 +187,7 @@ export default class Performance extends DashboardView {
187187
<Button
188188
primary={true}
189189
disabled={!this.state.mutated}
190-
onClick={this.handleRunQuery.bind(this, this.context.currentApp)}
190+
onClick={this.handleRunQuery.bind(this, this.context)}
191191
value='Run query' />
192192
</div>
193193
</div>

src/dashboard/Analytics/Retention/Retention.react.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export default class Retention extends DashboardView {
5353
}
5454

5555
componentWillMount() {
56-
this.fetchRetention(this.context.currentApp);
56+
this.fetchRetention(this.context);
5757
}
5858

5959
componentWillUnmount() {
@@ -62,7 +62,7 @@ export default class Retention extends DashboardView {
6262

6363
componentWillReceiveProps(nextProps, nextContext) {
6464
if (this.context !== nextContext) {
65-
this.fetchRetention(nextContext.currentApp);
65+
this.fetchRetention(nextContext);
6666
}
6767
}
6868

@@ -233,7 +233,7 @@ export default class Retention extends DashboardView {
233233
<Button
234234
primary={true}
235235
disabled={!this.state.mutated}
236-
onClick={this.fetchRetention.bind(this, this.context.currentApp)}
236+
onClick={this.fetchRetention.bind(this, this.context)}
237237
value='Refresh chart' />
238238
</div>
239239
</div>

src/dashboard/Analytics/SlowQueries/SlowQueries.react.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class SlowQueries extends TableView {
6767

6868
componentWillMount() {
6969
this.fetchDropdownData(this.props);
70-
this.fetchSlowQueries(this.context.currentApp);
70+
this.fetchSlowQueries(this.context);
7171
}
7272

7373
componentWillUnmount() {
@@ -77,7 +77,7 @@ class SlowQueries extends TableView {
7777
componentWillReceiveProps(nextProps, nextContext) {
7878
if (this.context !== nextContext) {
7979
this.fetchDropdownData(nextProps);
80-
this.fetchSlowQueries(nextContext.currentApp);
80+
this.fetchSlowQueries(nextContext);
8181
}
8282
}
8383

@@ -231,7 +231,7 @@ class SlowQueries extends TableView {
231231
<Button
232232
primary={true}
233233
disabled={!this.state.mutated}
234-
onClick={this.fetchSlowQueries.bind(this, this.context.currentApp)}
234+
onClick={this.fetchSlowQueries.bind(this, this.context)}
235235
value='Run query' />
236236
)}
237237
/>

0 commit comments

Comments
 (0)