Skip to content

Commit 90d4089

Browse files
committed
Update app and view
Readpost now uses New Set instead of an array
1 parent 4c7c501 commit 90d4089

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

src/js/app.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ const findPostById = (posts, id) => posts.find((post) => post.postId === id);
5252
const fillingModalWindow = (watcherState, posts, button) => {
5353
const postId = button.dataset.id;
5454
const post = findPostById(posts, postId);
55-
if (!watcherState.readPosts.includes(postId)) watcherState.readPosts.push(postId);
55+
if (!watcherState.readPosts.has(postId)) watcherState.readPosts.add(postId);
5656
watcherState.modal.post = post;
5757
watcherState.modal.state = 'show';
5858
};
@@ -86,7 +86,7 @@ const app = () => {
8686
feeds: [],
8787
posts: [],
8888
},
89-
readPosts: [],
89+
readPosts: new Set(),
9090
modal: {
9191
post: null,
9292
},
@@ -148,8 +148,8 @@ const app = () => {
148148
});
149149
elements.columnPosts.addEventListener('click', (e) => {
150150
const targetId = e.target.dataset.id;
151-
if (targetId && !watcherState.readPosts.includes(targetId)) {
152-
watcherState.readPosts.push(targetId);
151+
if (targetId && !watcherState.readPosts.has(targetId)) {
152+
watcherState.readPosts.add(targetId);
153153
}
154154
});
155155
elements.modal.addEventListener('show.bs.modal', (event) => fillingModalWindow(watcherState, initialState.content.posts, event.relatedTarget));

src/js/view.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ const renderPosts = (elements, posts, readPosts, i18n) => {
9696
const li = document.createElement('li');
9797
li.classList.add('list-group-item', 'd-flex', 'justify-content-between', 'align-items-start', 'border-0', 'border-end-0');
9898
const title = document.createElement('a');
99-
if (readPosts.includes(post.postId)) {
99+
if (readPosts.has(post.postId)) {
100100
title.classList.add('fw-normal');
101101
} else {
102102
title.classList.add('fw-bold');
@@ -139,8 +139,10 @@ const markPostAsRead = (elements, postId) => {
139139
postTitle.classList.add('fw-normal');
140140
};
141141

142+
const getLastElemFromSet = (coll) => Array.from(coll).at(-1);
143+
142144
const render = (elements, initialState, i18n) => (path, value) => {
143-
console.log(path, value);
145+
// console.log(path, value);
144146
if (path === 'validationUrl.state' && value === 'updated') {
145147
renderFormUpdated(elements);
146148
}
@@ -160,7 +162,7 @@ const render = (elements, initialState, i18n) => (path, value) => {
160162
renderModal(elements, initialState.modal.post);
161163
}
162164
if (path === 'readPosts') {
163-
markPostAsRead(elements, initialState.readPosts.at(-1));
165+
markPostAsRead(elements, getLastElemFromSet(initialState.readPosts));
164166
}
165167
if (path === 'modal.state' && !value) {
166168
clearModal(elements);

0 commit comments

Comments
 (0)