Skip to content

Commit d2c9731

Browse files
add addPost to posts slice
1 parent 9f8af7a commit d2c9731

File tree

4 files changed

+11
-4
lines changed

4 files changed

+11
-4
lines changed

src/Components/Posts/CreatePostModal/CreatePostModal.jsx

+4-1
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@ import useFetch from '../../../CustomHooks/useFetch';
99
import CreatePostModalFooter from './CreatePostModalFooter';
1010
import { useNavigate } from 'react-router-dom';
1111
import { postOptions as options } from '../../../options'
12+
import { useDispatch } from 'react-redux';
13+
import { addPost } from '../../../store/posts/postsSlice';
1214

1315

1416
function CreatePostModal({ closeModal, editPost, post }) {
1517
const [uploadedImage, setUploadedImage] = useState(post&&post['image_path'] ?`http://localhost:8000/${post['image_path']}`: null);
1618
const [contentValue, setContentValue] = useState(post ? post.content : '');
19+
const dispatch=useDispatch()
1720
const navigate = useNavigate();
1821
const { fetchApi:createPost, loading, error } = useFetch('http://localhost:8000/api/posts/create', options);
1922

@@ -32,10 +35,10 @@ function CreatePostModal({ closeModal, editPost, post }) {
3235
resData = await createPost(toFormData([{ name: 'content', value: contentValue }, uploadedImage ? { value: uploadedImage, name: 'file_path' } : null]));
3336
}
3437
if (resData.ok) {
38+
dispatch(addPost(resData.data.post))
3539
setUploadedImage(null);
3640
setContentValue('');
3741
closeModal();
38-
navigate(0);
3942
}
4043
};
4144

src/CustomHooks/useFetch.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ function useFetch(url, options) {
99
const fetchApi = async (data) => {
1010
setLoading(true);
1111
try {
12-
const response = await fetch(url, { ...options('37|7Q91EnaQrAhQQQ6YZKHQjsUJNdrMeYkiNXsRW2Jp'), body: data });
12+
const response = await fetch(url, { ...options('38|sum7kFXDl4oezUHHh6BgP6RhKAmdKPJ7YKGKK8bC'), body: data });
1313
const resData = await response.json();
1414
console.log(resData)
1515

src/Pages/Home/Home.jsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export const loader=async()=>{
3232
const response=await fetch('http://localhost:8000/api/posts',{
3333
method:'GET',
3434
headers:{
35-
"Authorization": "Bearer 35|vwM5NalCjgX4Gz0PHv6kTGvW63lV0SY0lzIfYvOF"
35+
"Authorization": "Bearer 38|sum7kFXDl4oezUHHh6BgP6RhKAmdKPJ7YKGKK8bC"
3636
}
3737
})
3838
if(!response.ok){

src/store/posts/postsSlice.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ const postsSlice=createSlice({
99
setPosts:(state,action)=>{
1010
state.posts=action.payload
1111
},
12+
addPost:(state,action)=>{
13+
const newPost=action.payload
14+
state.posts.unshift(newPost)
15+
},
1216
deletePost:(state,action)=>{
1317
const allPosts=state.posts
1418
state.posts=allPosts.filter(post=>post.id!=action.payload.id)
@@ -31,4 +35,4 @@ const postsSlice=createSlice({
3135
})
3236

3337
export const postsReducer=postsSlice.reducer
34-
export const {deletePost,setPosts,editPost}=postsSlice.actions
38+
export const {deletePost,setPosts,editPost,addPost}=postsSlice.actions

0 commit comments

Comments
 (0)