|
1 | | -import * as React from 'react'; |
| 1 | +import React, { useEffect } from 'react'; |
2 | 2 | import { NavLink, Route, RouteComponentProps, Switch } from 'react-router-dom'; |
3 | 3 | import NavigationBar from '@/components/NavigationBar'; |
4 | 4 | import ServerConsole from '@/components/server/ServerConsole'; |
5 | 5 | import TransitionRouter from '@/TransitionRouter'; |
| 6 | +import { Actions, State, useStoreActions, useStoreState } from 'easy-peasy'; |
| 7 | +import { ApplicationState } from '@/state/types'; |
| 8 | +import Spinner from '@/components/elements/Spinner'; |
| 9 | +import WebsocketHandler from '@/components/server/WebsocketHandler'; |
6 | 10 |
|
7 | | -export default ({ match, location }: RouteComponentProps) => ( |
8 | | - <React.Fragment> |
9 | | - <NavigationBar/> |
10 | | - <div id={'sub-navigation'}> |
11 | | - <div className={'mx-auto'} style={{ maxWidth: '1200px' }}> |
12 | | - <div className={'items'}> |
13 | | - <NavLink to={`${match.url}`} exact>Console</NavLink> |
14 | | - <NavLink to={`${match.url}/files`}>File Manager</NavLink> |
15 | | - <NavLink to={`${match.url}/databases`}>Databases</NavLink> |
16 | | - <NavLink to={`${match.url}/users`}>User Management</NavLink> |
| 11 | +export default ({ match, location }: RouteComponentProps<{ id: string }>) => { |
| 12 | + const server = useStoreState((state: State<ApplicationState>) => state.server.data); |
| 13 | + const { clearServerState, getServer } = useStoreActions((actions: Actions<ApplicationState>) => actions.server); |
| 14 | + |
| 15 | + if (!server) { |
| 16 | + getServer(match.params.id); |
| 17 | + } |
| 18 | + |
| 19 | + useEffect(() => () => clearServerState(), []); |
| 20 | + |
| 21 | + return ( |
| 22 | + <React.Fragment> |
| 23 | + <NavigationBar/> |
| 24 | + <div id={'sub-navigation'}> |
| 25 | + <div className={'mx-auto'} style={{ maxWidth: '1200px' }}> |
| 26 | + <div className={'items'}> |
| 27 | + <NavLink to={`${match.url}`} exact>Console</NavLink> |
| 28 | + <NavLink to={`${match.url}/files`}>File Manager</NavLink> |
| 29 | + <NavLink to={`${match.url}/databases`}>Databases</NavLink> |
| 30 | + <NavLink to={`${match.url}/users`}>User Management</NavLink> |
| 31 | + </div> |
17 | 32 | </div> |
18 | 33 | </div> |
19 | | - </div> |
20 | | - <TransitionRouter> |
21 | | - <div className={'w-full mx-auto'} style={{ maxWidth: '1200px' }}> |
22 | | - <Switch location={location}> |
23 | | - <Route path={`${match.path}`} component={ServerConsole} exact/> |
24 | | - </Switch> |
25 | | - </div> |
26 | | - </TransitionRouter> |
27 | | - </React.Fragment> |
28 | | -); |
| 34 | + <TransitionRouter> |
| 35 | + <div className={'w-full mx-auto'} style={{ maxWidth: '1200px' }}> |
| 36 | + {!server ? |
| 37 | + <div className={'flex justify-center m-20'}> |
| 38 | + <Spinner large={true}/> |
| 39 | + </div> |
| 40 | + : |
| 41 | + <React.Fragment> |
| 42 | + <WebsocketHandler/> |
| 43 | + <Switch location={location}> |
| 44 | + <Route path={`${match.path}`} component={ServerConsole} exact/> |
| 45 | + </Switch> |
| 46 | + </React.Fragment> |
| 47 | + } |
| 48 | + </div> |
| 49 | + </TransitionRouter> |
| 50 | + </React.Fragment> |
| 51 | + ); |
| 52 | +}; |
0 commit comments