Skip to content

Commit

Permalink
Implement reducers for article ReactionIcons
Browse files Browse the repository at this point in the history
- Create reducer file
- Add initial state
- Update state with database response
- Update view(Component) with new data
  • Loading branch information
ibrahimalausa committed Mar 2, 2019
1 parent 4a19285 commit 1ae66d1
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 41 deletions.
46 changes: 18 additions & 28 deletions src/components/widgets/ReactionIcons.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<i role="presentation" onClick={this.createReaction.bind(this, 'like')}>
<Icon disabled link size="small" fitted name={`thumbs up ${name}`} />
</i>
);
};
createLikeIcon = newReaction => (
<i role="presentation" onClick={this.createReaction.bind(this, 'like', newReaction)}>
<Icon disabled link size="small" fitted name="thumbs up outline" />
</i>
);

createDislikeIcon = (outline = 'outline') => {
const name = outline.length === 0 ? 'thumbs down' : `thumbs down ${outline}`;
return (
<i role="presentation" onClick={this.createReaction.bind(this, 'dislike')}>
<Icon disabled link size="small" fitted name={`thumbs down ${name}`} />
</i>
);
};
createDislikeIcon = () => (
<i role="presentation" onClick={this.createReaction.bind(this, 'dislike')}>
<Icon disabled link size="small" fitted name="thumbs down outline" />
</i>
);

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 (
<Fragment>
<i className="dates">{date}</i>
{likeClicked ? this.createLikeIcon(outline) : this.createLikeIcon()}
{this.createLikeIcon()}
{likes}
{dislikeClicked ? this.createDislikeIcon(outline) : this.createDislikeIcon()}
{this.createDislikeIcon()}
{dislikes}
<i className="articleComment">
<Icon disabled link size="small" fitted name="comments outline" />
Expand All @@ -71,7 +60,8 @@ ReactionIcons.propTypes = {
};

const mapStateToProps = state => ({
totalArticleReactions: state.totalArticleReactions
totalArticleReactions: state.totalArticleReactions,
newArticleReaction: state.newArticleReaction
});

export { ReactionIcons as ReactionIconsPage };
Expand Down
5 changes: 2 additions & 3 deletions src/components/widgets/test/ReactionIcons.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
<ReactionIconsPage
getTotalReactions={getTotalReactions}
selectedArticleId={props.match.params.title}
totalArticleReactions={props.totalArticleReactions}
articleReaction={articleReaction}
/>
);

expect(wrapper).toMatchSnapshot();

expect(wrapper.state().likeClicked).toEqual(false);
});

it('should have initial state of false and change when clicked ', () => {
Expand All @@ -32,6 +32,5 @@ describe('Test for reaction Icons', () => {

expect(wrapper).toMatchSnapshot();
wrapper.instance().createReaction();
expect(wrapper.state().dislikeClicked).toEqual(false);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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"
/>
</i>
Expand All @@ -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"
/>
</i>
Expand Down Expand Up @@ -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"
/>
</i>
Expand All @@ -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"
/>
</i>
Expand Down
18 changes: 18 additions & 0 deletions src/reducers/articleReactionsReducer.js
Original file line number Diff line number Diff line change
@@ -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;
}
};
12 changes: 10 additions & 2 deletions src/reducers/index.js
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -14,7 +21,8 @@ const rootReducer = combineReducers({
authReducer,
socialLogin,
readArticle,
totalArticleReactions
totalArticleReactions,
newArticleReaction
});

export default rootReducer;
11 changes: 7 additions & 4 deletions src/reducers/initialState.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -42,6 +41,10 @@ const initialState = {
success: false,
successResponse: {},
failureResponse: {}
},
articleReaction: {
success: false,
newReaction: ''
}
};

Expand Down
29 changes: 29 additions & 0 deletions src/reducers/test/articleReactionReducer.test.js
Original file line number Diff line number Diff line change
@@ -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
})
);
});
});

0 comments on commit 1ae66d1

Please sign in to comment.