Skip to content

Commit 5f1532a

Browse files
authoredSep 14, 2023
add parsers
1 parent 7dd612b commit 5f1532a

File tree

7 files changed

+109
-8
lines changed

7 files changed

+109
-8
lines changed
 

‎package-lock.json

+55-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

+2
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,10 @@
3434
},
3535
"dependencies": {
3636
"@popperjs/core": "^2.11.8",
37+
"axios": "^1.5.0",
3738
"bootstrap": "~5.2.3",
3839
"i18next": "^23.5.1",
40+
"lodash": "^4.17.21",
3941
"on-change": "^4.0.2",
4042
"yup": "^1.2.0"
4143
}

‎src/js/RSSparse.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const getFeed = (dom) => {
2+
const titleFeed = dom.querySelector('channel>title').textContent;
3+
const descriptionFeed = dom.querySelector('channel>description').textContent;
4+
return {
5+
titleFeed,
6+
descriptionFeed,
7+
};
8+
};
9+
10+
const getPosts = (dom) => {
11+
const posts = [];
12+
const items = dom.querySelectorAll('item');
13+
items.forEach((item) => posts.push({
14+
title: item.querySelector('title').textContent,
15+
link: item.querySelector('link').textContent,
16+
}));
17+
return posts;
18+
};
19+
20+
const parseRSS = (dom) => {
21+
const result = {
22+
feed: {},
23+
posts: [],
24+
};
25+
result.feed = getFeed(dom);
26+
result.posts = getPosts(dom);
27+
return result;
28+
};
29+
30+
export default parseRSS;

‎src/js/app.js

+20-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import * as yup from 'yup';
22
import onChange from 'on-change';
3+
import axios from 'axios';
34
import render from './view';
5+
import parseRSS from './RSSparse';
46

57
yup.setLocale({
68
string: {
@@ -32,13 +34,29 @@ const app = () => {
3234
e.preventDefault();
3335
watcherState.validationUrl.state = 'updated';
3436
const formData = new FormData(e.target);
35-
const schemaValidationUrl = yup.string().url().trim().notOneOf(initialState.rssFeeds, 'existing_RSS');
37+
const schemaValidationUrl = yup.string().url().notOneOf(initialState.rssFeeds, 'existing_RSS').trim();
3638
schemaValidationUrl.validate(formData.get('url'))
37-
.then((newUrl) => watcherState.rssFeeds.push(newUrl))
39+
.then((newUrl) => {
40+
axios.get(`https://allorigins.hexlet.app/get?url=${encodeURIComponent(newUrl)}`)
41+
.then((response) => {
42+
const parser = new DOMParser();
43+
const responseDom = parser.parseFromString(response.data.contents, 'text/xml');
44+
if (responseDom.querySelector('parsererror')) throw new Error('invalid_RSS');
45+
parseRSS(responseDom);
46+
})
47+
.catch((err) => {
48+
watcherState.validationUrl.error = err.message;
49+
watcherState.validationUrl.state = 'invalid';
50+
});
51+
watcherState.rssFeeds.push(newUrl);
52+
})
3853
.catch((err) => {
3954
if (err.name === 'ValidationError') {
4055
watcherState.validationUrl.error = err.message;
4156
watcherState.validationUrl.state = 'invalid';
57+
} else {
58+
console.log('Выпала неизвесная ошибка:');
59+
console.dir(err);
4260
}
4361
});
4462
});

‎src/js/locales/en.js

+1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ export default {
22
translation: {
33
url_invalid: 'The link must be a valid URL',
44
existing_rss: 'RSS already exists',
5+
invalid_RSS: 'Does not contain valid RSS',
56
},
67
};

‎src/js/locales/ru.js

+1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ export default {
33
URL_invalid: 'Ссылка должна быть валидным URL',
44
existing_RSS: 'RSS уже существует',
55
RSS_uploaded: 'RSS Успешно загружен',
6+
invalid_RSS: 'Не содержит валидный RSS',
67
},
78
};

‎src/js/view.js

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ const getI18n = (lng) => {
55
const i18n = i18next.createInstance();
66
return i18n.init({
77
lng,
8-
debug: true,
98
resources,
109
});
1110
};

0 commit comments

Comments
 (0)
Please sign in to comment.