focalboard/webapp/src/app.tsx

86 lines
2.8 KiB
TypeScript
Raw Normal View History

2020-10-20 21:50:53 +02:00
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
2020-12-07 20:40:16 +01:00
import React from 'react'
2020-10-23 13:40:39 +02:00
import {IntlProvider} from 'react-intl'
import {
2020-10-20 21:50:53 +02:00
BrowserRouter as Router,
Switch,
Route,
2020-12-04 16:03:09 +01:00
Redirect,
2020-10-20 21:52:56 +02:00
} from 'react-router-dom'
2020-12-04 16:03:09 +01:00
import client from './octoClient'
2020-12-07 20:40:16 +01:00
import {IUser, UserContext} from './user'
2020-12-04 16:03:09 +01:00
2020-10-23 13:40:39 +02:00
import {getCurrentLanguage, getMessages, storeLanguage} from './i18n'
import {FlashMessages} from './components/flashMessages'
2020-10-20 21:52:56 +02:00
import LoginPage from './pages/loginPage'
2020-12-04 16:03:09 +01:00
import RegisterPage from './pages/registerPage'
2020-10-20 21:52:56 +02:00
import BoardPage from './pages/boardPage'
2020-12-07 20:40:16 +01:00
type State = {
language: string,
user: IUser|null,
initialLoad: boolean,
}
export default class App extends React.PureComponent<unknown, State> {
constructor(props: unknown) {
super(props)
this.state = {
language: getCurrentLanguage(),
user: null,
initialLoad: false,
}
}
public componentDidMount(): void {
client.getMe().then((user: IUser|null) => {
this.setState({user, initialLoad: true})
})
}
setAndStoreLanguage = (lang: string): void => {
2020-10-23 13:40:39 +02:00
storeLanguage(lang)
2020-12-07 20:40:16 +01:00
this.setState({language: lang})
}
public render(): JSX.Element {
return (
<IntlProvider
locale={this.state.language}
messages={getMessages(this.state.language)}
>
<UserContext.Provider value={this.state.user}>
<FlashMessages milliseconds={2000}/>
<Router>
<div id='frame'>
<div id='main'>
<Switch>
<Route path='/login'>
<LoginPage/>
</Route>
<Route path='/register'>
<RegisterPage/>
</Route>
<Route path='/'>
{this.state.initialLoad && !this.state.user && <Redirect to='login'/>}
<BoardPage setLanguage={this.setAndStoreLanguage}/>
</Route>
<Route path='/board'>
{this.state.initialLoad && !this.state.user && <Redirect to='login'/>}
<BoardPage setLanguage={this.setAndStoreLanguage}/>
</Route>
</Switch>
</div>
</div>
</Router>
</UserContext.Provider>
</IntlProvider>
)
2020-10-23 13:40:39 +02:00
}
}