Advertisement
Guest User

Untitled

a guest
Jan 25th, 2023
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { createSlice } from '@reduxjs/toolkit';
  2.  
  3. export const postsSlice = createSlice({
  4.     name: 'post',
  5.     initialState: {
  6.         lastID: 0,
  7.         posts: [],
  8.         authors: []
  9.     },
  10.     reducers: {
  11.         createPost: (state, action) => {
  12.             state.posts.push({
  13.                 id: ++state.lastID,
  14.                 author: action.payload.author,
  15.                 text: action.payload.text
  16.             });
  17.             if(!state.authors.includes(action.payload.author))
  18.                 state.authors.push(action.payload.author);
  19.         }
  20.     }
  21. });
  22.  
  23. export const { createPost } = postsSlice.actions;
  24.  
  25. export const selectPosts = (state) => state.post.posts;
  26. export const selectAuthors = (state) => state.post.authors;
  27.  
  28. export const selectPostsByAuthor = postAuthor => state => {
  29.     return state.post.posts.filter(({ author }) => postAuthor === author).length;
  30. };
  31.  
  32. export default postsSlice.reducer;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement