1
+ import axios from "axios" ;
2
+ import { API } from ".." ;
1
3
import {
2
- GET_POST ,
3
- SET_CURRENT_POST ,
4
+ CLEAN_POST_ACTION ,
4
5
CLEAR_POST ,
5
- UPLOAD_POST ,
6
- GET_USER_POSTS ,
6
+ CLEAR_POST_ID ,
7
7
DELETE_POST ,
8
- LIKE_POST ,
9
- TOGGLE_SAVE_POST ,
10
- GET_SAVED_POST ,
11
8
GET_LIKED_POST ,
9
+ GET_POST ,
12
10
GET_POST_ID ,
13
- CLEAR_POST_ID ,
14
- SET_LOADING_POST_TRUE ,
15
- RESET_IMAGE_UPLOAD ,
16
- CLEAN_POST_ACTION ,
11
+ GET_SAVED_POST ,
12
+ GET_USER_POSTS ,
13
+ LIKE_POST ,
17
14
REALLY_GET_ALL_POSTS ,
15
+ RESET_IMAGE_UPLOAD ,
18
16
SAVE_POST_TOAST ,
17
+ SET_CURRENT_POST ,
18
+ SET_LOADING_POST_TRUE ,
19
+ TOGGLE_SAVE_POST ,
20
+ UPLOAD_POST ,
19
21
} from "../actions/types" ;
20
- import axios from "axios" ;
21
22
import { loadUser } from "./authAction" ;
22
23
23
24
export const getPost = ( post_id ) => async ( dispatch ) => {
24
25
try {
25
26
if ( post_id !== undefined ) {
26
- const url = `/api/post/${ post_id } ` ;
27
+ const url = `${ API } /api/post/${ post_id } ` ;
27
28
dispatch ( { type : SET_LOADING_POST_TRUE } ) ;
28
29
const res = await axios . get ( url ) ;
29
30
dispatch ( { type : GET_POST , payload : res . data } ) ; // * this will fetch a single post given the post id
30
31
}
31
- } catch ( err ) {
32
- }
32
+ } catch ( err ) { }
33
33
} ;
34
34
35
35
export const setCurrentPost = ( post_id ) => ( dispatch ) => {
@@ -49,95 +49,86 @@ export const uploadPost = (post, post_id) => async (dispatch) => {
49
49
try {
50
50
if ( post_id !== undefined ) {
51
51
const res = await axios . post (
52
- `/api/post/content/${ post_id } ` ,
52
+ `${ API } /api/post/content/${ post_id } ` ,
53
53
post ,
54
54
config
55
55
) ;
56
56
dispatch ( { type : UPLOAD_POST , payload : res . data } ) ;
57
57
dispatch ( loadUser ( ) ) ;
58
58
}
59
- } catch ( err ) {
60
- }
59
+ } catch ( err ) { }
61
60
} ;
62
61
63
62
// ? Extension of above
64
63
export const uploadImage = ( fData ) => async ( dispatch ) => {
65
64
try {
66
65
dispatch ( { type : RESET_IMAGE_UPLOAD } ) ;
67
- const res = await axios . post ( " /api/upload" , fData ) ;
66
+ const res = await axios . post ( ` ${ API } /api/upload` , fData ) ;
68
67
dispatch ( { type : GET_POST_ID , payload : res . data . _id } ) ;
69
- } catch ( err ) {
70
- }
68
+ } catch ( err ) { }
71
69
} ;
72
70
73
71
export const getUserPosts = ( user_id ) => async ( dispatch ) => {
74
72
try {
75
73
if ( user_id !== undefined ) {
76
- const res = await axios . get ( `/api/post/all/${ user_id } ` ) ;
74
+ const res = await axios . get ( `${ API } /api/post/all/${ user_id } ` ) ;
77
75
dispatch ( { type : GET_USER_POSTS , payload : res . data } ) ;
78
76
}
79
- } catch ( err ) {
80
- }
77
+ } catch ( err ) { }
81
78
} ;
82
79
83
80
export const deletePost = ( post_id ) => async ( dispatch ) => {
84
81
try {
85
82
if ( post_id !== undefined ) {
86
- const res = await axios . delete ( `/api/post/${ post_id } ` ) ;
83
+ const res = await axios . delete ( `${ API } /api/post/${ post_id } ` ) ;
87
84
dispatch ( { type : DELETE_POST , payload : res . data } ) ;
88
85
}
89
- } catch ( err ) {
90
- }
86
+ } catch ( err ) { }
91
87
} ;
92
88
93
89
export const savePost = ( post_id ) => async ( dispatch ) => {
94
90
try {
95
91
if ( post_id !== undefined ) {
96
- const res = await axios . post ( `/api/save/${ post_id } ` ) ;
92
+ const res = await axios . post ( `${ API } /api/save/${ post_id } ` ) ;
97
93
dispatch ( { type : TOGGLE_SAVE_POST , payload : res . data } ) ;
98
94
dispatch ( { type : SAVE_POST_TOAST , payload : true } ) ;
99
95
setTimeout ( ( ) => {
100
96
dispatch ( { type : SAVE_POST_TOAST , payload : false } ) ;
101
97
} , 5000 ) ;
102
98
}
103
- } catch ( err ) {
104
- }
99
+ } catch ( err ) { }
105
100
} ;
106
101
107
102
export const getSavedPosts = ( ) => async ( dispatch ) => {
108
103
try {
109
- const res = await axios . get ( " /api/save" ) ;
104
+ const res = await axios . get ( ` ${ API } /api/save` ) ;
110
105
dispatch ( { type : GET_SAVED_POST , payload : res . data } ) ;
111
- } catch ( err ) {
112
- }
106
+ } catch ( err ) { }
113
107
} ;
114
108
115
109
export const likePost = ( post_id ) => async ( dispatch ) => {
116
110
try {
117
111
if ( post_id !== undefined ) {
118
- const res = await axios . put ( `/api/post/like/${ post_id } ` ) ;
112
+ const res = await axios . put ( `${ API } /api/post/like/${ post_id } ` ) ;
119
113
dispatch ( { type : LIKE_POST , payload : res . data } ) ;
120
114
}
121
- } catch ( err ) {
122
- }
115
+ } catch ( err ) { }
123
116
} ;
124
117
125
118
export const getLikedPosts = ( user , post_id ) => async ( dispatch ) => {
126
119
try {
127
120
if ( user !== undefined && post_id !== undefined ) {
128
- const res = await axios . get ( `/api/post/${ user } /${ post_id } ` ) ;
121
+ const res = await axios . get ( `${ API } /api/post/${ user } /${ post_id } ` ) ;
129
122
dispatch ( { type : GET_LIKED_POST , payload : res . data } ) ;
130
123
}
131
- } catch ( err ) {
132
- }
124
+ } catch ( err ) { }
133
125
} ;
134
126
135
127
export const reallyGetAllPosts = ( userId ) => async ( dispatch ) => {
136
128
try {
137
- const res = await axios . get ( `/api/post/real-all/${ userId } ` ) ;
129
+ const res = await axios . get ( `${ API } /api/post/real-all/${ userId } ` ) ;
138
130
dispatch ( { type : REALLY_GET_ALL_POSTS , payload : res . data } ) ;
139
- } catch ( error ) {
140
- }
131
+ } catch ( error ) { }
141
132
} ;
142
133
143
134
export const clearPostID = ( ) => ( dispatch ) => {
@@ -150,8 +141,7 @@ export const cleanGetPostAction = () => (dispatch) => {
150
141
151
142
export const fetchPopularPosts = ( ) => async ( dispatch ) => {
152
143
try {
153
- const res = await axios . get ( " /api/post/5fc4c6a6cf07d202383aadcf" ) ;
144
+ await axios . get ( ` ${ API } /api/post/5fc4c6a6cf07d202383aadcf` ) ;
154
145
// dispatch({ type: , payload: res.data });
155
- } catch ( err ) {
156
- }
146
+ } catch ( err ) { }
157
147
} ;
0 commit comments