Newer
Older

Lucendio
committed
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import axios from 'axios'
const axiosInstance = axios.create({
baseURL: process.env.REACT_APP_baseAPIURL,
withCredentials: true,
});
axiosInstance.interceptors.response.use(
res => {
return res;
},
error => {
return Promise.reject(error.response)
}
);
export const signout = () => {
return axiosInstance.post(process.env.REACT_APP_baseAPIURL + '/logout').then(user => {
// delete axiosInstance.defaults.headers.common["Authorization"];
return user.data
})
}
export const init = () => {
return axiosInstance.post('/init').then(user => {
return user.data
})
}
export const login = (email, password) => {
return axiosInstance.post('/login', {
email,
password
}).then(user => {
// axiosInstance.defaults.headers.common["Authorization"] = `Bearer ${user.data.token}`;
return user.data
})
}
export const signup = (name, email, password) => {
return axiosInstance.post('/signup', {
name,
email,
password
}).then(user => {
return user.data
})
}
export const addTodo = (title, description) => {
return axiosInstance.post('/todo/add', {
title,
description
}).then(todo => {
return todo.data
})
}
export const listTodo = () => {
//I have used post instead of get, read https://blog.teamtreehouse.com/the-definitive-guide-to-get-vs-post
return axiosInstance.post('/todo/list').then(todos => {
return todos.data
})
}
export const updateTodo = (_id, important = null, done = null) => {
//I have used post instead of get, read https://blog.teamtreehouse.com/the-definitive-guide-to-get-vs-post
const fieldsToUpdate = {
_id
}
if (important !== null) {
fieldsToUpdate.important = important
}
if (done !== null) {
fieldsToUpdate.done = done
}
return axiosInstance.patch('/todo/update', fieldsToUpdate).then(todo => {
return todo.data
})
}
export const deleteTodo = (_id) => {
//I have used post instead of get, read https://blog.teamtreehouse.com/the-definitive-guide-to-get-vs-post
return axiosInstance.delete('/todo/delete', {
data: {
_id,
}
}).then(todo => {
return todo
})