2018-06-25 18:29:57 +02:00
|
|
|
// With trailing slash
|
2018-06-26 15:34:38 +02:00
|
|
|
export const BASE_URL = process.env.API_BASE_URL || '/';
|
2018-07-18 15:30:30 +02:00
|
|
|
const AUTHORIZATION_HEADERS = new Headers({});
|
|
|
|
if (process.env.API_TOKEN) {
|
|
|
|
AUTHORIZATION_HEADERS.set('Authorization', `Bearer ${process.env.API_TOKEN}`);
|
|
|
|
}
|
2018-06-25 18:29:57 +02:00
|
|
|
|
|
|
|
export function saveReport(type, lat, lng) {
|
|
|
|
return fetch(`${BASE_URL}api/v1/reports`, {
|
|
|
|
method: 'POST',
|
|
|
|
body: JSON.stringify({
|
|
|
|
type,
|
|
|
|
lat,
|
|
|
|
lng,
|
2018-11-01 15:43:34 +01:00
|
|
|
source: 'survey',
|
2018-06-25 18:29:57 +02:00
|
|
|
}),
|
2018-07-18 15:30:30 +02:00
|
|
|
headers: AUTHORIZATION_HEADERS,
|
2018-06-26 11:04:23 +02:00
|
|
|
})
|
2018-06-26 11:39:43 +02:00
|
|
|
.then(response => response.json())
|
|
|
|
.then(response => response.data)
|
2018-07-04 18:20:28 +02:00
|
|
|
.catch((exc) => {
|
|
|
|
console.error(`Unable to post report: ${exc}.`);
|
|
|
|
throw exc;
|
|
|
|
});
|
2018-06-25 18:29:57 +02:00
|
|
|
}
|
|
|
|
|
2018-07-10 15:55:45 +02:00
|
|
|
export function getActiveReports() {
|
2018-10-18 09:23:52 +02:00
|
|
|
return fetch(`${BASE_URL}api/v1/reports?filter[expiration_datetime][gt?]=${(new Date()).toISOString()}`)
|
2018-06-26 11:04:23 +02:00
|
|
|
.then(response => response.json())
|
|
|
|
.then(response => response.data)
|
2018-07-04 18:20:28 +02:00
|
|
|
.catch((exc) => {
|
|
|
|
console.error(`Unable to fetch reports: ${exc}.`);
|
|
|
|
throw exc;
|
|
|
|
});
|
2018-06-25 18:29:57 +02:00
|
|
|
}
|
2018-07-05 22:40:24 +02:00
|
|
|
|
2018-07-31 17:04:06 +02:00
|
|
|
export function getStats() {
|
|
|
|
return fetch(`${BASE_URL}api/v1/stats`)
|
|
|
|
.then(response => response.json())
|
|
|
|
.then(response => response.data)
|
|
|
|
.catch((exc) => {
|
|
|
|
console.error(`Unable to fetch stats: ${exc}.`);
|
|
|
|
throw exc;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-07-05 22:40:24 +02:00
|
|
|
export function downvote(id) {
|
|
|
|
return fetch(`${BASE_URL}api/v1/reports/${id}/downvote`, {
|
|
|
|
method: 'POST',
|
2018-07-18 15:30:30 +02:00
|
|
|
headers: AUTHORIZATION_HEADERS,
|
2018-07-05 22:40:24 +02:00
|
|
|
})
|
|
|
|
.then(response => response.json())
|
|
|
|
.then(response => response.data)
|
|
|
|
.catch((exc) => {
|
|
|
|
console.error(`Unable to downvote report: ${exc}.`);
|
|
|
|
throw exc;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function upvote(id) {
|
|
|
|
return fetch(`${BASE_URL}api/v1/reports/${id}/upvote`, {
|
|
|
|
method: 'POST',
|
2018-07-18 15:30:30 +02:00
|
|
|
headers: AUTHORIZATION_HEADERS,
|
2018-07-05 22:40:24 +02:00
|
|
|
})
|
|
|
|
.then(response => response.json())
|
|
|
|
.then(response => response.data)
|
|
|
|
.catch((exc) => {
|
|
|
|
console.error(`Unable to upvote report: ${exc}.`);
|
|
|
|
throw exc;
|
|
|
|
});
|
|
|
|
}
|