Created
September 17, 2017 15:55
-
-
Save philipszdavido/531ab1e41d321d67844cb9f43092356a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//src/app/redux/reducer/articles.ts | |
import * as articles from '../actions/articles'; | |
import { Article } from '../models/article'; | |
export interface State { | |
articles : Array<Article>, | |
}; | |
export const initialState : State = { | |
articles : [], | |
}; | |
export function reducer | |
(state = initialState, action:articles.Actions) : State { | |
switch (action.type) { | |
case articles.ActionTypes.ADD_ARTICLE: { | |
return { | |
articles: [...state.articles,action.payload], | |
} | |
} | |
case articles.ActionTypes.DECREMENT_ARTICLE_POINT: { | |
return Object.assign({},state,{ | |
articles: state.articles.map( | |
(article: Article) => article.id == action.payload.id ? Object.assign({},article,--action.payload.points) : article | |
) | |
}) | |
} | |
case articles.ActionTypes.INCREMENT_ARTICLE_POINT: { | |
return Object.assign({},state,{ | |
articles: state.articles.map( | |
(article: Article) => article.id == action.payload.id ? Object.assign({},article,++action.payload.points) : article | |
) | |
}) | |
} | |
default: { | |
return state; | |
} | |
} | |
}; | |
export const getArticles = (state : State) => state.articles; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment