Skip to content

Commit

Permalink
add index.js
Browse files Browse the repository at this point in the history
  • Loading branch information
Tatyana-js committed May 29, 2024
1 parent 2daacfc commit 963757a
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 74 deletions.
14 changes: 0 additions & 14 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,4 @@ <h1 class="display-3 mb-0">RSS агрегатор</h1>
</div>
</footer>
</body>
<script>
if ("serviceWorker" in navigator) {
window.addEventListener("load", () => {
navigator.serviceWorker
.register("service-worker.js")
.then((registration) => {
console.log("Service Worker registered: ", registration);
})
.catch((registrationError) => {
console.error("Service Worker registration failed: ", registrationError);
});
});
}
</script>
</html>
105 changes: 53 additions & 52 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {});
});
});
};
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import './styles.scss';
import 'bootstrap';
import app from './app.js';

console.log('Hello World!');
app();
3 changes: 3 additions & 0 deletions src/locales/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import ru from './ru.js';

export default { ru };
13 changes: 6 additions & 7 deletions src/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {

Check failure on line 11 in src/view.js

View workflow job for this annotation

GitHub Actions / build (18.x)

'path' is defined but never used
if (path === 'isValid') {
/* empty */
} else if (path === 'errors') {
// прописать ошибки//
}
// switch (path) {
// case 'form.isValid':

// }
});
return {
watchedState,
Expand Down

0 comments on commit 963757a

Please sign in to comment.