-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfavorites.js
98 lines (89 loc) · 2.16 KB
/
favorites.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
export const setFavorites = favorites => {
return {
type: 'SET_FAVORITES',
favorites
}
}
export const clearFavorites = () => {
return {
type: 'CLEAR_FAVORITES'
}
}
export const addFavorite = favorite => {
return {
type: 'ADD_FAVORITE',
favorite
}
}
export const deleteFavoriteSuccess = favoriteId => {
return {
type: 'DELETE_FAVORITE',
favoriteId
}
}
export const fetchFavorites = () => {
return dispatch => {
return fetch(`http://localhost:3001/api/v1/favorites`, {
credentials: 'include',
method: 'GET',
headers: {
'Content-Type': 'application/json'
},
})
.then(r => r.json())
.then(response => {
if (response.error) {
alert(response.error)
} else {
dispatch(setFavorites(response.data))
}
})
.catch(console.log)
}
}
export const createFavorite = (team_id, user_id) => {
return dispatch => {
return fetch('http://localhost:3001/api/v1/favorites', {
credentials: 'include',
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
user_id: parseInt(user_id),
team_id: parseInt(team_id)
})
})
.then(r => r.json())
.then(response => {
if (response.error) {
alert(response.error)
} else {
dispatch(addFavorite(response.data))
dispatch(fetchFavorites())
}
})
.catch(console.log)
}
}
export const deleteFavorite = (favoriteId) => {
return dispatch => {
return fetch(`http://localhost:3001/api/v1/favorites/${favoriteId}`, {
credentials: 'include',
method: 'DELETE',
headers: {
'Content-Type': 'application/json'
}
})
.then(r => r.json())
.then(resp => {
if (resp.error) {
alert(resp.error)
} else {
dispatch(deleteFavoriteSuccess(favoriteId))
}
})
.catch(console.log)
}
}
export default fetchFavorites