-
Notifications
You must be signed in to change notification settings - Fork 157
/
AppBody.jsx
129 lines (102 loc) · 3.09 KB
/
AppBody.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
const {
Link,
Navigation,
State,
RouteHandler
} = ReactRouter;
// true if we should show an error dialog when there is a connection error.
// Exists so that we don't show a connection error dialog when the app is just
// starting and hasn't had a chance to connect yet.
const ShowConnectionIssues = new ReactiveVar(false);
const CONNECTION_ISSUE_TIMEOUT = 5000;
// Only show the connection error box if it has been 5 seconds since
// the app started
setTimeout(function () {
// Show the connection error box
ShowConnectionIssues.set(true);
}, CONNECTION_ISSUE_TIMEOUT);
// This component handles making the subscriptons to globally necessary data,
// handling router transitions based on that data, and rendering the basid app
// layout
AppBody = React.createClass({
mixins: [ReactMeteorData, Navigation, State],
getInitialState() {
return {
menuOpen: false
};
},
childContextTypes: {
toggleMenuOpen: React.PropTypes.func.isRequired
},
getChildContext() {
return {
toggleMenuOpen: this.toggleMenuOpen
}
},
getMeteorData() {
const subHandles = [
Meteor.subscribe("publicLists"),
Meteor.subscribe("privateLists")
];
const subsReady = _.all(subHandles, function (handle) {
return handle.ready();
});
// Get the current routes from React Router
const routes = this.getRoutes();
// If we are at the root route, and the subscrioptions are ready
if (routes.length > 1 && routes[1].isDefault && subsReady) {
// Redirect to the route for the first todo list
this.replaceWith("todoList", { listId: Lists.findOne()._id });
}
return {
subsReady: subsReady,
lists: Lists.find({}, { sort: {createdAt: -1} }).fetch(),
currentUser: Meteor.user(),
disconnected: ShowConnectionIssues.get() && (! Meteor.status().connected)
};
},
toggleMenuOpen() {
this.setState({
menuOpen: ! this.state.menuOpen
});
},
addList() {
Meteor.call("/lists/add", (err, res) => {
if (err) {
// Not going to be too fancy about error handling in this example app
alert("Error creating list.");
return;
}
// Go to the page for the new list
this.transitionTo('todoList', { listId: res });
});
},
getListId() {
return this.getParams().listId;
},
render() {
let appBodyContainerClass = "";
if (Meteor.isCordova) {
appBodyContainerClass += " cordova";
}
if (this.state.menuOpen) {
appBodyContainerClass += " menu-open";
}
return (
<div id="container" className={ appBodyContainerClass }>
<LeftPanel
currentUser={this.data.currentUser}
onAddList={this.addList}
lists={this.data.lists}
activeListId={this.getListId()} />
{ this.data.disconnected ? <ConnectionIssueDialog /> : "" }
<div className="content-overlay" onClick={ this.toggleMenuOpen }></div>
<div id="content-container">
{ this.data.subsReady ?
<RouteHandler /> :
<AppLoading /> }
</div>
</div>
);
}
});