1- import axios from ' axios' ;
2- import { errorHandler } from ' ../utils/errorHandler' ;
3- import { setRequestStatus } from ' ../utils/setRequestStatus' ;
4- import { GET_ALL_POSTS , GET_ALL_PINNED_POSTS , GET_SINGLE_POST } from ' ./types' ;
5- import { BASE_URL } from ' ./baseApi'
1+ import axios from " axios" ;
2+ import { errorHandler } from " ../utils/errorHandler" ;
3+ import { setRequestStatus } from " ../utils/setRequestStatus" ;
4+ import { GET_ALL_POSTS , GET_ALL_PINNED_POSTS , GET_SINGLE_POST } from " ./types" ;
5+ import { BASE_URL } from " ./baseApi" ;
66
77// GET ALL POSTS
88export const getAllPosts = ( pagination = 10 , page = 1 ) => async ( dispatch ) => {
99 try {
10- const res = await axios . get ( `${ BASE_URL } /post/all_posts?pagination=${ pagination } &page=${ page } ` )
11- dispatch ( setRequestStatus ( false ) )
12- if ( res . status === 200 ) {
13- dispatch ( setRequestStatus ( true ) )
14- console . log ( 'all posts ' , res . data . posts )
10+ const res = await axios . get (
11+ `${ BASE_URL } /post/all_posts?pagination=${ pagination } &page=${ page } `
12+ ) ;
13+ dispatch ( setRequestStatus ( false ) ) ;
14+ if ( res . status === 200 ) {
15+ dispatch ( setRequestStatus ( true ) ) ;
16+ console . log ( "all posts " , res . data . posts ) ;
1517 dispatch ( {
1618 type : GET_ALL_POSTS ,
17- payload : res . data . posts
18- } )
19+ payload : res . data . posts ,
20+ } ) ;
1921 }
20- } catch ( error ) {
21- dispatch ( errorHandler ( error ) )
22+ } catch ( error ) {
23+ dispatch ( errorHandler ( error ) ) ;
2224 }
23- }
25+ } ;
2426
25- // GET ALL PINNED POSTS
26- export const getAllPinnedPosts = ( pagination = 10 , page = 1 ) => async ( dispatch ) => {
27+ // GET ALL PINNED POSTS
28+ export const getAllPinnedPosts = ( pagination = 10 , page = 1 ) => async (
29+ dispatch
30+ ) => {
2731 try {
28- const res = await axios . get ( `${ BASE_URL } /post/all/pinned?pagination=${ pagination } &page=${ page } ` )
29- dispatch ( setRequestStatus ( false ) )
30- if ( res . status === 200 ) {
31- dispatch ( setRequestStatus ( true ) )
32- console . log ( 'fetching all pinned posts ' , res . data . pinnedPost )
32+ const res = await axios . get (
33+ `${ BASE_URL } /post/all/pinned?pagination=${ pagination } &page=${ page } `
34+ ) ;
35+ dispatch ( setRequestStatus ( false ) ) ;
36+ if ( res . status === 200 ) {
37+ dispatch ( setRequestStatus ( true ) ) ;
38+ console . log ( "fetching all pinned posts " , res . data . pinnedPost ) ;
3339 dispatch ( {
3440 type : GET_ALL_PINNED_POSTS ,
35- payload : res . data . pinnedPost
36- } )
41+ payload : res . data . pinnedPost ,
42+ } ) ;
3743 }
38- } catch ( error ) {
39- dispatch ( errorHandler ( error ) )
44+ } catch ( error ) {
45+ dispatch ( errorHandler ( error ) ) ;
4046 }
41- }
47+ } ;
4248
4349// UPVOTE POST
44- export const upVotePost = ( postId ) => async ( dispatch ) => {
50+ export const upVotePost = ( postId , type ) => async ( dispatch ) => {
4551 try {
46- const res = await axios . patch ( `${ BASE_URL } /post/upvote/${ postId } ` )
47- if ( res . status === 200 ) {
48- console . log ( ' successfully upvoted post ' , res . data )
52+ const res = await axios . patch ( `${ BASE_URL } /post/upvote/${ postId } ` , type ) ;
53+ if ( res . status === 200 ) {
54+ console . log ( " successfully upvoted post " , res . data ) ;
4955 dispatch ( getAllPosts ( ) ) ;
5056 }
5157 } catch ( error ) {
52- dispatch ( errorHandler ( error ) )
58+ dispatch ( errorHandler ( error ) ) ;
5359 }
54- }
60+ } ;
5561
56- // GET POST BY ID
62+ // GET POST BY ID
5763export const getPostById = ( postId ) => async ( dispatch ) => {
5864 try {
59- console . log ( ' postId from action ' , postId )
65+ console . log ( " postId from action " , postId ) ;
6066 const res = await axios . get ( `${ BASE_URL } /post/${ postId } ` ) ;
6167 if ( res . status === 200 ) {
6268 dispatch ( {
6369 type : GET_SINGLE_POST ,
64- payload : res . data . post
65- } )
70+ payload : res . data . post ,
71+ } ) ;
6672 }
6773 } catch ( error ) {
68- dispatch ( errorHandler ( error ) )
74+ dispatch ( errorHandler ( error ) ) ;
6975 }
70- }
76+ } ;
7177
72- // UPDATE POST
78+ // UPDATE POST
7379export const updatePost = ( postId , updatedInfo ) => async ( dispatch ) => {
7480 try {
75- console . log ( ' updatedPostInfo ' , updatedInfo )
76- const res = await axios . patch ( `${ BASE_URL } /post/${ postId } ` , updatedInfo )
81+ console . log ( " updatedPostInfo " , updatedInfo ) ;
82+ const res = await axios . patch ( `${ BASE_URL } /post/${ postId } ` , updatedInfo ) ;
7783 if ( res . status === 200 ) {
78- dispatch ( getPostById ( postId ) )
84+ dispatch ( getPostById ( postId ) ) ;
7985 }
8086 } catch ( error ) {
81- dispatch ( errorHandler ( error ) )
87+ dispatch ( errorHandler ( error ) ) ;
8288 }
83- }
89+ } ;
8490
85- // DELETE POST
91+ // DELETE POST
8692export const deletePost = ( postId ) => async ( dispatch ) => {
8793 try {
88- const res = await axios . delete ( `${ BASE_URL } /post/${ postId } ` )
89- if ( res . status === 200 ) {
90- dispatch ( getAllPosts ( ) )
94+ const res = await axios . delete ( `${ BASE_URL } /post/${ postId } ` ) ;
95+ if ( res . status === 200 ) {
96+ dispatch ( getAllPosts ( ) ) ;
97+ }
98+ } catch ( error ) {
99+ dispatch ( errorHandler ( error ) ) ;
100+ }
101+ } ;
102+
103+ // REMOVE REACTION
104+ export const removeReaction = ( postId , type ) => async ( dispatch ) => {
105+ try {
106+ const res = await axios . patch ( `${ BASE_URL } /post/removereaction/${ postId } ` , type ) ;
107+ if ( res . status === 200 ) {
108+ dispatch ( getAllPosts ( ) ) ;
91109 }
92110 } catch ( error ) {
93- dispatch ( errorHandler ( error ) )
111+ dispatch ( errorHandler ( error ) ) ;
94112 }
95- }
113+ } ;
0 commit comments