48 lines
1.0 KiB
JavaScript
48 lines
1.0 KiB
JavaScript
import { createSlice, nanoid } from "@reduxjs/toolkit";
|
|
import { sub } from "date-fns";
|
|
|
|
const initialState = [
|
|
{
|
|
id: "1",
|
|
title: "Learning Redux Toolkit",
|
|
content: "I've heard good things.",
|
|
date: sub(new Date(), { minutes: 10 }).toISOString(),
|
|
},
|
|
{
|
|
id: "2",
|
|
title: "Slice...",
|
|
content: "The more I say slice, the more I want pizza.",
|
|
date: sub(new Date(), { minutes: 5 }).toISOString(),
|
|
},
|
|
];
|
|
|
|
const postsSlice = createSlice({
|
|
name: "posts",
|
|
initialState,
|
|
reducers: {
|
|
postAdded: {
|
|
reducer: (state, action) => {
|
|
state.push(action.payload);
|
|
},
|
|
prepare(title, content, userId) {
|
|
// adding new post also need userid (author)
|
|
return {
|
|
payload: {
|
|
id: nanoid(),
|
|
title,
|
|
content,
|
|
userId,
|
|
date: new Date().toISOString(),
|
|
},
|
|
};
|
|
},
|
|
},
|
|
},
|
|
});
|
|
|
|
export const selectAllPosts = (state) => state.posts;
|
|
|
|
export const { postAdded } = postsSlice.actions;
|
|
|
|
export default postsSlice.reducer;
|