diff --git a/src/app.js b/src/app.js index 58613ac..50b69b5 100644 --- a/src/app.js +++ b/src/app.js @@ -25,7 +25,6 @@ const state = { form: { status: 'pending', // 'processed', errors: [], - isValid: false, }, loadingProcess: { status: 'waiting', // 'sending', 'finished' @@ -57,7 +56,9 @@ export default () => { resources: { ru }, }).then(() => { const { watchedState, renderForm } = watch(elements, i18n, state); + renderForm(); + elements.form.addEventListener('submit', async (e) => { e.preventDefault(); const formData = new FormData(e.target); @@ -69,20 +70,21 @@ export default () => { validate(urlTarget, urlFeeds) .then(({ url }) => axios.get(createLink(url)) .then((responce) => { + console.log(responce); const parseData = parse(responce.data); + console.log(parseData); const { feed, posts } = parseData; const id = uniqueId(); watchedState.feeds.push({ ...feed, feedId: id, link: urlTarget }); posts.forEach((post) => watchedState.posts.push({ ...post, id })); - watchedState.form.isValid = true; watchedState.loadingProcess.status = 'finished'; - console.log(posts); + watchedState.loadingProcess.error = ''; }) .catch((error) => { - + console.log(error); + watchedState.form.errors.push(error); })) .catch((error) => { - watchedState.form.isValid = false; watchedState.form.errors.push(error); }); }); diff --git a/src/locales/ru.js b/src/locales/ru.js index 023fc08..d9664bb 100644 --- a/src/locales/ru.js +++ b/src/locales/ru.js @@ -7,7 +7,7 @@ export default { button: 'Добавить', errors: { existsRss: 'RSS уже существует', - invalidUrl: 'Ресурс не содержит валидный RSS', + invalidUrl: 'Ссылка должна быть валидным URL', }, }, }; diff --git a/src/parser.js b/src/parser.js index 91c8f0c..0dda365 100644 --- a/src/parser.js +++ b/src/parser.js @@ -1,20 +1,22 @@ export default (data) => { const parser = new DOMParser(); const doc = parser.parseFromString(data, 'text/xml'); + console.log(doc); const errorNode = doc.querySelector('parsererror'); if (errorNode) { throw new Error('parsererror'); } - const feedTitle = doc.querySelector('title').textContent; + const feedTitle = doc.querySelector('channel > title').textContent; + console.log(feedTitle); const feedDescription = doc.querySelector('description').textContent; const feed = { title: feedTitle, description: feedDescription }; - - const posts = doc.querySelectorAll('items'); - Array.from(posts).forEach((post) => { - const postTitle = post.querySelector('title').textContent; - const postDescription = post.querySelector('description').textContent; - const postLink = post.querySelector('link').textContent; - return { title: postTitle, description: postDescription, link: postLink }; + console.log(feed); + const items = doc.querySelectorAll('items'); + const posts = Array.from(items).map((post) => { + const title = post.querySelector('title').textContent; + const description = post.querySelector('description').textContent; + return { title, description }; }); + console.log(feed, posts); return { feed, posts }; }; diff --git a/src/view.js b/src/view.js index c6c8559..62c0ee0 100644 --- a/src/view.js +++ b/src/view.js @@ -5,20 +5,17 @@ export default (elements, i18n, state) => { focus(); const { t } = i18n; - const renderValid = () => { - }; - const renderForm = () => { Object.entries(elements.staticEl).forEach(([key, value]) => { const element = value; element.textContent = t(`${key}`); }); }; - const watchedState = onChange(state, (path) => { - const { errorElement, input } = elements; + const watchedState = onChange(state, (path, value) => { + const { errorElement, input, button } = elements; switch (path) { - case 'form.isValid': - renderValid(); + case 'pending': + renderForm(); break; case 'form.errors': input.classList.add('is-invalid'); @@ -26,7 +23,13 @@ export default (elements, i18n, state) => { errorElement.classList.add('text-danger'); errorElement.textContent = t('errors.invalidUrl'); break; + case 'loadingProcess.status': + if (value === 'sending') { + button.disabled = true; + } + break; default: + break; } }); return {