Skip to content

Commit 9e576bb

Browse files
committed
рендер только новых постов, добавление viewed стилей через поле поста
1 parent 8074800 commit 9e576bb

File tree

4 files changed

+34
-34
lines changed

4 files changed

+34
-34
lines changed

src/js/model.js

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const schema = yup
1515
.test(
1616
"no-duplicate",
1717
i18next.t("no_duplicate"),
18-
(value) => !state.feeds.some((feed) => feed.link === value)
18+
(value) => !state.feedsList.includes(value)
1919
);
2020

2121
export const validateInput = () => {
@@ -42,33 +42,33 @@ export const updateInputValue = (value) => {
4242
const fetchAndParseFeed = (url) => {
4343
return fetchRssData(url)
4444
.then((xmlString) => {
45-
// console.log("Полученные данные:", xmlString);
4645
return parseRss(xmlString);
4746
})
4847
.catch((error) => {
4948
throw error;
5049
})
5150
};
5251

53-
const addNewPosts = (items) => {
52+
const addNewPosts = (posts) => {
5453
const existingIds = new Set(state.posts.map((post) => post.id));
55-
const newPosts = items
56-
.filter((item) => !existingIds.has(item.id));
57-
state.posts = [...state.posts, ...newPosts];
54+
const newPosts = posts
55+
.filter((post) => !existingIds.has(post.id))
56+
.map((post) => ({ ...post, viewed: false }));
57+
state.posts.unshift(...newPosts);
5858
};
5959

6060
const updateFeedsAndPostsState = ({ channel, items }) => {
61-
state.feeds = [...state.feeds, channel];
61+
state.feeds.push(channel);
62+
state.feedsList.push(state.form.inputValue);
6263
addNewPosts(items);
6364
state.form.inputValue = "";
6465
};
6566

6667
export const addRssFeed = () => {
6768
return fetchAndParseFeed(state.form.inputValue)
6869
.then(({ channel, items }) => {
69-
updateFeedsAndPostsState({ channel, items });
70-
// return { channel, items };
71-
})
70+
updateFeedsAndPostsState({ channel, items });
71+
})
7272
.catch((error) => {
7373
console.error("Ошибка в addRssFeed:", error.message);
7474
throw error;
@@ -77,7 +77,6 @@ export const addRssFeed = () => {
7777

7878
export const feedsChecking = () => {
7979
if (state.feeds.length === 0) {
80-
// setTimeout(feedsChecking, 10000);
8180
return;
8281
}
8382
checkRssFeed()
@@ -91,13 +90,15 @@ export const feedsChecking = () => {
9190
};
9291

9392
export const checkRssFeed = () => {
94-
const promises = state.feeds.map((feed) => {
95-
return fetchAndParseFeed(feed.link)
93+
const promises = state.feedsList.map((feed) => {
94+
// передавать нормальную ссылку на feed
95+
console.log("feed link:", feed)
96+
return fetchAndParseFeed(feed)
9697
.then(({ items }) => {
9798
return items;
9899
})
99100
.catch((error) => {
100-
console.error("Ошибка проверки фида:", feed.link, error.message);
101+
console.error("Ошибка проверки фида:", feed, error.message);
101102
return [];
102103
});
103104
});
@@ -117,5 +118,8 @@ export const setActivePost = (id) => {
117118
};
118119

119120
export const markPostAsRead = (id) => {
120-
state.viewedPostsIds = [...state.viewedPostsIds, id];
121+
const post = state.posts.find(post => post.id === id);
122+
if (post && !post.viewed) {
123+
post.viewed = true;
124+
}
121125
};

src/js/parseRss.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,24 @@
11
export const parseRss = (xmlString) => {
22
const parser = new DOMParser();
3-
const doc = parser.parseFromString(xmlString, "application/xml");
3+
const doc = parser.parseFromString(xmlString, "text/xml");
44

55
const error = doc.querySelector("parsererror");
66
if (error) {
7-
throw new Error("ошибка парсинга xml");
7+
throw new Error(`Ошибка парсинга XML: ${error.textContent}`);
88
}
99

1010
const channel = {
1111
id: doc.querySelector("channel > link")?.textContent || "",
1212
title: doc.querySelector("channel > title")?.textContent || "",
1313
description: doc.querySelector("channel > description")?.textContent || "",
1414
link: doc.querySelector("channel > link")?.textContent || "",
15-
pubDate: doc.querySelector("channel > pubDate")?.textContent || "",
1615
};
1716

1817
const items = Array.from(doc.querySelectorAll("item")).map((item) => ({
1918
id: item.querySelector("guid")?.textContent || "",
2019
title: item.querySelector("title")?.textContent || "",
2120
description: item.querySelector("description")?.textContent || "",
2221
link: item.querySelector("link")?.textContent || "",
23-
pubDate: item.querySelector("pubDate")?.textContent || "",
2422
}));
2523

2624
return { channel, items };

src/js/renderRssFeed.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export const renderFeeds = (feeds) => {
2222
`;
2323
};
2424

25-
export const renderPosts = (posts, viewedPostsIds) => {
25+
export const renderPosts = (posts) => {
2626
const postsContainer = document.querySelector(".posts");
2727

2828
postsContainer.innerHTML = `
@@ -34,7 +34,7 @@ export const renderPosts = (posts, viewedPostsIds) => {
3434
${posts
3535
.map(
3636
(post) => {
37-
const titleStyle = viewedPostsIds.includes(post.id) ? "fw-normal" : "fw-bold";
37+
const titleStyle = post.viewed ? "fw-normal" : "fw-bold";
3838
return `
3939
<li class="list-group-item d-flex justify-content-between align-items-start border-0 border-end-0">
4040
<a href="${post.link}" class=${titleStyle} data-id="${post.id}" target="_blank" rel="noopener noreferrer">${post.title}</a>
@@ -48,3 +48,9 @@ export const renderPosts = (posts, viewedPostsIds) => {
4848
`;
4949
};
5050

51+
export const renderViewedPost = (id) => {
52+
const viewedPost = document.querySelector(`[data-id="${id}"]`);
53+
viewedPost.classList.remove("fw-bold");
54+
viewedPost.classList.add("fw-normal");
55+
}
56+

src/js/state.js

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,26 @@ const createState = () => {
1010
inputValue: "",
1111
errors: null,
1212
},
13+
feedsList: [],
1314
feeds: [],
1415
posts: [],
1516
activeItem: null,
16-
viewedPostsIds: [],
1717
};
1818

1919
const state = onChange(object, (path, value) => {
2020
console.log(`состояние изменено: ${path}`, value);
2121
if (path === "feeds") {
22-
renderFeeds(state.feeds);
22+
renderFeeds(value);
2323
feedsChecking();
2424
}
2525
if (path === "posts") {
26-
renderPosts(state.posts, [...state.viewedPostsIds]);
27-
state.posts.forEach((post) => {
28-
post.rendered = true;
29-
});
26+
renderPosts(value);
3027
}
3128
if (path === "activeItem") {
3229
if (state.activeItem !== null) {
3330
showModal(state.activeItem);
34-
markPostAsRead(state.activeItem.id); //проверять на уникальность!
35-
renderPosts(state.posts, [...state.viewedPostsIds]);
31+
markPostAsRead(state.activeItem.id);
32+
renderViewedPost(state.activeItem.id);
3633
} else {
3734
closeModal();
3835
}
@@ -49,8 +46,3 @@ const createState = () => {
4946
};
5047

5148
export default createState;
52-
53-
54-
55-
56-

0 commit comments

Comments
 (0)