From 47e543f2b4f76515a21534b8b67448a0e3b2ce74 Mon Sep 17 00:00:00 2001
From: Jason Zhu
Date: Tue, 7 Mar 2023 15:28:14 +1100
Subject: [PATCH] 02_lesson: Added ReactionButton for emoji thumbs up/down
---
.../src/features/posts/PostsList.js | 10 +++---
.../src/features/posts/ReactionButton.js | 32 +++++++++++++++++++
.../src/features/posts/postsSlice.js | 30 ++++++++++++++++-
3 files changed, 67 insertions(+), 5 deletions(-)
create mode 100644 02_lesson_starter/src/features/posts/ReactionButton.js
diff --git a/02_lesson_starter/src/features/posts/PostsList.js b/02_lesson_starter/src/features/posts/PostsList.js
index 3a1a5aa..bf04115 100644
--- a/02_lesson_starter/src/features/posts/PostsList.js
+++ b/02_lesson_starter/src/features/posts/PostsList.js
@@ -1,7 +1,8 @@
-import { useSelector } from "react-redux";
-import { selectAllPosts } from "./postsSlice";
-import PostAuthor from "./PostAuthor";
-import TimeAgo from "./TimeAgo";
+import { useSelector } from 'react-redux';
+import { selectAllPosts } from './postsSlice';
+import PostAuthor from './PostAuthor';
+import TimeAgo from './TimeAgo';
+import ReactionButtons from './ReactionButton';
const PostsList = () => {
const posts = useSelector(selectAllPosts);
@@ -18,6 +19,7 @@ const PostsList = () => {
+
));
diff --git a/02_lesson_starter/src/features/posts/ReactionButton.js b/02_lesson_starter/src/features/posts/ReactionButton.js
new file mode 100644
index 0000000..6307d0c
--- /dev/null
+++ b/02_lesson_starter/src/features/posts/ReactionButton.js
@@ -0,0 +1,32 @@
+import { useDispatch } from 'react-redux';
+import { reactionAdded } from './postsSlice';
+
+const reactionEmoji = {
+ thumbsUp: '👍',
+ wow: '😮',
+ heart: '❤️',
+ rocket: '🚀',
+ coffee: '☕',
+};
+
+const ReactionButtons = ({ post }) => {
+ const dispatch = useDispatch();
+ const reactionButtons = Object.entries(reactionEmoji).map(([name, emoji]) => {
+ return (
+
+ );
+ });
+
+ return {reactionButtons}
;
+};
+
+export default ReactionButtons;
diff --git a/02_lesson_starter/src/features/posts/postsSlice.js b/02_lesson_starter/src/features/posts/postsSlice.js
index 406d984..b503e41 100644
--- a/02_lesson_starter/src/features/posts/postsSlice.js
+++ b/02_lesson_starter/src/features/posts/postsSlice.js
@@ -7,12 +7,26 @@ const initialState = [
title: "Learning Redux Toolkit",
content: "I've heard good things.",
date: sub(new Date(), { minutes: 10 }).toISOString(),
+ reactions: {
+ thumbsUp: 0,
+ wow: 0,
+ heart: 0,
+ rocket: 0,
+ coffee: 0,
+ },
},
{
id: "2",
title: "Slice...",
content: "The more I say slice, the more I want pizza.",
date: sub(new Date(), { minutes: 5 }).toISOString(),
+ reactions: {
+ thumbsUp: 0,
+ wow: 0,
+ heart: 0,
+ rocket: 0,
+ coffee: 0,
+ },
},
];
@@ -33,15 +47,29 @@ const postsSlice = createSlice({
content,
userId,
date: new Date().toISOString(),
+ reactions: {
+ thumbsUp: 0,
+ wow: 0,
+ heart: 0,
+ rocket: 0,
+ coffee: 0,
+ },
},
};
},
},
+ reactionAdded(state, action) {
+ const { postId, reaction } = action.payload;
+ const existingPost = state.find((post) => post.id === postId);
+ if (existingPost) {
+ existingPost.reactions[reaction]++; // this kind of immer action can only happen in slice
+ }
+ },
},
});
export const selectAllPosts = (state) => state.posts;
-export const { postAdded } = postsSlice.actions;
+export const { postAdded, reactionAdded } = postsSlice.actions;
export default postsSlice.reducer;