Files
SWA-frontend/src/api/posts.api.js

52 lines
1.8 KiB
JavaScript

import axios from "axios"
import {locationUtils} from "../utils/location";
import keycloak from "../Authentification/Keycloak";
const path = "https://api.jodel.anxietyprime.de/post";
export const postApi = {
async getPostsByCoords (lon, lat) {
if (!keycloak.authenticated) return [];
return (await axios.get(`${path}s/${lon}/${lat}`)).data;
},
async getPostByID (id) {
if (!keycloak.authenticated) return [];
return (await axios.get(`${path}/${id}`)).data;
},
async createNewPost(title, content, parent = null) {
const location = await locationUtils.getCurrentLocation();
if(location.lon && location.lat) {
if(parent !== null) {
await axios.post(`${path}s`, {
"authorID": 1,
"title": title,
"content": content,
"date": Date.now().toString(),
"location": {
"longitude": location.lon,
"latitude": location.lat,
},
"parent": parent
});
} else {
await axios.post(`${path}s`, {
"authorID": 1,
"title": title,
"content": content,
"date": Date.now().toString(),
"location": {
"longitude": location.lon,
"latitude": location.lat,
}
});
}
} else {
console.log("Geolocation is not supported by this browser. Could'nt post without valid location");
}
},
async reactToPost(id, reaction) {
await axios.put(`${path}/${id}`, {
"reaction": reaction,
})
}
}