diff --git a/src/components/widgets/ReactionIcons.jsx b/src/components/widgets/ReactionIcons.jsx index ce895ad1..f24649ce 100644 --- a/src/components/widgets/ReactionIcons.jsx +++ b/src/components/widgets/ReactionIcons.jsx @@ -5,48 +5,37 @@ import PropTypes from 'prop-types'; import { getTotalReactions, articleReaction } from '../../actions/articleReactionsAction'; class ReactionIcons extends Component { - state = { - likeClicked: false, - dislikeClicked: false - }; - componentDidMount = () => { this.props.getTotalReactions(this.props.selectedArticleId); }; - createLikeIcon = (outline = 'outline') => { - const name = outline.length === 0 ? 'thumbs up' : `thumbs up ${outline}`; - return ( - - - - ); - }; + createLikeIcon = newReaction => ( + + + + ); - createDislikeIcon = (outline = 'outline') => { - const name = outline.length === 0 ? 'thumbs down' : `thumbs down ${outline}`; - return ( - - - - ); - }; + createDislikeIcon = () => ( + + + + ); - createReaction = (reaction) => { - this.props.articleReaction(this.props.selectedArticleId, reaction); + createReaction = (newReaction) => { + this.props.articleReaction(this.props.selectedArticleId, newReaction); + setTimeout(() => this.props.getTotalReactions(this.props.selectedArticleId), 100); }; render() { const { date, numberofcomments, totalArticleReactions } = this.props; const { likes, dislikes } = totalArticleReactions.successResponse; - const { likeClicked, dislikeClicked } = this.state; - const outline = ''; + return ( {date} - {likeClicked ? this.createLikeIcon(outline) : this.createLikeIcon()} + {this.createLikeIcon()} {likes} - {dislikeClicked ? this.createDislikeIcon(outline) : this.createDislikeIcon()} + {this.createDislikeIcon()} {dislikes} @@ -71,7 +60,8 @@ ReactionIcons.propTypes = { }; const mapStateToProps = state => ({ - totalArticleReactions: state.totalArticleReactions + totalArticleReactions: state.totalArticleReactions, + newArticleReaction: state.newArticleReaction }); export { ReactionIcons as ReactionIconsPage }; diff --git a/src/components/widgets/test/ReactionIcons.test.js b/src/components/widgets/test/ReactionIcons.test.js index 3c735d39..34c04aa8 100644 --- a/src/components/widgets/test/ReactionIcons.test.js +++ b/src/components/widgets/test/ReactionIcons.test.js @@ -6,17 +6,17 @@ import { getTotalReactions } from '../../../actions/articleReactionsAction'; describe('Test for reaction Icons', () => { it('should have initial state of false and change when clicked ', () => { + const articleReaction = jest.fn(); const wrapper = shallow( ); expect(wrapper).toMatchSnapshot(); - - expect(wrapper.state().likeClicked).toEqual(false); }); it('should have initial state of false and change when clicked ', () => { @@ -32,6 +32,5 @@ describe('Test for reaction Icons', () => { expect(wrapper).toMatchSnapshot(); wrapper.instance().createReaction(); - expect(wrapper.state().dislikeClicked).toEqual(false); }); }); diff --git a/src/components/widgets/test/__snapshots__/ReactionIcons.test.js.snap b/src/components/widgets/test/__snapshots__/ReactionIcons.test.js.snap index eb63dfc3..1c19a880 100644 --- a/src/components/widgets/test/__snapshots__/ReactionIcons.test.js.snap +++ b/src/components/widgets/test/__snapshots__/ReactionIcons.test.js.snap @@ -16,7 +16,7 @@ exports[`Test for reaction Icons should have initial state of false and change w disabled={true} fitted={true} link={true} - name="thumbs up thumbs up outline" + name="thumbs up outline" size="small" /> @@ -30,7 +30,7 @@ exports[`Test for reaction Icons should have initial state of false and change w disabled={true} fitted={true} link={true} - name="thumbs down thumbs down outline" + name="thumbs down outline" size="small" /> @@ -67,7 +67,7 @@ exports[`Test for reaction Icons should have initial state of false and change w disabled={true} fitted={true} link={true} - name="thumbs up thumbs up outline" + name="thumbs up outline" size="small" /> @@ -81,7 +81,7 @@ exports[`Test for reaction Icons should have initial state of false and change w disabled={true} fitted={true} link={true} - name="thumbs down thumbs down outline" + name="thumbs down outline" size="small" /> diff --git a/src/reducers/articleReactionsReducer.js b/src/reducers/articleReactionsReducer.js new file mode 100644 index 00000000..01126f5d --- /dev/null +++ b/src/reducers/articleReactionsReducer.js @@ -0,0 +1,18 @@ +import actionTypes from '../actions/articleReactionsAction/actionTypes'; +import initialState from './initialState'; + +const { ARTICLE_REACTION_SUCCESS } = actionTypes; + +const { articleReaction } = initialState; + +export default (state = articleReaction, action) => { + switch (action.type) { + case ARTICLE_REACTION_SUCCESS: + return { + success: true, + newReaction: action.payload + }; + default: + return state; + } +}; diff --git a/src/reducers/index.js b/src/reducers/index.js index 57440cab..7f9070d0 100644 --- a/src/reducers/index.js +++ b/src/reducers/index.js @@ -1,9 +1,16 @@ import { combineReducers } from 'redux'; -import { trendingArticlesReducer, popularArticlesReducer, FeaturedArticlesReducer, TagsReducer, CategoriesReducer } from './articlesReducers/fetchArticlesReducers'; +import { + trendingArticlesReducer, + popularArticlesReducer, + FeaturedArticlesReducer, + TagsReducer, + CategoriesReducer +} from './articlesReducers/fetchArticlesReducers'; import authReducer from './authReducer'; import socialLogin from './socialLoginReducer'; import readArticle from './readArticleReducer'; import totalArticleReactions from './totalArticleReactionReducer'; +import newArticleReaction from './articleReactionsReducer'; const rootReducer = combineReducers({ trendingArticlesReducer, @@ -14,7 +21,8 @@ const rootReducer = combineReducers({ authReducer, socialLogin, readArticle, - totalArticleReactions + totalArticleReactions, + newArticleReaction }); export default rootReducer; diff --git a/src/reducers/initialState.js b/src/reducers/initialState.js index 0571ae99..d749fd8a 100644 --- a/src/reducers/initialState.js +++ b/src/reducers/initialState.js @@ -18,18 +18,17 @@ const initialState = { featuredArticles: { featuredArticlesResponse: [], featuredArticlesfailure: false, - featuredArticlesSuccess: false, + featuredArticlesSuccess: false }, categories: { categoriesResponseSuccess: false, categoriesResponse: [], - categoriesResponsefailure: false, + categoriesResponsefailure: false }, tags: { tagsResponseSuccess: false, tagsResponse: [], - tagsResponsefailure: false, - + tagsResponsefailure: false }, readArticle: { articleIsLoading: false, @@ -42,6 +41,10 @@ const initialState = { success: false, successResponse: {}, failureResponse: {} + }, + articleReaction: { + success: false, + newReaction: '' } }; diff --git a/src/reducers/test/articleReactionReducer.test.js b/src/reducers/test/articleReactionReducer.test.js new file mode 100644 index 00000000..cb711583 --- /dev/null +++ b/src/reducers/test/articleReactionReducer.test.js @@ -0,0 +1,29 @@ +import actionTypes from '../../actions/articleReactionsAction/actionTypes'; +import initialState from '../initialState'; +import articleReactionsReducer from '../articleReactionsReducer'; + +const { ARTICLE_REACTION_SUCCESS } = actionTypes; + +describe(' article reaction reducer', () => { + it('should return the initial state', () => { + expect(articleReactionsReducer(undefined, {})).toEqual(initialState.articleReaction); + }); + + it('should update state when the start action is dispatched', () => { + const action = { + payload: 'dislike' + }; + expect( + articleReactionsReducer(initialState.articleReaction, { + type: ARTICLE_REACTION_SUCCESS, + payload: 'dislike' + }) + ).toEqual( + (initialState, + { + success: true, + newReaction: action.payload + }) + ); + }); +});