1- import axios from "axios" ;
2-
3- import config from "../../config" ;
41import { setAlert } from "../alert/alert.actions" ;
52import {
63 GET_POSTS ,
@@ -11,11 +8,19 @@ import {
118 DELETE_POST ,
129 ADD_POST ,
1310} from "./posts.types" ;
11+ import {
12+ allPostsData ,
13+ singlePostData ,
14+ allTopPostsData ,
15+ allTagPostsData ,
16+ createSinglePost ,
17+ deleteSinglePost
18+ } from "../../api/postsApis" ;
1419
1520// Get posts
1621export const getPosts = ( ) => async ( dispatch ) => {
1722 try {
18- const res = await axios . get ( config . BASE_URL + "/api/posts" ) ;
23+ const res = await allPostsData ( ) ;
1924
2025 dispatch ( {
2126 type : GET_POSTS ,
@@ -34,7 +39,7 @@ export const getPosts = () => async (dispatch) => {
3439// Get post
3540export const getPost = ( id ) => async ( dispatch ) => {
3641 try {
37- const res = await axios . get ( config . BASE_URL + `/api/posts/ ${ id } ` ) ;
42+ const res = await singlePostData ( id ) ;
3843
3944 dispatch ( {
4045 type : GET_POST ,
@@ -53,7 +58,7 @@ export const getPost = (id) => async (dispatch) => {
5358//GET TOP POSTS
5459export const getTopPosts = ( ) => async ( dispatch ) => {
5560 try {
56- const res = await axios . get ( config . BASE_URL + "/api/posts/top" ) ;
61+ const res = await allTopPostsData ( ) ;
5762
5863 dispatch ( {
5964 type : GET_TOP_POSTS ,
@@ -72,7 +77,7 @@ export const getTopPosts = () => async (dispatch) => {
7277//GET TAG POSTS
7378export const getTagPosts = ( tagName ) => async ( dispatch ) => {
7479 try {
75- const res = await axios . get ( config . BASE_URL + `/api/posts/tag/ ${ tagName } ` ) ;
80+ const res = await allTagPostsData ( tagName ) ;
7681
7782 dispatch ( {
7883 type : GET_TAG_POSTS ,
@@ -90,18 +95,8 @@ export const getTagPosts = (tagName) => async (dispatch) => {
9095
9196// Add post
9297export const addPost = ( formData ) => async ( dispatch ) => {
93- const config_headers = {
94- headers : {
95- "Content-Type" : "application/json" ,
96- } ,
97- } ;
98-
9998 try {
100- const res = await axios . post (
101- config . BASE_URL + "/api/posts" ,
102- formData ,
103- config_headers
104- ) ;
99+ const res = await createSinglePost ( formData ) ;
105100
106101 dispatch ( {
107102 type : ADD_POST ,
@@ -124,7 +119,7 @@ export const addPost = (formData) => async (dispatch) => {
124119// Delete post
125120export const deletePost = ( id ) => async ( dispatch ) => {
126121 try {
127- const res = await axios . delete ( config . BASE_URL + `/api/posts/ ${ id } ` ) ;
122+ const res = await deleteSinglePost ( id ) ;
128123
129124 dispatch ( {
130125 type : DELETE_POST ,
0 commit comments