Skip to content

Commit 3246eb6

Browse files
committed
notifications learned in classes is added
1 parent 77806e0 commit 3246eb6

File tree

7 files changed

+81
-20
lines changed

7 files changed

+81
-20
lines changed

index.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55
<meta name="viewport" content="width=device-width, initial-scale=1.0">
66
<title>Document</title>
77
<link rel="stylesheet" href="src/styles/loader.css">
8+
<link rel="stylesheet" href="src/styles/notifications.css">
9+
810
</head>
911
<body>
12+
<div class="notifications"></div>
1013
<div class="loader hidden"></div> <!-- by defaul "loader" is hidden -->
1114
<div class="posts-container"></div>
1215
<script type="module" src="src/js/main.js"></script>

src/js/components/loader/loaderController.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11

2-
// the "loader" in loaderController(loader) is the class '.loader' from index.html"
3-
export function loaderController(loader) { // 'loader' comes from ShowPostsController.js
2+
// the "loader" in loaderController(loader) is the class '.loader' from index.html".
3+
export function loaderController(container) { // 'container' is ".loader" and comes from ShowPostsController.js.
44

55
const show = () => {
66

7-
// <div class="loader hidden -> X"></div>
8-
loader.classList.remove("hidden");
7+
// <div class="loader -> -hidden</div>
8+
// "classList" is a property of DOM elements.
9+
container.classList.remove("hidden");
910
}
1011
const hide = () => {
1112

12-
// <div class="loader + hidden"></div>
13-
loader.classList.add("hidden");
13+
// <div class="loader -> +hidden"></div>
14+
container.classList.add("hidden");
1415
}
1516

1617
return {
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { buildNotification } from "./notificationsView.js";
2+
3+
export function notificationsController(container){ // container is ".notifications"
4+
5+
// Internal function.
6+
const removeNotification = (newNotification) => {
7+
newNotification.remove();
8+
}
9+
10+
// This method receives a message and type (error / success), if it does not receive type by default it will be "error".
11+
const showNotification = (message, type = 'error') => {
12+
13+
const newNotification = document.createElement('div');
14+
15+
newNotification.classList.add('notification') // <-- Here we add the "notification" class so "notification.css" will detect it.
16+
newNotification.classList.add(type) // <-- Here we add the class of the container we pass to "type".
17+
newNotification.innerHTML = buildNotification(message, type) // <-- If "type" is not added, the default will be “error”.
18+
19+
container.appendChild(newNotification) // <-- Here we insert our HTML ready to use.
20+
21+
const closeButton = newNotification.querySelector("button"); // <-- It will select only the button element of the container that we pass it.
22+
23+
closeButton.addEventListener("click", () => {
24+
removeNotification(newNotification);
25+
});
26+
27+
setTimeout(() => {
28+
removeNotification(newNotification)
29+
}, 5000);
30+
}
31+
32+
return {
33+
showNotification
34+
}
35+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export const buildNotification = (message) => {
2+
return `
3+
<p>${message}</p>
4+
<button>X</button>
5+
`
6+
}
7+

src/js/main.js

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
import { loaderController } from "./components/loader/loaderController.js";
2+
import { notificationsController } from "./components/notifications/notificationsController.js";
23
import { showPostsController } from "./modules/show-posts/showPostsController.js";
34

45

56
// Get the DOM nodes we need to interact with.
67
document.addEventListener("DOMContentLoaded", () => {
78

8-
// Here I go to the DOM and select the nodes I need to manage in my code.
9-
const container = document.querySelector(".posts-container")
10-
const loader = document.querySelector(".loader")
9+
// Here I go to the DOM and select the nodes I need to manage in my code and I manage them in my controllers.
10+
const container = document.querySelector(".posts-container") // ".posts-container" is a Node
11+
const loader = document.querySelector(".loader") // ".loader" is a Node
12+
const notifications = document.querySelector(".notifications")
1113

12-
const { show, hide } = loaderController(loader)
14+
15+
const { show, hide } = loaderController(loader);
16+
const { showNotification } = notificationsController(notifications)
1317

1418
/*
1519
'main.js' needs to know when posts start and finish loading,
@@ -18,19 +22,19 @@ document.addEventListener("DOMContentLoaded", () => {
1822
For that reason, we use 'CustomEvent' in 'showPostsController.js'.
1923
2024
Here, we listen to those custom events ('load-posts-started' and 'load-posts-finished')
21-
on the container element. When triggered, we call 'show()' to display the loader,
25+
on the "container" element. When triggered, we call 'show()' to display the loader,
2226
and 'hide()' to remove it once loading is complete.
2327
*/
2428
container.addEventListener('load-posts-started', () => {
25-
show()
29+
show();
2630
})
2731
container.addEventListener('load-posts-finished', () => {
28-
hide()
29-
/*showNotification('Posts Loaded')*/
32+
hide();
33+
showNotification('Posts Loaded' , 'success') // <-- Here we add type as “success”.
3034
})
3135
container.addEventListener('load-tweets-error', (event) => {
32-
/* const errorMesage = event.detail;
33-
showNotification(errorMesage) */
36+
const errorMessage = event.detail; // <-- Here we are passing the error obtained from "showPostsController.js".
37+
showNotification(errorMessage) // <-- Here we do not add the type “success” so it will be “error”.
3438
})
3539

3640
// Then this next code happens in showPostsController.js

src/js/modules/show-posts/showPostsModel.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@ export async function getPosts() {
1212
the error will have a better context so you have a better answer for detecting problems with BD. */
1313

1414
// here "error" would return a 'status code'.
15-
16-
/* the error should not be sent to the file driver for it to handle, but directly send the error itself. */
17-
18-
1915

2016
throw new Error("It was not possible to get the posts. Please try again later.") // this error is sended to showPostsController.js
2117
}

src/styles/notifications.css

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
.notification {
2+
border: solid 1px;
3+
padding: 1rem;
4+
}
5+
6+
.notification.success {
7+
background-color: lightblue;
8+
border-color: rgb(0, 172, 230);
9+
}
10+
11+
.notification.error {
12+
background-color: lightcoral;
13+
border-color: rgb(242, 8, 8);
14+
}
15+

0 commit comments

Comments
 (0)