import Immutable from 'immutable' ; import { EArticleFilter , IArticle , IArticlesState , IIArticlesState , IReduxAction , } from '../../schemas' ; import Utils from '../../utils' ; import { ArticlesActionTypes , ISetArticlesFilterPayload , ISetArticlesPayload , ISetCurrentArticlePayload , ISetIsFetchingArticlesPayload , ISetIsUpdatingCurrentArticlePayload , } from '../actions' ; const initialArticlesState = Immutable . Record < IArticlesState > ( { current : null , filter : EArticleFilter . ALL , isFetching : false , isUpdatingCurrent : false , list : Immutable . List < IArticle > ( [ ] ) , } ) ( ) ; const articlesReducer = ( state = initialArticlesState , action ) => { const payload = action . payload ; switch ( action . type ) { case ArticlesActionTypes . SET_CURRENT_ARTICLE : return handleSetCurrentArticle ( state , payload ) ; case ArticlesActionTypes . SET_ARTICLES_FILTER : return handleSetArticlesFilter ( state , payload ) ; case ArticlesActionTypes . SET_IS_FETCHING : return handleSetIsFetchingArticle ( state , payload ) ; case ArticlesActionTypes . SET_IS_UPDATING_CURRENT : return handleSetIsUpdatingCurrentArticle ( state , payload ) ; case ArticlesActionTypes . SET_ARTICLES : return handleSetArticles ( state , payload ) ; default : return state ; } } ; function handleSetArticles ( state , payload ) { let lastDateTime = '' ; payload . articles . forEach ( ( article ) => { const dateTime = Utils . timeToDateString ( article . time ) ; if ( lastDateTime !== dateTime ) { lastDateTime = dateTime ; article . isDayFirst = true ; } } ) ; return state . set ( 'list' , Immutable . List < IArticle > ( payload . articles ) ) ; } function handleSetCurrentArticle ( state , payload ) { return state . set ( 'current' , payload . article ) ; } function handleSetArticlesFilter ( state , payload ) { return state . set ( 'filter' , payload . filter ) ; } function handleSetIsFetchingArticle ( state , payload ) { return state . set ( 'isFetching' , payload . isFetching ) ; } function handleSetIsUpdatingCurrentArticle ( state , payload ) { return state . set ( 'isUpdatingCurrent' , payload . isUpdatingCurrent ) ; } export default articlesReducer ;