setting up routes order mern
//MERN STACK - UPDATE
//Add your route
router.patch(`/:id`, updatePost)
//Set up controller
export const updatePost = async (req, res) => {
const { id: _id } = req.params;
const post = req.body;
if(!mongoose.Types.ObjectId.isValid(_id)) return res.status(404).send('No post with that id');
const updatedPost = await PostMessage.findByIdAndUpdate(_id, post, { new: true })
res.json(updatedPost)
}
//Make sure your prop drilling/redux data is being passed along properly
//Update handlesubmit needbe, and add to imports
const handleSubmit = (e) => {
e.preventDefault();
if(currentId) {
dispatch(updatePost(currentId, postData))
} else {
dispatch(createPost(postData))
}
// console.log("dispatched")
};
//Add API route
export const updatePost = (id, updatedPost) => axios.patch(`${url}/${id}`, updatedPost)
//Add action
export const updatePost = (id, post) => async (dispatch) => {
try {
const { data } = await api.updatePost(id, post);
dispatch({ type: `UPDATE`, payload: data });
} catch (error) {}
};
//Go to reducers
case "UPDATE":
return posts.map((post) => post._id === action.payload._id ? action.payload : post)
//Set state