Created
December 17, 2017 04:57
-
-
Save Piterden/e4dd7837725609eb155c4bf7f0e749d0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { fetchChatList } from 'api/chatList'; | |
import { api } from 'helpers/Telegram/pool'; | |
import * as React from 'react'; | |
import { Context, Params, Route } from 'universal-router'; | |
import history from '../../history'; | |
export const resolveRoute = async (context: Context, params: Params) => { | |
const { route, store } = context; | |
const { auth } = store.getState(); | |
if (typeof route.action === 'function') { | |
if (route.guard && !auth.authenticated) { | |
history.push('/login'); | |
return null; | |
} | |
const Comp = await route.action(context, params); | |
return <Comp/>; | |
} | |
return null; | |
}; | |
const routes: [Route] = [ | |
{ | |
path: '', | |
// TODO: remove this | |
async action({ next }): Promise<React.ComponentType<{}>> { | |
// TODO: remove this | |
// tslint:disable-next-line | |
const Child = await next(); | |
window.api = api; | |
await api('help.getConfig', {}) | |
.then(app => console.debug('init', app)) | |
.catch(err => console.error('init failed', err)); | |
return function Middleware() { return Child; }; | |
}, | |
children: [ | |
{ | |
path: '', | |
name: 'home', | |
guard: false, | |
async action() { | |
const { Home } = await import (/* webpackChunkName: "home" */ 'containers/Home'); | |
return Home; | |
}, | |
}, | |
{ | |
path: '/im', | |
guard: true, | |
children: [ | |
{ | |
path: '', | |
name: 'im', | |
guard: true, | |
async action({ store: { dispatch } }) { | |
const { InstantMessages } = await import (/* webpackChunkName: "im" */ 'containers/InstantMessages'); | |
await dispatch(fetchChatList()); | |
return InstantMessages; | |
}, | |
}, | |
{ | |
path: '/:id', | |
name: 'im/user', | |
guard: true, | |
action: async (context, { id }: { id: number }) => (props: {}) => { | |
console.debug('im route', id, props, context); | |
return <div>Memes<\div>; | |
}, | |
}, | |
], | |
}, | |
{ | |
path: '/login', | |
name: 'login', | |
guard: false, | |
async action() { | |
const { Login } = await import (/* webpackChunkName: "login" */ 'containers/Login'); | |
return Login; | |
}, | |
}, | |
], | |
}, | |
]; | |
export default routes; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment