Skip to content

Commit 9a4231f

Browse files
committed
refactor: move the apis urls
1 parent 482ec93 commit 9a4231f

File tree

8 files changed

+128
-61
lines changed

8 files changed

+128
-61
lines changed

src/api/answersApi.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import axios from 'axios';
2+
3+
import {
4+
allAnswersData as _allAnswersData,
5+
createSingleAnswer as _createSingleAnswer,
6+
deleteSingleAnswer as _deleteSingleAnswer
7+
} from './urls';
8+
9+
export const allAnswersData = (id) => {
10+
return axios.get(_allAnswersData.replace('{id}', id));
11+
}
12+
13+
export const createSingleAnswer = (postId, formData) => {
14+
const config_headers = {
15+
headers: {
16+
"Content-Type": "application/json",
17+
},
18+
};
19+
20+
return axios.post(_createSingleAnswer.replace('{postId}', postId), formData, config_headers);
21+
}
22+
23+
export const deleteSingleAnswer = (AnswerId) => {
24+
return axios.delete(_deleteSingleAnswer.replace('{AnswerId}', AnswerId));
25+
}

src/api/commentsApi.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import axios from 'axios';
2+
3+
import {
4+
allCommentsData as _allCommentsData,
5+
createSingleComment as _createSingleComment,
6+
deleteSingleComment as _deleteSingleComment
7+
} from './urls';
8+
9+
export const allCommentsData = (id) => {
10+
return axios.get(_allCommentsData.replace('{id}', id));
11+
}
12+
13+
export const createSingleComment = (postId, formData) => {
14+
const config_headers = {
15+
headers: {
16+
"Content-Type": "application/json",
17+
},
18+
};
19+
20+
return axios.post(_createSingleComment.replace('{postId}', postId), formData, config_headers);
21+
}
22+
23+
export const deleteSingleComment = (CommentId) => {
24+
return axios.delete(_deleteSingleComment.replace('{CommentId}', CommentId));
25+
}

src/api/postsApis.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import axios from 'axios';
2+
3+
import {
4+
allPostsData as _allPostsData,
5+
singlePostData as _singlePostData,
6+
allTopPostsData as _allTopPostsData,
7+
allTagPostsData as _allTagPostsData,
8+
createSinglePost as _createSinglePost,
9+
deleteSinglePost as _deleteSinglePost
10+
} from './urls';
11+
12+
export const allPostsData = () => {
13+
return axios.get(_allPostsData);
14+
}
15+
16+
export const singlePostData = (id) => {
17+
return axios.get(_singlePostData.replace('{id}', id));
18+
}
19+
20+
export const allTopPostsData = () => {
21+
return axios.get(_allTopPostsData);
22+
}
23+
24+
export const allTagPostsData = (tagName) => {
25+
return axios.get(_allTagPostsData.replace('{tagName}', tagName));
26+
}
27+
28+
export const createSinglePost = (formData) => {
29+
const config_headers = {
30+
headers: {
31+
"Content-Type": "application/json",
32+
},
33+
};
34+
35+
return axios.post(_createSinglePost, formData, config_headers);
36+
}
37+
38+
export const deleteSinglePost = (id) => {
39+
return axios.delete(_deleteSinglePost.replace('{id}', id));
40+
}

src/api/tagsApi.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import axios from 'axios';
2+
3+
import { allTagsData as _allTagsData, singleTagData as _singleTagData } from './urls';
4+
5+
export const allTagsData = () => {
6+
return axios.get(_allTagsData);
7+
}
8+
9+
export const singleTagData = (tagName) => {
10+
return axios.get(_singleTagData.replace('{tagName}', tagName));
11+
}

src/redux/answers/answers.actions.js

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
1+
import { setAlert } from "../alert/alert.actions";
12
import {
23
GET_ANSWERS,
34
ANSWER_ERROR,
45
ADD_ANSWER,
56
DELETE_ANSWER,
67
} from "./answers.types";
7-
8-
import axios from "axios";
9-
import { setAlert } from "../alert/alert.actions";
10-
import config from "../../config";
8+
import { allAnswersData, createSingleAnswer, deleteSingleAnswer } from "../../api/answersApi";
119

