1- import { GET_COMMENTS_OF_A_POST } from ' ./types'
2- import { errorHandler } from ' ../utils/errorHandler'
3- import axios from ' axios'
4- import { setRequestStatus } from ' ../utils/setRequestStatus'
1+ import { GET_COMMENTS_OF_A_POST , RESET_COMMENTS } from " ./types" ;
2+ import { errorHandler } from " ../utils/errorHandler" ;
3+ import axios from " axios" ;
4+ import { setRequestStatus } from " ../utils/setRequestStatus" ;
55import { BASE_URL } from './baseApi'
66
7- // CREATE COMMENT ON A PARTICULAR POST
7+ // CREATE COMMENT ON A PARTICULAR POST
88export const createComment = ( postId , comment ) => async ( dispatch ) => {
99 try {
1010 const res = await axios . post ( `${ BASE_URL } /comment/${ postId } ` , comment )
@@ -14,10 +14,10 @@ export const createComment = (postId, comment) => async (dispatch) => {
1414 console . log ( 'created comment ' , res . data . comment )
1515 dispatch ( getAllCommentsOfPost ( postId ) ) ;
1616 }
17- } catch ( error ) {
18- dispatch ( errorHandler ( error ) )
17+ } catch ( error ) {
18+ dispatch ( errorHandler ( error ) ) ;
1919 }
20- }
20+ } ;
2121
2222// GET ALL COMMENTS OF A POST
2323export const getAllCommentsOfPost = ( postId ) => async ( dispatch ) => {
@@ -26,19 +26,21 @@ export const getAllCommentsOfPost = (postId) => async (dispatch) => {
2626 dispatch ( setRequestStatus ( false ) )
2727 if ( res . status === 200 ) {
2828 dispatch ( setRequestStatus ( true ) ) ;
29- console . log ( ' fetching comments of ' , postId , res . data . comments ) ;
29+ console . log ( " fetching comments of " , postId , res . data . comments ) ;
3030 dispatch ( {
3131 type : GET_COMMENTS_OF_A_POST ,
32- payload : res . data . comments
33- } )
32+ payload : res . data . comments ,
33+ } ) ;
3434 }
35- } catch ( error ) {
36- dispatch ( errorHandler ( error ) )
35+ } catch ( error ) {
36+ dispatch ( errorHandler ( error ) ) ;
3737 }
38- }
38+ } ;
3939
4040// UPDATE COMMENT OF A POST
41- export const updateComment = ( commentId , updatedComment ) => async ( dispatch ) => {
41+ export const updateComment = ( commentId , updatedComment ) => async (
42+ dispatch
43+ ) => {
4244 try {
4345 const res = await axios . patch ( `${ BASE_URL } /comment/${ commentId } ` , updatedComment )
4446 dispatch ( setRequestStatus ( false ) )
@@ -47,10 +49,10 @@ export const updateComment = (commentId, updatedComment) => async (dispatch) =>
4749 console . log ( 'comment updated ' , res . data . comment )
4850 dispatch ( getAllCommentsOfPost ( ) )
4951 }
50- } catch ( error ) {
51- errorHandler ( error )
52+ } catch ( error ) {
53+ errorHandler ( error ) ;
5254 }
53- }
55+ } ;
5456
5557// DELETE COMMENT
5658export const deleteComment = ( commentId ) => async ( dispatch ) => {
@@ -59,10 +61,14 @@ export const deleteComment = (commentId) => async (dispatch) => {
5961 dispatch ( setRequestStatus ( false ) )
6062 if ( res . status === 200 ) {
6163 dispatch ( setRequestStatus ( true ) ) ;
62- console . log ( ' comment deleted ' , res . data )
63- dispatch ( getAllCommentsOfPost ( ) )
64+ console . log ( " comment deleted " , res . data ) ;
65+ dispatch ( getAllCommentsOfPost ( ) ) ;
6466 }
65- } catch ( error ) {
66- dispatch ( errorHandler ( error ) )
67+ } catch ( error ) {
68+ dispatch ( errorHandler ( error ) ) ;
6769 }
68- }
70+ } ;
71+
72+ export const resetComments = ( ) => async ( dispatch ) => {
73+ dispatch ( { type : RESET_COMMENTS } ) ;
74+ } ;
0 commit comments