File tree Expand file tree Collapse file tree 5 files changed +58
-43
lines changed Expand file tree Collapse file tree 5 files changed +58
-43
lines changed Original file line number Diff line number Diff line change @@ -121,6 +121,6 @@ <h1 id="main-title" class="display-3 mb-0">RSS агрегатор</h1>
121121 </ div >
122122 </ footer >
123123 < deepl-input-controller translate ="no "> </ deepl-input-controller >
124- < script type ="module " src ="./js/main .js "> </ script >
124+ < script type ="module " src ="./js/index .js "> </ script >
125125 </ body >
126126</ html >
Original file line number Diff line number Diff line change 1+ import "../i18n.js" ;
2+ import { initApp } from "./main.js" ;
3+
4+ initApp ( ) ;
5+
6+ // validateInput -> fetchRssData -> parseRss -> state -> render
Load Diff This file was deleted.
Original file line number Diff line number Diff line change 1- import "../i18n.js" ;
2- import { initApp } from "./initApp.js" ;
1+ import { renderUIText } from "./view.js" ;
2+ import {
3+ updateInputValue ,
4+ addRssFeed ,
5+ validateInput ,
6+ setActivePost ,
7+ checkRssFeed ,
8+ } from "./model.js" ;
9+
10+ export const initApp = ( ) => {
11+ renderUIText ( ) ;
12+
13+ const input = document . querySelector ( "#url-input" ) ;
14+ const form = document . querySelector ( "#rss-form" ) ;
15+ const postsContainer = document . querySelector ( ".posts" ) ;
16+
17+ setInterval ( checkRssFeed , 5000 ) ;
18+
19+ input . addEventListener ( "input" , ( e ) => {
20+ updateInputValue ( e . target . value ) ;
21+ } ) ;
22+
23+ form . addEventListener ( "submit" , ( e ) => {
24+ e . preventDefault ( ) ;
25+ validateInput ( )
26+ . then ( ( ) => addRssFeed ( ) )
27+ . catch ( ( ) => console . log ( "валидация не пройдена" ) ) ;
28+ } ) ;
29+
30+ postsContainer . addEventListener ( "click" , ( e ) => {
31+ const button = e . target . closest ( ".modal-btn" ) ;
32+ console . log ( "id:" , button . dataset . id ) ;
33+ if ( button && button . dataset . id ) {
34+ setActivePost ( button . dataset . id ) ;
35+ }
36+ } ) ;
37+ } ;
38+
339
4- initApp ( ) ;
Original file line number Diff line number Diff line change @@ -39,10 +39,14 @@ export const updateInputValue = (value) => {
3939 if ( state . form . errors ) state . form . errors = null ;
4040} ;
4141
42- export const addRssFeed = ( ) => {
43- return fetchRssData ( state . form . inputValue )
44- . then ( ( xmlString ) => parseRss ( xmlString ) )
45- . then ( ( { channel, items } ) => {
42+ const fetchAndParseFeed = ( url ) => {
43+ return fetchRssData ( url ) . then ( ( xmlString ) => parseRss ( xmlString ) ) . catch ( ( error ) => {
44+ console . error ( "fetchAndParseRss:" , error ) ;
45+ throw error ;
46+ } )
47+ } ;
48+
49+ const updateFeedsAndPostsState = ( { channel, items} ) => {
4650 if ( state . feeds . some ( ( feed ) => feed . link === channel . link ) ) {
4751 state . form . errors = i18next . t ( "no_duplicate" ) ;
4852 return ;
@@ -53,7 +57,11 @@ export const addRssFeed = () => {
5357 state . posts = [ ...state . posts , ...newItems ] ;
5458 state . form . inputValue = "" ;
5559 console . log ( "обновлён список rss" , state . feeds ) ;
56- } )
60+ } ;
61+
62+ export const addRssFeed = ( ) => {
63+ return fetchAndParseFeed ( state . form . inputValue )
64+ . then ( ( { channel, items } ) => updateFeedsAndPostsState ( { channel, items } ) )
5765 . catch ( ( error ) => {
5866 throw error ;
5967 } ) ;
You can’t perform that action at this time.
0 commit comments