Skip to content

Commit 132ce3d

Browse files
committed
solution: useReducer message
1 parent 9448dd9 commit 132ce3d

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

src/components/Message/useReducer.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,17 @@ import "./styles.css";
88
export const messageReducer = (state, action) => {
99
switch (action.type) {
1010
case "RETWEET":
11-
// TODO: retweet
11+
return {
12+
...state,
13+
retweeted: !state.retweeted,
14+
retweet_count: state.retweet_count + (!state.retweeted ? 1 : -1)
15+
};
1216
case "FAVORITE":
13-
// TODO: favorite
17+
return {
18+
...state,
19+
favorited: !state.favorited,
20+
favorite_count: state.favorite_count + (!state.favorited ? 1 : -1)
21+
};
1422
default:
1523
return state;
1624
}
@@ -51,30 +59,30 @@ export const Message = props => {
5159
<div className="Message_Body">{text}</div>
5260
<div className="Message_Footer">
5361
<Icon icon="comment" title="comment" />
54-
<IconCountWrapper title="retweet_count" count={0}>
62+
<IconCountWrapper title="retweet_count" count={state.retweet_count}>
5563
<IconButton
5664
role="retweet"
5765
onClick={() => {
58-
/* toggle retweet */
66+
dispatch({ type: "RETWEET" });
5967
}}
6068
>
6169
<Icon
6270
icon="retweet"
63-
active={false}
71+
active={state.retweeted}
6472
highlight="rgb(23, 191, 99)"
6573
/>
6674
</IconButton>
6775
</IconCountWrapper>
68-
<IconCountWrapper title="favorite_count" count={0}>
76+
<IconCountWrapper title="favorite_count" count={state.favorite_count}>
6977
<IconButton
7078
role="favorite"
7179
onClick={() => {
72-
/* toggle favorite */
80+
dispatch({ type: "FAVORITE" });
7381
}}
7482
>
7583
<Icon
7684
icon="favorite"
77-
active={false}
85+
active={state.favorited}
7886
highlight="rgb(224, 36, 94)"
7987
/>
8088
</IconButton>

0 commit comments

Comments
 (0)