-
Notifications
You must be signed in to change notification settings - Fork 4
/
App.jsx
313 lines (276 loc) · 13.9 KB
/
App.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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
import React, { Component } from 'react'
import './scss/style.scss'
import { BrowserRouter, Routes, Route, Navigate } from 'react-router-dom'
import { Loader } from 'semantic-ui-react'
import SignIn from './views/auth/SignIn'
import ActivateAccount from './views/auth/ActivateAccount'
import PasswordReset from './views/auth/PasswordReset'
import LogOut from './views/auth/LogOut'
import MainApp from './views/App'
import User from './views/User'
import Group from './views/Group'
import Groups from './views/Groups'
import GroupRequestApproved from './views/GroupRequestApproved'
import GroupRequestPending from './views/GroupRequestPending'
import GroupRequestUpdate from './views/GroupRequestUpdate'
import Settings from './views/Settings'
import SinglePost from './views/SinglePost'
import KnowledgeBase from './views/KnowledgeBase'
import SignUpWrapper from './views/auth/SignUp'
import Topic from './views/Topic'
import Error404 from './views/Error404'
import FirstLogin from './components/User/UserFirstLogin'
import CookieBanner from './components/App/AppCookieBanner'
import { AuthProvider } from './store/AuthContext'
import ConfigContext from './store/ConfigContext.jsx'
class App extends React.Component {
constructor(props) {
super(props)
this.state = {
isLoggedIn: null,
first_login: false,
loginData: [],
loadedConfig: [],
configLoaded: false,
loggedInUserIsAdmin: false,
}
this.setLoggedInStatus = this.setLoggedInStatus.bind(this)
this.handleStateChange = this.handleStateChange.bind(this)
this.loadConfig = this.loadConfig.bind(this)
}
setLoggedInStatus = (status) => {
this.setState({ isLoggedIn: status })
return true
}
loadConfig = () => {
let configHeaders = new Headers()
if (localStorage.getItem('token') !== undefined) {
configHeaders.append('Authorization', 'Bearer ' + localStorage.getItem('token'))
}
configHeaders.append('Content-Type', 'application/x-www-form-urlencoded')
let requestOptions = {
method: 'GET',
headers: configHeaders,
redirect: 'follow',
}
let responseStatus = ''
// eslint-disable-next-line no-undef
fetch(process.env.REACT_APP_API_URL + '/api/settings', requestOptions)
.then((response) => {
return response.json()
})
.then((result) => {
let resultData = {
app: {},
server: {},
analytics: {},
}
result.forEach((configItems) => {
if (configItems.config_key === 'app.name') {
resultData.app.name = configItems.config_value
}
if (configItems.config_key === 'app.logo') {
resultData.app.logo = configItems.config_value
}
if (configItems.config_key === 'app.locale') {
resultData.app.locale = configItems.config_value
}
if (configItems.config_key === 'app.url') {
resultData.app.url = configItems.config_value
}
if (configItems.config_key === 'app.registration_enabled') {
resultData.app.registration_enabled = configItems.config_value
}
if (configItems.config_key === 'app.password_reset_enabled') {
if (this.state.loggedInUserIsAdmin) {
resultData.app.password_reset_enabled = true
} else {
resultData.app.password_reset_enabled = configItems.config_value
}
}
if (configItems.config_key === 'app.group_creation_enabled') {
if (this.state.loggedInUserIsAdmin) {
resultData.app.group_creation_enabled = true
} else {
resultData.app.group_creation_enabled = configItems.config_value
}
}
if (configItems.config_key === 'app.minimum_search_length') {
resultData.app.minimum_search_length = configItems.config_value
}
if (configItems.config_key === 'app.maximum_posts_per_page') {
resultData.app.maximum_posts_per_page = configItems.config_value
}
if (configItems.config_key === 'server.api_url') {
resultData.server.api_url = configItems.config_value
}
if (configItems.config_key === 'analytics.google_analytics.enabled') {
resultData.analytics.google_analytics_enabled = configItems.config_value
}
if (configItems.config_key === 'analytics.google_analytics.key') {
resultData.analytics.google_analytics_key = configItems.config_value
}
if (configItems.config_key === 'analytics.sentry.enabled') {
resultData.analytics.sentry_enabled = configItems.config_value
}
if (configItems.config_key === 'analytics.sentry.dsn') {
resultData.analytics.sentry_dsn = configItems.config_value
}
})
this.setState({ loadedConfig: resultData, configLoaded: true })
})
}
componentDidMount() {
const that = this
this.loadConfig()
setTimeout(() => {
this.activityLogger()
}, 5000)
if (localStorage.getItem('token') !== null && localStorage.getItem('token') !== undefined) {
let tokenHeaders = new Headers()
tokenHeaders.append('Authorization', 'Bearer ' + localStorage.getItem('token'))
tokenHeaders.append('Content-Type', 'application/x-www-form-urlencoded')
let requestOptions = {
method: 'GET',
headers: tokenHeaders,
redirect: 'follow',
}
let responseStatus = ''
// eslint-disable-next-line no-undef
fetch(process.env.REACT_APP_API_URL + '/api/auth/user', requestOptions)
.then((response) => {
responseStatus = response.status
return response.json()
})
.then((result) => {
if (responseStatus === 200) {
localStorage.setItem('user_id', result.data.id)
localStorage.setItem('user_name', result.data.name)
localStorage.setItem('user_email', result.data.email)
localStorage.setItem('user_admin', result.data.is_admin)
if (result.data.is_admin == '1') {
this.setState({ loggedInUserIsAdmin: true })
}
if (result.data.cookie_choice != '') {
localStorage.setItem('cookies_accepted', result.data.cookie_choice)
}
this.setLoggedInStatus(true)
this.setState({ loginData: result.data })
this.loadConfig()
} else {
let cookiesAcceptedDecision = ''
if (localStorage.getItem('cookies_accepted') !== null) {
cookiesAcceptedDecision = localStorage.getItem('cookies_accepted')
}
localStorage.clear()
if (cookiesAcceptedDecision !== '') {
localStorage.setItem('cookies_accepted', cookiesAcceptedDecision)
}
this.setLoggedInStatus(false)
}
})
} else {
let cookiesAcceptedDecision = ''
if (localStorage.getItem('cookies_accepted') !== null) {
cookiesAcceptedDecision = localStorage.getItem('cookies_accepted')
}
localStorage.clear()
if (cookiesAcceptedDecision !== '') {
localStorage.setItem('cookies_accepted', cookiesAcceptedDecision)
}
this.setLoggedInStatus(false)
}
if (localStorage.getItem('first_login') == 'true') {
this.setState({ first_login: true })
localStorage.removeItem('first_login')
}
setInterval(() => {
this.activityLogger()
}, 180000) // Every 3 minutes
}
handleStateChange() {
this.setState({ first_login: false })
localStorage.removeItem('first_login')
}
activityLogger() {
if (this.state.isLoggedIn) {
// Send Active-State
let activityTokenHeaders = new Headers()
activityTokenHeaders.append('Authorization', 'Bearer ' + localStorage.getItem('token'))
const activityRequestOptions = {
method: 'POST',
headers: activityTokenHeaders,
body: JSON.stringify({
active: true,
}),
}
// eslint-disable-next-line no-undef
fetch(process.env.REACT_APP_API_URL + '/api/auth/activity', activityRequestOptions)
.then((response) => {
return response.json()
})
.then((response) => {
if (response.token) {
localStorage.setItem('token', response.token)
}
})
}
}
render() {
return (
<ConfigContext.Provider value={this.state.loadedConfig}>
<BrowserRouter>
{this.state.first_login && <FirstLogin handleStateChange={this.handleStateChange} />}
{this.state.isLoggedIn === true && this.state.configLoaded === true && (
<AuthProvider value={this.state.loginData}>
<Routes>
<Route exact path="/" element={<Navigate to="/app" />} />
<Route exact path="/app" element={<MainApp />} />
<Route exact path="/app/post/:postId" element={<SinglePost />} />
<Route exact path="/app/topic/:hashTag" element={<Topic />} />
<Route exact path="/logout" element={<LogOut />} />
<Route exact path="/app/user/:email" element={<User />} />
<Route exact path="/app/group/:id" element={<Group />} />
<Route exact path="/app/groups" element={<Groups />} />
<Route exact path="/app/group-request-approved" element={<GroupRequestApproved />} />
<Route exact path="/app/group-request-pending" element={<GroupRequestPending />} />
<Route exact path="/app/group/:id/request/:request_id/:request_status" element={<GroupRequestUpdate />} />
<Route exact path="/app/knowledgebase" element={<KnowledgeBase />} />
<Route exact path="/app/knowledgebase/:folderId" element={<KnowledgeBase />} />
<Route exact path="/app/knowledgebase/:folderId/:fileId" element={<KnowledgeBase />} />
<Route exact path="/app/knowledgebase/:folderId/:fileId/:historyId" element={<KnowledgeBase />} />
{this.state.loggedInUserIsAdmin && (
<React.Fragment>
<Route path="/app/settings/:category" element={<Settings />} />
<Route exact path="/app/settings" element={<Navigate to="/app/settings/overview" />} />
</React.Fragment>
)}
<Route path="*" element={<Error404 />} />
</Routes>
</AuthProvider>
)}
<Routes>
{this.state.isLoggedIn === false && this.state.configLoaded === true && (
<React.Fragment>
<Route exact path="/" element={<SignIn />} />
<Route path="/app" element={<Navigate to={`/?ref=${window.location.pathname}`} />} />
<Route exact path="/signup" element={<SignUpWrapper />} />
<Route path="/signup/activate/:token" element={<ActivateAccount />} />
<Route exact path="/password-reset" element={<PasswordReset />} />
<Route exact path="/logout" element={<LogOut />} />
<Route path="*" element={<Error404 />} />
</React.Fragment>
)}
{(this.state.isLoggedIn === null || this.state.configLoaded == null || this.state.configLoaded == false) && (
<React.Fragment>
<Route path="/*" element={<Loader className="app-loader" active size="large" content="Initializing WorkGroup..." />}></Route>
</React.Fragment>
)}
</Routes>
{this.state.configLoaded === true && <CookieBanner isLoggedIn={this.state.isLoggedIn} />}
</BrowserRouter>
</ConfigContext.Provider>
)
}
}
export default App