Skip to content

Commit 5f642b3

Browse files
committed
парсинг и изменения state для фидов разделены
1 parent 3f878d1 commit 5f642b3

File tree

5 files changed

+58
-43
lines changed

5 files changed

+58
-43
lines changed

src/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,6 @@ <h1 id="main-title" class="display-3 mb-0">RSS агрегатор</h1>
121121
</div>
122122
</footer>
123123
<deepl-input-controller translate="no"></deepl-input-controller>
124-
<script type="module" src="./js/main.js"></script>
124+
<script type="module" src="./js/index.js"></script>
125125
</body>
126126
</html>

src/js/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import "../i18n.js";
2+
import { initApp } from "./main.js";
3+
4+
initApp();
5+
6+
// validateInput -> fetchRssData -> parseRss -> state -> render

src/js/initApp.js

Lines changed: 0 additions & 34 deletions
This file was deleted.

src/js/main.js

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,39 @@
1-
import "../i18n.js";
2-
import { initApp } from "./initApp.js";
1+
import { renderUIText } from "./view.js";
2+
import {
3+
updateInputValue,
4+
addRssFeed,
5+
validateInput,
6+
setActivePost,
7+
checkRssFeed,
8+
} from "./model.js";
9+
10+
export const initApp = () => {
11+
renderUIText();
12+
13+
const input = document.querySelector("#url-input");
14+
const form = document.querySelector("#rss-form");
15+
const postsContainer = document.querySelector(".posts");
16+
17+
setInterval(checkRssFeed, 5000);
18+
19+
input.addEventListener("input", (e) => {
20+
updateInputValue(e.target.value);
21+
});
22+
23+
form.addEventListener("submit", (e) => {
24+
e.preventDefault();
25+
validateInput()
26+
.then(() => addRssFeed())
27+
.catch(() => console.log("валидация не пройдена"));
28+
});
29+
30+
postsContainer.addEventListener("click", (e) => {
31+
const button = e.target.closest(".modal-btn");
32+
console.log("id:", button.dataset.id);
33+
if (button && button.dataset.id) {
34+
setActivePost(button.dataset.id);
35+
}
36+
});
37+
};
38+
339

4-
initApp();

src/js/model.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,14 @@ export const updateInputValue = (value) => {
3939
if (state.form.errors) state.form.errors = null;
4040
};
4141

42-
export const addRssFeed = () => {
43-
return fetchRssData(state.form.inputValue)
44-
.then((xmlString) => parseRss(xmlString))
45-
.then(({ channel, items }) => {
42+
const fetchAndParseFeed = (url) => {
43+
return fetchRssData(url).then((xmlString) => parseRss(xmlString)).catch((error) => {
44+
console.error("fetchAndParseRss:", error);
45+
throw error;
46+
})
47+
};
48+
49+
const updateFeedsAndPostsState = ({ channel, items}) => {
4650
if (state.feeds.some((feed) => feed.link === channel.link)) {
4751
state.form.errors = i18next.t("no_duplicate");
4852
return;
@@ -53,7 +57,11 @@ export const addRssFeed = () => {
5357
state.posts = [...state.posts, ...newItems];
5458
state.form.inputValue = "";
5559
console.log("обновлён список rss", state.feeds);
56-
})
60+
};
61+
62+
export const addRssFeed = () => {
63+
return fetchAndParseFeed(state.form.inputValue)
64+
.then(({ channel, items }) => updateFeedsAndPostsState({ channel, items }))
5765
.catch((error) => {
5866
throw error;
5967
});

0 commit comments

Comments
 (0)