diff --git a/src/api/posts.api.js b/src/api/posts.api.js index 851cbf2..f1f4399 100644 --- a/src/api/posts.api.js +++ b/src/api/posts.api.js @@ -1,4 +1,5 @@ import axios from "axios" +import {locationUtils} from "../utils/location"; const path = "https://api.jodel.anxietyprime.de/post"; @@ -10,20 +11,17 @@ export const postApi = { return (await axios.get(`${path}/${id}`)).data; }, async createNewPost(title, content) { - if (navigator.geolocation) { - navigator.geolocation.getCurrentPosition(async function(position) { - const latitude = position.coords.latitude; - const longitude = position.coords.longitude; - await axios.post(`${path}s`, { - "authorID": 1, - "title": title, - "content": content, - "date": Date.now().toString(), - "location": { - "longitude": longitude, - "latitude": latitude, - }, - }); + const location = locationUtils.getCurrentLocation(); + if(location.lon && location.lat) { + 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"); diff --git a/src/utils/location.js b/src/utils/location.js new file mode 100644 index 0000000..1bd25c5 --- /dev/null +++ b/src/utils/location.js @@ -0,0 +1,13 @@ +export const locationUtils = { + getCurrentLocation: () => { + if (navigator.geolocation) { + navigator.geolocation.getCurrentPosition(async function(position) { + const latitude = position.coords.latitude; + const longitude = position.coords.longitude; + return {lon: longitude, lat: latitude}; + }); + } else { + return {lon: null, lat: null}; + } + } +} \ No newline at end of file