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" ;
55
6- // CREATE COMMENT ON A PARTICULAR POST
6+ // CREATE COMMENT ON A PARTICULAR POST
77export const createComment = ( postId , comment ) => async ( dispatch ) => {
88 try {
9- const res = await axios . post ( `/comment/${ postId } ` , comment )
9+ const res = await axios . post ( `/comment/${ postId } ` , comment ) ;
1010 dispatch ( setRequestStatus ( false ) ) ;
11- if ( res . status === 201 ) {
12- dispatch ( setRequestStatus ( true ) )
13- console . log ( ' created comment ' , res . data . comment )
11+ if ( res . status === 201 ) {
12+ dispatch ( setRequestStatus ( true ) ) ;
13+ console . log ( " created comment " , res . data . comment ) ;
1414 dispatch ( getAllCommentsOfPost ( ) ) ;
1515 }
16- } catch ( error ) {
17- dispatch ( errorHandler ( error ) )
16+ } catch ( error ) {
17+ dispatch ( errorHandler ( error ) ) ;
1818 }
19- }
19+ } ;
2020
2121// GET ALL COMMENTS OF A POST
2222export const getAllCommentsOfPost = ( postId ) => async ( dispatch ) => {
2323 try {
24- const res = await axios . get ( `/comment/${ postId } ` )
25- dispatch ( setRequestStatus ( false ) )
26- if ( res . status === 200 ) {
24+ const res = await axios . get ( `/comment/${ postId } ` ) ;
25+ dispatch ( setRequestStatus ( false ) ) ;
26+ if ( res . status === 200 ) {
2727 dispatch ( setRequestStatus ( true ) ) ;
28- console . log ( ' fetching comments of ' , postId , res . data . comments ) ;
28+ console . log ( " fetching comments of " , postId , res . data . comments ) ;
2929 dispatch ( {
3030 type : GET_COMMENTS_OF_A_POST ,
31- payload : res . data . comments
32- } )
31+ payload : res . data . comments ,
32+ } ) ;
3333 }
34- } catch ( error ) {
35- dispatch ( errorHandler ( error ) )
34+ } catch ( error ) {
35+ dispatch ( errorHandler ( error ) ) ;
3636 }
37- }
37+ } ;
3838
3939// UPDATE COMMENT OF A POST
40- export const updateComment = ( commentId , updatedComment ) => async ( dispatch ) => {
40+ export const updateComment = ( commentId , updatedComment ) => async (
41+ dispatch
42+ ) => {
4143 try {
42- const res = await axios . patch ( `/comment/${ commentId } ` , updatedComment )
43- dispatch ( setRequestStatus ( false ) )
44- if ( res . status === 200 ) {
45- dispatch ( setRequestStatus ( true ) )
46- console . log ( ' comment updated ' , res . data . comment )
47- dispatch ( getAllCommentsOfPost ( ) )
44+ const res = await axios . patch ( `/comment/${ commentId } ` , updatedComment ) ;
45+ dispatch ( setRequestStatus ( false ) ) ;
46+ if ( res . status === 200 ) {
47+ dispatch ( setRequestStatus ( true ) ) ;
48+ console . log ( " comment updated " , res . data . comment ) ;
49+ dispatch ( getAllCommentsOfPost ( ) ) ;
4850 }
49- } catch ( error ) {
50- errorHandler ( error )
51+ } catch ( error ) {
52+ errorHandler ( error ) ;
5153 }
52- }
54+ } ;
5355
5456// DELETE COMMENT
5557export const deleteComment = ( commentId ) => async ( dispatch ) => {
5658 try {
57- const res = await axios . delete ( `/comment/${ commentId } ` )
58- dispatch ( setRequestStatus ( false ) )
59- if ( res . status === 200 ) {
59+ const res = await axios . delete ( `/comment/${ commentId } ` ) ;
60+ dispatch ( setRequestStatus ( false ) ) ;
61+ if ( res . status === 200 ) {
6062 dispatch ( setRequestStatus ( true ) ) ;
61- console . log ( ' comment deleted ' , res . data )
62- dispatch ( getAllCommentsOfPost ( ) )
63+ console . log ( " comment deleted " , res . data ) ;
64+ dispatch ( getAllCommentsOfPost ( ) ) ;
6365 }
64- } catch ( error ) {
65- dispatch ( errorHandler ( error ) )
66+ } catch ( error ) {
67+ dispatch ( errorHandler ( error ) ) ;
6668 }
67- }
69+ } ;
70+
71+ export const resetComments = ( ) => async ( dispatch ) => {
72+ dispatch ( { type : RESET_COMMENTS } ) ;
73+ } ;
0 commit comments