From 963757a7b9708d5d656455205907188f3987eece Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A2=D0=B0=D1=82=D1=8C=D1=8F=D0=BD=D0=B0=20=D0=90=D0=BD?= =?UTF-8?q?=D0=B4=D1=80=D0=B5=D0=B5=D0=B2=D0=B0?= Date: Wed, 29 May 2024 17:55:58 +0300 Subject: [PATCH] add index.js --- index.html | 14 ------ src/app.js | 105 ++++++++++++++++++++++--------------------- src/index.js | 3 +- src/locales/index.js | 3 ++ src/view.js | 13 +++--- 5 files changed, 64 insertions(+), 74 deletions(-) create mode 100644 src/locales/index.js diff --git a/index.html b/index.html index 030745b..5893b3e 100644 --- a/index.html +++ b/index.html @@ -47,18 +47,4 @@

RSS агрегатор

- diff --git a/src/app.js b/src/app.js index a10d4da..01b79ff 100644 --- a/src/app.js +++ b/src/app.js @@ -3,68 +3,69 @@ import * as yup from 'yup'; import axios from 'axios'; import validate, { proxyObj } from './utils.js'; import watch from './view.js'; -import resources from './locales/ru.js'; +import resources from './locales/index.js'; -export default () => { - const defaultLanguage = 'ru'; - const i18n = i18next.createInstance(); - i18n.init({ - lng: defaultLanguage, - debug: true, - resources, - }); - - const elements = { +const elements = { + staticEl: { title: document.querySelector('h1'), subtitle: document.querySelector('.lead'), - form: document.querySelector('form'), - input: document.getElementById('url-input'), label: document.querySelector('[for="url-input"]'), example: document.querySelector('p.mt-2.mb-0.text-secondary'), button: document.querySelector('[type="submit"]'), - }; - - const state = { - form: { - status: 'pending', // 'processed', - errors: [], - isValid: false, - }, - loadingProcess: { - status: 'waiting', - error: '', - }, - posts: [], - feeds: [], - ui: { - activePostsId: null, - touchedPostId: [], - }, - }; - const { watchedState, renderForm } = watch(elements, i18n, state); + }, + form: document.querySelector('form'), + input: document.getElementById('url-input'), +}; - yup.setLocale({ - string: { - url: () => ({ key: 'errors.invalidRss' }), - }, - mixed: { - required: () => ({ key: 'errors.required' }), - notoneOf: () => ({ key: 'errors.existsRss' }), - }, - }); +const state = { + form: { + status: 'pending', // 'processed', + errors: [], + isValid: false, + }, + loadingProcess: { + status: 'waiting', + error: '', + }, + posts: [], + feeds: [], + ui: { + activePostsId: null, + touchedPostId: [], + }, +}; - renderForm(); +yup.setLocale({ + string: { + url: () => ({ key: 'errors.invalidRss' }), + }, + mixed: { + required: () => ({ key: 'errors.required' }), + notoneOf: () => ({ key: 'errors.existsRss' }), + }, +}); - elements.form.addEventListener('submit', async (e) => { - e.preventDefault(); - const formData = new FormData(e.target); - const urlTarget = formData.get('url').trim(); - const urlFeeds = watchedState.feeds.map(({ url }) => url); +export default () => { + const defaultLanguage = 'ru'; + const i18n = i18next.createInstance(); + i18n.init({ + lng: defaultLanguage, + debug: true, + resources, + }).then(() => { + const { watchedState, renderForm } = watch(elements, i18n, state); + renderForm(); + elements.form.addEventListener('submit', async (e) => { + e.preventDefault(); + const formData = new FormData(e.target); + const urlTarget = formData.get('url').trim(); + const urlFeeds = watchedState.feeds.map(({ url }) => url); - watchedState.loadingProcess.state = 'sending'; + watchedState.loadingProcess.state = 'sending'; - validate(urlTarget, urlFeeds) - .then(({ url }) => axios.get(proxyObj(url))) - .catch(() => {}); + validate(urlTarget, urlFeeds) + .then(({ url }) => axios.get(proxyObj(url))) + .catch(() => {}); + }); }); }; diff --git a/src/index.js b/src/index.js index 92b8013..0a1f3d8 100644 --- a/src/index.js +++ b/src/index.js @@ -1,4 +1,5 @@ import './styles.scss'; import 'bootstrap'; +import app from './app.js'; -console.log('Hello World!'); +app(); diff --git a/src/locales/index.js b/src/locales/index.js new file mode 100644 index 0000000..4550c3f --- /dev/null +++ b/src/locales/index.js @@ -0,0 +1,3 @@ +import ru from './ru.js'; + +export default { ru }; diff --git a/src/view.js b/src/view.js index 69fac84..4587b1c 100644 --- a/src/view.js +++ b/src/view.js @@ -3,17 +3,16 @@ import onChange from 'on-change'; export default (elements, i18n, state) => { const { t } = i18n; const renderForm = () => { - Object.entries(elements).forEach(([key, value]) => { + Object.entries(elements.staticEl).forEach(([key, value]) => { const element = value; - element.textContent = t(`${key}`) ?? ''; + element.textContent = t(`${key}`); }); }; const watchedState = onChange(state, (path) => { - if (path === 'isValid') { - /* empty */ - } else if (path === 'errors') { - // прописать ошибки// - } + // switch (path) { + // case 'form.isValid': + + // } }); return { watchedState,