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;