Skip to content

Commit

Permalink
add app
Browse files Browse the repository at this point in the history
  • Loading branch information
Tatyana-js committed Jun 4, 2024
1 parent e4fb582 commit 4fff0de
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 21 deletions.
12 changes: 7 additions & 5 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ const state = {
form: {
status: 'pending', // 'processed',
errors: [],
isValid: false,
},
loadingProcess: {
status: 'waiting', // 'sending', 'finished'
Expand Down Expand Up @@ -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);
Expand All @@ -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);
});
});
Expand Down
2 changes: 1 addition & 1 deletion src/locales/ru.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export default {
button: 'Добавить',
errors: {
existsRss: 'RSS уже существует',
invalidUrl: 'Ресурс не содержит валидный RSS',
invalidUrl: 'Ссылка должна быть валидным URL',
},
},
};
18 changes: 10 additions & 8 deletions src/parser.js
Original file line number Diff line number Diff line change
@@ -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 };
};
17 changes: 10 additions & 7 deletions src/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,31 @@ 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');
errorElement.classList.remove('text-success');
errorElement.classList.add('text-danger');
errorElement.textContent = t('errors.invalidUrl');
break;
case 'loadingProcess.status':
if (value === 'sending') {
button.disabled = true;
}
break;
default:
break;
}
});
return {
Expand Down

0 comments on commit 4fff0de

Please sign in to comment.