-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.js
74 lines (64 loc) · 1.58 KB
/
api.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import axios from 'axios'
const myApi = axios.create({ baseURL: "https://cb-news.onrender.com/api" });
// const myApi = axios.create({ baseURL: 'http://localhost:9090/api' })
export const getTopics = () => {
return myApi.get('/topics').then(({ data }) => {
return data
})
}
export const getArticles = (topic, searchParams) => {
if (!topic) topic = 'all'
let topicQuery = ''
if (topic !== 'all') {
topicQuery = `topic=${topic}&`
}
return myApi
.get(`/articles?${topicQuery}${searchParams}`)
.then(({ data }) => {
return data
})
}
export const getArticle = (article_id) => {
return myApi.get(`/articles/${article_id}`).then(({ data }) => {
return data
})
}
export const getComments = (article_id) => {
return myApi.get(`/articles/${article_id}/comments`).then(({ data }) => {
return data
})
}
export const patchArticleVotes = (article_id, vote) => {
return myApi
.patch(`/articles/${article_id}`, { inc_votes: vote })
.then(({ data }) => {
return data
})
}
export const patchCommentVotes = (comment_id, vote) => {
return myApi
.patch(`/comments/${comment_id}`, { inc_votes: vote })
.then(({ data }) => {
return data
})
}
export const deleteComment = (comment_id) => {
return myApi.delete(`/comments/${comment_id}`).then(({ data }) => {
return data
})
}
export const postComment = (article_id, user, body) => {
return myApi
.post(`/articles/${article_id}/comments`, {
username: user,
body: body,
})
.then(({ data }) => {
return data
})
}
export const getUsers = () => {
return myApi.get('/users').then(({ data }) => {
return data
})
}