This repository has been archived by the owner on Aug 24, 2020. It is now read-only.
/
app.js
206 lines (177 loc) · 5.18 KB
/
app.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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// Walkhub
// Copyright (C) 2015 Pronovix
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
import React from "react";
import App from "components/app";
import UserStore from "stores/user";
import UserActions from "actions/user";
import connectToStores from "alt/utils/connectToStores";
import flux from "control";
import {noop} from "form";
import {t, N_} from "t";
import {capitalizeFirstLetter} from "util";
import RouterActions from "actions/router";
import OuterClassActions from "actions/outerclass";
import MessageActions from "actions/message";
import WalkhubBackend from "walkthrough/walkhub_backend";
import $ from "jquery";
import NetworkActivityWrapper from "components/wrappers/networkactivity";
import WalkthroughActions from "actions/walkthrough";
let FooterComponent = null;
let AnnouncementComponent = null;
if (WALKHUB_CUSTOM_FOOTER) {
FooterComponent = require("FOOTER");
}
if (WALKHUB_ANNOUNCEMENT) {
AnnouncementComponent = require("ANNOUNCEMENT");
}
let menuItems = {
navbar: {
left: [
{path: "https://github.com/Pronovix/walkhub-service", label: N_("Download from GitHub")},
],
right: [
{path: "/search", label: N_("Search")},
{path: "/profile/me", label: N_("My Profile"), loggedin: true},
{path: "/record", label: N_("Record"), loggedin: true},
{path: "/connect", icon: "log-in", loggedin: false},
{path: "/api/auth/logout?token=CSRF_TOKEN", icon: "log-out", loggedin: true},
],
},
footer: {
left: [
],
right: [
],
},
};
if (WALKHUB_MENU_ITEMS) {
menuItems = require("MENU_ITEMS");
}
@connectToStores
class AppWrapper extends React.Component {
static getStores(props) {
return [UserStore];
}
static getPropsFromStores() {
const storeState = UserStore.getState();
return {
currentUser: storeState.users[storeState.currentUser] || {},
};
}
static contextTypes = {
location: React.PropTypes.shape,
history: React.PropTypes.shape,
};
state = {
messages: [],
classes: {},
}
dispatcherToken = null;
componentDidMount() {
UserStore.performLoad(null);
this.dispatcherToken = flux.dispatcher.register(this.onChange);
}
componentWillUnmount() {
if (this.dispatcherToken) {
flux.dispatcher.unregister(this.dispatcherToken);
}
}
blacklistedErrors() {
return [
WalkthroughActions.LOADING_SCREENING_FAILED,
WalkthroughActions.RECEIVING_SITEINFO_FAILED,
];
}
onChange = (event) => {
const messages = this.state.messages;
let classes = this.state.classes;
// handle messages
if (event.action === MessageActions.FLASH_MESSAGE) {
messages.push({
type: "success",
message: event.data,
key: Math.random().toString(),
});
}
// error, failed
if (event.action.match(/(error|fail)/i) && this.blacklistedErrors().indexOf(event.action) === -1) {
if (event.data && event.data.status && !(event.data.config.method === "get" && event.data.config.url === "/api/user")) { // Network error, but not related to the current user.
messages.push({
type: "danger",
message: t(capitalizeFirstLetter(event.data.data.message)),
key: Math.random().toString(),
});
$("html, body").animate({scrollTop: 0}, "slow");
}
}
if (event.action === OuterClassActions.CHANGE_OUTER_CLASSES) {
Object.keys(event.data).map((k) => {
const v = event.data[k];
if (v) {
classes[k] = v;
} else {
delete classes[k];
}
});
}
this.setState({
messages: messages,
classes: classes,
});
};
onMessageClose = (evt) => {
noop(evt);
const key = evt.target.dataset.key;
const messages = this.state.messages.filter(function(msg) {
return msg.key !== key;
});
this.setState({messages: messages});
};
getContainerClassName() {
const path = this.context.location.pathname;
if (path === "/" || !path) {
return "frontpage";
}
return path.split("/")[1];
}
render() {
let className = "app";
Object.keys(this.state.classes).map((k) => {
className += ` ${k}-${this.state.classes[k]}`;
});
const footer = FooterComponent ? <FooterComponent /> : null;
const announcement = AnnouncementComponent ? <AnnouncementComponent /> : null;
return (
<App
currentUser={this.props.currentUser}
embedded={!!this.context.location.query.embedded}
messages={this.state.messages}
onMessageClose={this.onMessageClose}
navbarConfig={menuItems.navbar}
footerConfig={menuItems.footer}
className={className}
containerClassName={this.getContainerClassName()}
footer={footer}
announcement={announcement}
navbarIsFixedTop={true}
>
<NetworkActivityWrapper />
{this.props.children}
</App>
);
}
}
export default AppWrapper;