Skip to content

Commit

Permalink
post delete option and logic done
Browse files Browse the repository at this point in the history
  • Loading branch information
aliarslanansari committed Jul 5, 2021
1 parent 61f853b commit 5c2af9c
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 5 deletions.
24 changes: 23 additions & 1 deletion client/src/actions/post.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import axios from 'axios'
import { GET_POSTS_FAILURE, GET_POSTS_SUCCESS, UPDATE_LIKES } from './types'
import {
GET_POSTS_FAILURE,
GET_POSTS_SUCCESS,
POST_DELETED,
UPDATE_LIKES
} from './types'
import { setAlert } from './alert'

export const getPosts = () => async (dispatch) => {
try {
Expand Down Expand Up @@ -51,3 +57,19 @@ export const removeLike = (postId) => async (dispatch) => {
})
}
}
export const deletePost = (postId) => async (dispatch) => {
try {
await axios.delete(`http://localhost:5000/api/posts/${postId}`)

dispatch({ type: POST_DELETED, payload: postId })
dispatch(setAlert('Post Deleted', 'success'))
} catch (error) {
dispatch({
type: GET_POSTS_FAILURE,
payload: {
msg: error.response?.statusText,
status: error.response?.status
}
})
}
}
1 change: 1 addition & 0 deletions client/src/actions/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@ export const GET_PROFILES_SUCCESS = 'GET_PROFILES_SUCCESS'

export const GET_POSTS_SUCCESS = 'GET_POSTS_SUCCESS'
export const GET_POSTS_FAILURE = 'GET_POSTS_FAILURE'
export const POST_DELETED = 'POST_DELETED'

export const UPDATE_LIKES = 'UPDATE_LIKES'
15 changes: 11 additions & 4 deletions client/src/components/posts/PostItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ import PropTypes from 'prop-types'
import Moment from 'react-moment'
import { Link } from 'react-router-dom'
import { connect } from 'react-redux'
import { removeLike, addLike } from './../../actions/post'
import { removeLike, addLike, deletePost } from './../../actions/post'

const PostItem = ({
post: { _id, date, avatar, comments, name, likes, user, text },
auth,
addLike,
deletePost,
removeLike
}) => {
return (
Expand Down Expand Up @@ -46,7 +47,12 @@ const PostItem = ({
)}
</Link>
{!auth.loading && user === auth.user?._id && (
<button type="button" data-id={user} className="btn btn-danger">
<button
onClick={() => deletePost(_id)}
type="button"
data-id={user}
className="btn btn-danger"
>
<i className="fas fa-times"></i>
</button>
)}
Expand All @@ -59,7 +65,8 @@ PostItem.propTypes = {
post: PropTypes.object.isRequired,
auth: PropTypes.object.isRequired,
addLike: PropTypes.func.isRequired,
removeLike: PropTypes.func.isRequired
removeLike: PropTypes.func.isRequired,
deletePost: PropTypes.func.isRequired
}

export default connect(null, { addLike, removeLike })(PostItem)
export default connect(null, { addLike, removeLike, deletePost })(PostItem)
7 changes: 7 additions & 0 deletions client/src/reducers/post.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
GET_POSTS_FAILURE,
GET_POSTS_SUCCESS,
POST_DELETED,
UPDATE_LIKES
} from './../actions/types'

Expand All @@ -18,6 +19,12 @@ export default (state = initialState, { type, payload }) => {
),
loading: false
}
case POST_DELETED:
return {
...state,
posts: state.posts.filter((post) => post._id !== payload),
loading: false
}
case GET_POSTS_FAILURE:
return { ...state, error: payload, loading: false }
default:
Expand Down

0 comments on commit 5c2af9c

Please sign in to comment.