-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapiCalls.js
168 lines (147 loc) · 4.66 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
161
162
163
164
165
166
167
168
import "isomorphic-fetch";
export const getActivities = (lat, long) => {
const url = `https://globe-trotter-api.herokuapp.com/api/v1/yelp_activities/?lat=${lat}&long=${long}`;
return fetch(url)
.then(response => {
if (!response.ok) {
throw Error('error retrieving activities data')
}
return response.json()
})
};
export const getAllTrips = () => {
const query = {
"query": "{allTrips(userApiKey: \"b9aead4b955bccb5c57ef830580f3de5\") {id name origin originAbbrev originLat originLong tripdestinationSet {destination {location abbrev lat long} id startDate endDate activitySet {id name date address category rating image lat long tripDestination { trip { name } destination { location abbrev } } } } } }"
};
const options = {
method: 'POST',
body: JSON.stringify(query),
headers: {
'Content-Type': 'application/json',
}
};
return fetch('https://globe-trotter-api.herokuapp.com/graphql/', options)
.then(response => {
if (!response.ok) {
throw Error('error retrieving trips data')
}
return response.json()
})
};
export const createNewTrip = (name, origin) => {
const mutation = {
"query": `mutation {createTrip(userApiKey: \"b9aead4b955bccb5c57ef830580f3de5\", name: \"${name}\", origin: \"${origin}\") {trip {id name origin originAbbrev originLat originLong}}}`
};
const options = {
method: 'POST',
body: JSON.stringify(mutation),
headers: {
'Content-Type': 'application/json',
}
};
return fetch('https://globe-trotter-api.herokuapp.com/graphql/', options)
.then(response => {
if (!response.ok) {
throw Error('error posting new trip')
}
return response.json()
})
};
export const createNewDestination = (tripId, location, startDate, endDate) => {
const mutation = {
"query": `mutation {createDestination(userApiKey: \"b9aead4b955bccb5c57ef830580f3de5\", tripId: ${tripId}, location: \"${location}\", startDate: \"${startDate}\", endDate: \"${endDate}\") {destination {id location abbrev lat long tripdestinationSet {id startDate endDate trip {name origin}}}}}`
};
const options = {
method: 'POST',
body: JSON.stringify(mutation),
headers: {
'Content-Type': 'application/json',
}
}
return fetch('https://globe-trotter-api.herokuapp.com/graphql/', options)
.then(response => {
if (!response.ok) {
throw Error('error posting new destination data')
}
return response.json()
})
};
export const deleteTrip = (tripId) => {
const mutation = {
"query": `mutation {deleteTrip(userApiKey: \"b9aead4b955bccb5c57ef830580f3de5\", tripId: ${tripId}) {id name origin}}`
};
const options = {
method: 'POST',
body: JSON.stringify(mutation),
headers: {
'Content-Type': 'application/json',
}
}
return fetch('https://globe-trotter-api.herokuapp.com/graphql/', options)
.then(response => {
if (!response.ok) {
throw Error('error deleting trip')
}
return response.json()
})
};
export const addActivity = (id, name, date, address, category, rating, image, lat, long) => {
const mutation = {
"query": `mutation {
createActivity(userApiKey: \"b9aead4b955bccb5c57ef830580f3de5\", tripDestinationId: ${id}, name: \"${name}\", date: \"${date}\", address: \"${address}\", category: \"${category}\", rating: ${rating}, image: \"${image}\", lat: ${lat}, long: ${long}) {
activity {
name
date
address
category
rating
image
lat
long
tripDestination {
trip {
name
}
destination {
location
abbrev
}
}
}
}
}`
}
const options = {
method: 'POST',
body: JSON.stringify(mutation),
headers: {
'Content-Type': 'application/json',
}
}
return fetch('https://globe-trotter-api.herokuapp.com/graphql/', options)
.then(response => {
if (!response.ok) {
throw Error('error saving new activity')
}
return response.json()
})
}
export const deleteActivity = (activityId) => {
const mutation = {
"query": `mutation {deleteActivity(userApiKey: \"b9aead4b955bccb5c57ef830580f3de5\", activityId: ${activityId}) {id}}`
};
const options = {
method: 'POST',
body: JSON.stringify(mutation),
headers: {
'Content-Type': 'application/json',
}
}
return fetch('https://globe-trotter-api.herokuapp.com/graphql/', options)
.then(response => {
if (!response.ok) {
throw Error('error deleting activity')
}
return response.json()
})
};