1210
export const getAnswers = (id) => async (dispatch) => {
1311
try {
14-
const res = await axios.get(config.BASE_URL + `/api/posts/answers/${id}`);
12+
const res = await allAnswersData(id);
1513

1614
dispatch({
1715
type: GET_ANSWERS,
@@ -27,18 +25,8 @@ export const getAnswers = (id) => async (dispatch) => {
2725

2826
// Add Answer
2927
export const addAnswer = (postId, formData) => async (dispatch) => {
30-
const config_headers = {
31-
headers: {
32-
"Content-Type": "application/json",
33-
},
34-
};
35-
3628
try {
37-
const res = await axios.post(
38-
config.BASE_URL + `/api/posts/answers/${postId}`,
39-
formData,
40-
config_headers
41-
);
29+
const res = await createSingleAnswer(postId, formData);
4230

4331
dispatch({
4432
type: ADD_ANSWER,
@@ -61,9 +49,7 @@ export const addAnswer = (postId, formData) => async (dispatch) => {
6149
// Delete Answer
6250
export const deleteAnswer = (AnswerId) => async (dispatch) => {
6351
try {
64-
const res = await axios.delete(
65-
config.BASE_URL + `/api/posts/answers/${AnswerId}`
66-
);
52+
const res = await deleteSingleAnswer(AnswerId);
6753

6854
dispatch({
6955
type: DELETE_ANSWER,

src/redux/comments/comments.actions.js

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
1-
import axios from "axios";
2-
3-
import config from "../../config";
41
import { setAlert } from "../alert/alert.actions";
52
import {
63
GET_COMMENTS,
74
COMMENT_ERROR,
85
ADD_COMMENT,
96
DELETE_COMMENT,
107
} from "./comments.types";
8+
import { allCommentsData, createSingleComment, deleteSingleComment } from "../../api/commentsApi";
119

1210
export const getComments = (id) => async (dispatch) => {
1311
try {
14-
const res = await axios.get(config.BASE_URL + `/api/posts/comments/${id}`);
12+
const res = await allCommentsData(id);
1513

1614
dispatch({
1715
type: GET_COMMENTS,
@@ -27,18 +25,8 @@ export const getComments = (id) => async (dispatch) => {
2725

2826
// Add COMMENT
2927
export const addComment = (postId, formData) => async (dispatch) => {
30-
const config_headers = {
31-
headers: {
32-
"Content-Type": "application/json",
33-
},
34-
};
35-
3628
try {
37-
const res = await axios.post(
38-
config.BASE_URL + `/api/posts/comments/${postId}`,
39-
formData,
40-
config_headers
41-
);
29+
const res = await createSingleComment(postId, formData);
4230

4331
dispatch({
4432
type: ADD_COMMENT,
@@ -61,9 +49,7 @@ export const addComment = (postId, formData) => async (dispatch) => {
6149
// Delete Comment
6250
export const deleteComment = (CommentId) => async (dispatch) => {
6351
try {
64-
const res = await axios.delete(
65-
config.BASE_URL + `/api/posts/comments/${CommentId}`
66-
);
52+
const res = await deleteSingleComment(CommentId);
6753

6854
dispatch({
6955
type: DELETE_COMMENT,

src/redux/posts/posts.actions.js

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
import axios from "axios";
2-
3-
import config from "../../config";
41
import { setAlert } from "../alert/alert.actions";
52
import {
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
1621
export 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
3540
export 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
5459
export 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
7378
export 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
9297
export 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
125120
export 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,

src/redux/tags/tags.actions.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
import {GET_TAG, GET_TAGS, TAG_ERROR} from './tags.types';
2-
import axios from 'axios';
31
import {setAlert} from '../alert/alert.actions';
4-
import config from "../../config";
2+
import {GET_TAG, GET_TAGS, TAG_ERROR} from './tags.types';
3+
import { allTagsData, singleTagData } from '../../api/tagsApi';
54

65
export const getTag = (tagName) => async (dispatch) => {
76
try {
8-
const res = await axios.get(config.BASE_URL + `/api/tags/${tagName}`);
7+
const res = await singleTagData(tagName);
98

109
dispatch({
1110
type: GET_TAG,
@@ -24,7 +23,7 @@ export const getTag = (tagName) => async (dispatch) => {
2423

2524
export const getTags = () => async (dispatch) => {
2625
try {
27-
const res = await axios.get(config.BASE_URL + '/api/tags');
26+
const res = await allTagsData();
2827

2928
dispatch({
3029
type: GET_TAGS,

0 commit comments

Comments
 (0)