-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
94 lines (80 loc) · 2.84 KB
/
index.js
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
import Login from './views/login.js';
import Register from './views/register.js';
import Settings from './views/settings.js';
import Chat from './views/chat.js';
import AddChat from './views/add-chat.js';
import Error404 from './views/error404.js';
import Utils from './services/utils.js';
let user = null;
const routes = {
'/': Login,
'/register' : Register,
'/settings' : Settings,
'/chat' : Chat,
'/chat/:id' : Chat,
'/add-chat' : AddChat
};
// The router code. Takes a URL, checks against the list of supported routes and then renders the corresponding content page.
const router = async () => {
const all = [];
// Lazy load view element:
const body = null || document.querySelector('body');
// Get the parsed URl from the addressbar
let request = Utils.parseRequestURL();
// Parse the URL and if it has an id part, change it with the string ":id"
let parsedURL = (request.resource ? '/' + request.resource : '/') +
(request.id ? '/:id' : '');
// Get the page from our hash of supported routes.
// If the parsed URL is not in our list of supported routes, select the 404 page instead
let page = routes[parsedURL] ? routes[parsedURL] : Error404;
if (page === Chat) {
if (firebase.auth().currentUser) {
body.innerHTML = await page.render();
// await page.afterRender();
await page.afterRender(Number.parseInt(request.id));
} else {
body.innerHTML = await Login.render();
window.location.hash = '/';
await Login.afterRender();
}
} else if (page === Settings) {
if (firebase.auth().currentUser) {
body.innerHTML = await page.render();
await page.afterRender();
} else {
body.innerHTML = await Login.render();
window.location.hash = '/';
await Login.afterRender();
}
} else if (page === AddChat) {
if (firebase.auth().currentUser) {
body.innerHTML = await page.render();
await page.afterRender();
} else {
body.innerHTML = await Login.render();
window.location.hash = '/';
await Login.afterRender();
}
}
else if (page === Login) {
body.innerHTML = await page.render();
await page.afterRender();
} else {
body.innerHTML = await page.render();
await page.afterRender();
}
}
// Listen on hash change:
window.addEventListener('hashchange', router);
// Listen on page load:
window.addEventListener('DOMContentLoaded', () => {
auth.onAuthStateChanged(firebaseUser => {
if(firebaseUser){
user = firebaseUser.email;
window.location.hash = '/chat';
} else {
window.location.hash = '/';
}
// router();
});
});