-
Notifications
You must be signed in to change notification settings - Fork 0
/
ApiCalls.js
160 lines (151 loc) · 3.81 KB
/
ApiCalls.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Network Request Calls for Flies
export const fetchFlies = async () => {
try {
const response = await fetch(
"https://my-fly-box-api.herokuapp.com/api/v1/flies"
);
const flyData = await response.json();
return flyData;
} catch (error) {
console.error(error);
}
};
export const addFlyToAPI = async (flyData) => {
try {
const response = await fetch(
"https://my-fly-box-api.herokuapp.com/api/v1/flies",
{
method: "POST",
headers: {
"Content-Type": "application/json;charset=utf-8",
},
body: JSON.stringify(flyData),
}
);
const flyDataResponse = await response.json();
return flyDataResponse;
} catch (error) {
console.error(error);
}
};
export const addUpdatedFly = async (flyData) => {
try {
const response = await fetch(
`https://my-fly-box-api.herokuapp.com/api/v1/flies/${flyData.id}`,
{
method: "PATCH",
headers: {
"Content-Type": "application/json;charset=utf-8",
},
body: JSON.stringify(flyData.attributes),
}
);
const flyDataResponse = await response.json();
return flyDataResponse;
} catch (error) {
console.error(error);
}
};
export const deleteFly = async (flyId) => {
try {
const response = await fetch(
`https://my-fly-box-api.herokuapp.com/api/v1/flies/${flyId}`,
{
method: "DELETE",
headers: {
"Content-Type": "application/json;charset=utf-8",
},
}
);
const deletionResponse = await response.json();
console.log(deletionResponse);
} catch (error) {
console.error(error);
}
};
// Network Request Calls for Fish
export const fetchFish = async () => {
try {
const response = await fetch(
"https://my-fly-box-api.herokuapp.com/api/v1/catches"
);
const fishData = await response.json();
return fishData;
} catch (error) {
console.error(error);
}
};
export const addFishToAPI = async (fishData) => {
try {
const response = await fetch(
"https://my-fly-box-api.herokuapp.com/api/v1/catches",
{
method: "POST",
headers: {
"Content-Type": "application/json;charset=utf-8",
},
body: JSON.stringify(fishData),
}
);
const fishDataResponse = await response.json();
return fishDataResponse;
} catch (error) {
console.error(error);
}
};
export const addUpdatedFish = async (fishData) => {
try {
const response = await fetch(
`https://my-fly-box-api.herokuapp.com/api/v1/catches/${fishData.id}`,
{
method: "PATCH",
headers: {
"Content-Type": "application/json;charset=utf-8",
},
body: JSON.stringify(fishData.attributes),
}
);
const fishDataResponse = await response.json();
return fishDataResponse;
} catch (error) {
console.error(error);
}
};
export const deleteFish = async (fishId) => {
try {
const response = await fetch(
`https://my-fly-box-api.herokuapp.com/api/v1/catches/${fishId}`,
{
method: "DELETE",
headers: {
"Content-Type": "application/json;charset=utf-8",
},
}
);
const deletionResponse = await response.json();
console.log(deletionResponse);
} catch (error) {
console.error(error);
}
};
// Image upload for species identification:
export const uploadPhoto = async (photo) => {
try {
const response = await fetch(
"https://my-fly-box-api.herokuapp.com/api/v1/images",
{
method: "POST",
headers: {
"Content-Type": "application/json;charset=utf-8",
},
body: JSON.stringify({
base_64: photo,
}),
}
);
const imageResponse = await response.json();
return imageResponse;
} catch (error) {
console.error(error);
}
};