Skip to content

Commit 3f878d1

Browse files
committed
проверка на дубликаты при добавлении фидов и постов
1 parent 05e8834 commit 3f878d1

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

src/js/model.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,23 @@ export const validateInput = () => {
3434
};
3535

3636
export const updateInputValue = (value) => {
37+
if (value === null) return;
3738
state.form.inputValue = value;
38-
state.form.errors = null;
39+
if (state.form.errors) state.form.errors = null;
3940
};
4041

4142
export const addRssFeed = () => {
42-
fetchRssData(state.form.inputValue)
43+
return fetchRssData(state.form.inputValue)
4344
.then((xmlString) => parseRss(xmlString))
4445
.then(({ channel, items }) => {
46+
if (state.feeds.some((feed) => feed.link === channel.link)) {
47+
state.form.errors = i18next.t("no_duplicate");
48+
return;
49+
}
50+
const existingIds = new Set(state.posts.map((post) => post.id));
51+
const newItems = items.filter((item) => !existingIds.has(item.id));
4552
state.feeds = [...state.feeds, channel];
46-
state.posts = [...state.posts, ...items];
53+
state.posts = [...state.posts, ...newItems];
4754
state.form.inputValue = "";
4855
console.log("обновлён список rss", state.feeds);
4956
})

0 commit comments

Comments
 (0)