02_lesson: Added usersSlice and attach userid to post

02_lesson
Jason Zhu 2023-03-07 13:12:54 +11:00
parent 1dc07f39e5
commit 83a42fd660
5 changed files with 44 additions and 5 deletions

View File

@ -1,6 +1,7 @@
import { configureStore } from "@reduxjs/toolkit"; import { configureStore } from "@reduxjs/toolkit";
import postsReducer from "../features/posts/postsSlice"; import postsReducer from "../features/posts/postsSlice";
import usersReducer from "../features/users/usersSlice";
export const store = configureStore({ export const store = configureStore({
reducer: { posts: postsReducer }, reducer: { posts: postsReducer, users: usersReducer },
}); });

View File

@ -1,23 +1,36 @@
import { useState } from "react"; import { useState } from "react";
import { postAdded } from "./postsSlice"; import { postAdded } from "./postsSlice";
import { useDispatch } from "react-redux"; import { useDispatch, useSelector } from "react-redux";
import { selectAllUsers } from "../users/usersSlice";
const AddPostForm = () => { const AddPostForm = () => {
const dispatch = useDispatch();
const [title, setTitle] = useState(""); // Temporary state for adding information in form const [title, setTitle] = useState(""); // Temporary state for adding information in form
const [content, setContent] = useState(""); const [content, setContent] = useState("");
const dispatch = useDispatch(); const [userId, setUserId] = useState("");
const users = useSelector(selectAllUsers); // get users from Redux store
const onTitleChanged = (e) => setTitle(e.target.value); const onTitleChanged = (e) => setTitle(e.target.value);
const onContentChanged = (e) => setContent(e.target.value); const onContentChanged = (e) => setContent(e.target.value);
const onAuthorChanged = (e) => setUserId(e.target.value);
const onSavePostClicked = () => { const onSavePostClicked = () => {
if (title && content) { if (title && content) {
dispatch(postAdded(title, content)); dispatch(postAdded(title, content, userId));
setTitle(""); setTitle("");
setContent(""); setContent("");
} }
}; };
const usersOptions = users.map((user) => (
<option key={user.id} value={user.id}>
{user.name}
</option>
));
return ( return (
<section> <section>
<h2>Add a New Post</h2> <h2>Add a New Post</h2>
@ -30,6 +43,11 @@ const AddPostForm = () => {
value={title} value={title}
onChange={onTitleChanged} onChange={onTitleChanged}
/> />
<label htmlFor="postAuthor">Author:</label>
<select id="postAuthor" value={userId} onChange={onAuthorChanged}>
<option value=""></option>
{usersOptions}
</select>
<label htmlFor="postContent">Content:</label> <label htmlFor="postContent">Content:</label>
<textarea <textarea
id="postContent" id="postContent"

View File

@ -7,6 +7,7 @@ const PostsList = () => {
const renderedPosts = posts.map((post) => ( const renderedPosts = posts.map((post) => (
<article key={post.id}> <article key={post.id}>
<h3>{post.title}</h3> <h3>{post.title}</h3>
<h4>{post.userId}</h4>
<p>{post.content.substring(0, 100)}</p> <p>{post.content.substring(0, 100)}</p>
</article> </article>
)); ));

View File

@ -21,12 +21,14 @@ const postsSlice = createSlice({
reducer: (state, action) => { reducer: (state, action) => {
state.push(action.payload); state.push(action.payload);
}, },
prepare(title, content) { prepare(title, content, userId) {
// adding new post also need userid (author)
return { return {
payload: { payload: {
id: nanoid(), id: nanoid(),
title, title,
content, content,
userId,
}, },
}; };
}, },

View File

@ -0,0 +1,17 @@
import { createSlice } from "@reduxjs/toolkit";
const initialState = [
{ id: "0", name: "Dude Lebowski" },
{ id: "1", name: "Neil Young" },
{ id: "2", name: "Dave Gray" },
];
const usersSlice = createSlice({
name: "users",
initialState,
reducers: {},
});
// Selectors
export const selectAllUsers = (state) => state.users;
export default usersSlice.reducer;