-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a7cf3ef
commit e9bb00b
Showing
10 changed files
with
247 additions
and
49 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* eslint-disable no-param-reassign */ | ||
|
||
import * as yup from 'yup'; | ||
import axios from 'axios'; | ||
import domElements from '../utils/domElements.js'; | ||
import strings from '../utils/strings.js'; | ||
import { normalizeUrl, parseRss, parseXML } from './utilities.js'; | ||
|
||
const schema = yup.string().required('this is a required field').url(); | ||
|
||
const request = (watchedState, normalizedValue) => { | ||
axios | ||
.get( | ||
`https://allorigins.hexlet.app/get?disableCache=true&url=${encodeURIComponent( | ||
normalizedValue, | ||
)}`, | ||
) | ||
.then((response) => { | ||
try { | ||
const rssDocument = parseXML(response); | ||
parseRss(rssDocument); | ||
|
||
watchedState.feedList.push(domElements.form.input?.value); | ||
watchedState.feedback = strings.feedback.loaded; | ||
watchedState.formState = strings.formStates.init; | ||
|
||
domElements.form.input.value = ''; | ||
domElements.form.input?.focus(); | ||
} catch { | ||
watchedState.formState = strings.formStates.invalid; | ||
watchedState.feedback = strings.feedback.invalidRss; | ||
domElements.form.input?.focus(); | ||
} | ||
}) | ||
.catch((error) => { | ||
console.log(error); | ||
watchedState.formState = strings.formStates.invalid; | ||
watchedState.feedback = strings.feedback.networkError; | ||
domElements.form.input?.focus(); | ||
}); | ||
}; | ||
|
||
export default (state, watchedState) => { | ||
domElements.form.submit.addEventListener('click', (e) => { | ||
e.preventDefault(); | ||
|
||
const normalizedValue = normalizeUrl(domElements.form.input.value); | ||
|
||
if (state.feedList.includes(normalizedValue)) { | ||
watchedState.formState = strings.formStates.invalid; | ||
watchedState.feedback = strings.feedback.exists; | ||
} else { | ||
schema | ||
.validate(normalizedValue) | ||
.then(() => { | ||
watchedState.formState = strings.formStates.sending; | ||
request(watchedState, normalizedValue); | ||
}) | ||
.catch(() => { | ||
watchedState.formState = strings.formStates.invalid; | ||
watchedState.feedback = strings.feedback.invalidValidation; | ||
domElements.form.input?.focus(); | ||
}); | ||
} | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,44 @@ | ||
import _ from 'lodash'; | ||
import parsedData from '../utils/parsedData.js'; | ||
|
||
const normalizeUrl = (url) => url.trim().toLowerCase(); | ||
|
||
const normalizeError = (error) => error.toString().split(': ')[1]; | ||
const parseXML = (response) => { | ||
const parser = new DOMParser(); | ||
const xmlDoc = parser.parseFromString(response.data.contents, 'text/xml'); | ||
|
||
const isRSS = xmlDoc.querySelector('rss') !== null; | ||
|
||
if (!isRSS) { | ||
throw new Error('not a RSS document'); | ||
} | ||
|
||
return parser.parseFromString(response.data.contents, 'text/xml'); | ||
}; | ||
|
||
const parseRss = (rssDocument) => { | ||
const items = rssDocument.querySelectorAll('item'); | ||
items.forEach((item) => { | ||
const title = item.querySelector('title'); | ||
const description = item.querySelector('description'); | ||
const link = item.querySelector('link'); | ||
|
||
parsedData.posts.push({ | ||
id: _.uniqueId('item_'), | ||
title: title.textContent, | ||
description: description.textContent, | ||
link: link.textContent, | ||
}); | ||
}); | ||
|
||
const feedTitle = rssDocument.querySelector('title'); | ||
const feedDesc = rssDocument.querySelector('description'); | ||
|
||
parsedData.feed.push({ | ||
id: _.uniqueId('feed_'), | ||
title: feedTitle.textContent, | ||
description: feedDesc.textContent, | ||
}); | ||
}; | ||
|
||
export { normalizeUrl, normalizeError }; | ||
export { normalizeUrl, parseXML, parseRss }; |
Oops, something went wrong.