|
1 | | -import { loaderController } from "./components/loader/loaderController.js"; |
2 | | -import { notificationsController } from "./components/notifications/notificationsController.js"; |
3 | 1 |
|
4 | | -import { showPostsController } from "./modules/show-posts/showPostsController.js"; |
5 | | -import { loginController } from "../js/modules/login/loginController.js"; |
| 2 | + |
| 3 | +import { initShowPosts } from './modules/show-posts/showPosts.js'; |
| 4 | +import { initCreatePost } from './modules/create-post/createPost.js'; |
| 5 | +import { initLogin } from './modules/login/login.js'; |
| 6 | +import { initPostDetail } from './modules/post-detail/postDetail.js'; |
| 7 | +import { initSignUp } from './modules/signup/signup.js'; |
| 8 | + |
| 9 | +//--------------------------------------------------------------------------------------------------------------------------- |
| 10 | + |
| 11 | +import { navbar } from './components/navbar/navbar.js'; |
| 12 | +import { redirectIfNotAuthenticated } from './auth/auth.js'; |
6 | 13 |
|
7 | 14 |
|
8 | | -// Get the DOM nodes we need to interact with. |
9 | 15 | document.addEventListener("DOMContentLoaded", () => { |
10 | 16 |
|
11 | | - // Here I go to the DOM and select the nodes I need to manage in my code and I manage them in my controllers. |
12 | | - const container = document.querySelector(".posts-container") |
13 | | - const loader = document.querySelector(".loader") |
14 | | - const notifications = document.querySelector(".notifications") |
15 | | - const session = document.querySelector(".session") |
16 | | - |
17 | | - const { show, hide } = loaderController(loader); |
18 | | - const { showNotification } = notificationsController(notifications) |
19 | | - |
20 | | - /* |
21 | | - 'main.js' needs to know when posts start and finish loading, |
22 | | - which is handled inside 'showPostsController.js'. |
23 | | -
|
24 | | - For that reason, we use 'CustomEvent' in 'showPostsController.js'. |
25 | | -
|
26 | | - Here, we listen to those custom events ('load-posts-started' and 'load-posts-finished') |
27 | | - on the "container" element. When triggered, we call 'show()' to display the loader, |
28 | | - and 'hide()' to remove it once loading is complete. |
29 | | - */ |
30 | | - container.addEventListener('load-posts-started', () => { |
31 | | - show(); |
32 | | - }) |
33 | | - container.addEventListener('load-posts-finished', () => { |
34 | | - hide(); |
35 | | - }) |
36 | | - container.addEventListener('load-posts-error', (event) => { |
37 | | - const errorMessage = event.detail; // <-- Here we are passing the error obtained from "showPostsController.js". |
38 | | - showNotification(errorMessage) // <-- Here we do not add the type “success” so it will be “error”. |
39 | | - }) |
40 | | - |
41 | | - showPostsController(container) // This line happens in showPostsController.js |
42 | | - loginController(session) |
| 17 | + redirectIfNotAuthenticated(); |
| 18 | + navbar(); |
| 19 | + |
| 20 | + const path = window.location.pathname; |
| 21 | + |
| 22 | + if (path === '/index.html' || path === '/' || path === '/index') { |
| 23 | + initShowPosts(); |
| 24 | + } |
| 25 | + |
| 26 | + if (path.endsWith('/views/create-post.html')) { |
| 27 | + initCreatePost(); |
| 28 | + } |
| 29 | + |
| 30 | + if (path.endsWith('/views/login.html')) { |
| 31 | + initLogin(); |
| 32 | + } |
| 33 | + |
| 34 | + if (path.endsWith('/views/post-detail.html')) { |
| 35 | + initPostDetail(); |
| 36 | + } |
| 37 | + |
| 38 | + if (path.endsWith('/views/signup.html')) { |
| 39 | + initSignUp(); |
| 40 | + } |
43 | 41 | }) |
0 commit comments