-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
114 lines (106 loc) · 3.27 KB
/
index.ts
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
import page from 'page';
import axios from 'axios';
import { PageLayoutView } from './common/PageViewLayout';
import { Application } from './utils/framework';
import { config } from './config';
interface IRoute {
path: string;
action: (ctx: PageJS.Context) => Promise<void>
}
export class RealWorldApp extends Application {
routes: IRoute[] = [
{
path: '/',
action: this.initArticles.bind(this),
},
{
path: '/login',
action: this.initLogin.bind(this),
},
{
path: '/register',
action: this.initRegister.bind(this),
},
{
path: '/settings',
action: this.initSettings.bind(this),
},
{
path: '/editor',
action: this.initEditor.bind(this),
},
{
path: '/editor/:id',
action: this.initEditor.bind(this),
},
{
path: '/article/:id',
action: this.initArticle.bind(this),
},
{
path: '*',
action: this.initPageNotFound.bind(this),
},
];
layout: PageLayoutView = new PageLayoutView();
async init(): Promise<any> {
// Detect user
const jwt = localStorage.getItem('jwt');
if (jwt) {
const response = await axios.get(`${config.apiUrl}/user`, {
headers: {
Authorization: `Token ${jwt}`
}
});
config.user = response.data.user;
}
// Link handler
document.body.addEventListener('click', async (event) => {
const element = event.target as HTMLElement;
if (element.tagName === 'A') {
let href = element.getAttribute('href');
if (href && href.startsWith('/')) {
event.preventDefault();
page(href.replace('/#!', ''));
}
}
});
// Initialize page layout
await this.mainRegion.show(this.layout);
// Initialize router
this.routes.forEach(route => page(route.path, route.action));
page.start({
hashbang: true,
});
}
async initArticles(): Promise<void> {
const { ArticlesPresenter } = await import('./pages/articles/ArticlesPresenter');
await this.layout.content.show(new ArticlesPresenter());
}
async initLogin(): Promise<void> {
const { LoginPresenter } = await import('./pages/login/LoginPresenter');
await this.layout.content.show(new LoginPresenter());
}
async initRegister(): Promise<void> {
const { RegisterPresenter } = await import('./pages/register/RegisterPresenter');
await this.layout.content.show(new RegisterPresenter());
}
async initSettings(): Promise<void> {
const { SettingsPresenter } = await import('./pages/settings/SettingsPresenter');
await this.layout.content.show(new SettingsPresenter());
}
async initEditor(ctx: PageJS.Context): Promise<void> {
const { EditorPresenter } = await import('./pages/editor/EditorPresenter');
await this.layout.content.show(new EditorPresenter(ctx.params.id));
}
async initArticle(ctx: PageJS.Context): Promise<void> {
const { ArticlePresenter } = await import('./pages/article/ArticlePresenter');
await this.layout.content.show(new ArticlePresenter(ctx.params.id));
}
async initPageNotFound(): Promise<void> {
const { PageNotFoundView } = await import('./common/PageNotFoundView');
await this.layout.content.show(new PageNotFoundView());
}
}
const app = new RealWorldApp('#root');
app.init();