mirror of
https://github.com/KevinMidboe/leifsopplevelser.git
synced 2025-10-29 09:40:21 +00:00
All api calls are now piped through utils script to easier control base url.
This commit is contained in:
@@ -34,6 +34,8 @@ import DayElement from '@/components/calendar/DayElement'
|
||||
import MonthSummary from '@/components/calendar/MonthSummary'
|
||||
import moment from 'moment'
|
||||
|
||||
import { adventureList } from '@/utils/leifsbackend-api'
|
||||
|
||||
export default {
|
||||
components: { DayElement, MonthSummary },
|
||||
props: {
|
||||
@@ -95,8 +97,7 @@ export default {
|
||||
.then(events => this.populateCalendar(events))
|
||||
},
|
||||
getEvents() {
|
||||
return fetch('http://localhost:5000/api/adventure')
|
||||
.then(resp => resp.json())
|
||||
return adventureList()
|
||||
.then(events => {
|
||||
console.log('events', events)
|
||||
this.eventsFound = events;
|
||||
|
||||
@@ -54,6 +54,8 @@ import VueGridLayout from 'vue-grid-layout';
|
||||
import ClickOutside from 'vue-click-outside'
|
||||
import EventForm from './EventForm'
|
||||
|
||||
import { adventureById, createAdventure } from '@/utils/leifsbackend-api'
|
||||
|
||||
export default {
|
||||
components: {
|
||||
GridLayout: VueGridLayout.GridLayout,
|
||||
@@ -147,8 +149,7 @@ export default {
|
||||
},
|
||||
methods: {
|
||||
fetchById(id) {
|
||||
fetch('http://localhost:5000/api/adventure/' + id)
|
||||
.then(resp => resp.json())
|
||||
adventureById(id)
|
||||
.then(data => {
|
||||
this.formData = data;
|
||||
console.log('data', data)
|
||||
@@ -166,19 +167,7 @@ export default {
|
||||
mapboxData: this.selectedPlace
|
||||
}
|
||||
|
||||
fetch('http://localhost:5000/api/adventure', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Accept': 'application/json',
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify(data)
|
||||
})
|
||||
// axios.post('localhost:5000/api/adventure', {
|
||||
// body: JSON.stringify(data)
|
||||
// })
|
||||
.then((resp) => console.log('response from posting to server:', resp))
|
||||
.catch((error) => console.error('error from post request:', error))
|
||||
createAdventure(data)
|
||||
},
|
||||
processFile(event) {
|
||||
this.files = event.target.files;
|
||||
|
||||
@@ -60,6 +60,8 @@ import FormElementLocation from './form/FormElementLocation'
|
||||
import FormElementUpload from './form/FormElementUpload'
|
||||
import { dateToDayMonthYearDashed } from '@/utils/dates'
|
||||
|
||||
import { createAdventure } from '@/utils/leifsbackend-api'
|
||||
|
||||
export default {
|
||||
components: {
|
||||
FormElementLocation,
|
||||
@@ -118,20 +120,7 @@ export default {
|
||||
console.log('Processing form to post to backend with data', data)
|
||||
|
||||
|
||||
fetch('http://localhost:5000/api/adventure', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Accept': 'application/json',
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify(data)
|
||||
})
|
||||
|
||||
// axios.post('localhost:5000/api/adventure', {
|
||||
// body: JSON.stringify(data)
|
||||
// })
|
||||
.then((resp) => console.log('response from posting to server:', resp))
|
||||
.catch((error) => console.error('error from post request:', error))
|
||||
createAdventure(data)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,6 +39,8 @@ import Gallery from '@/components/Gallery'
|
||||
import MapView from '@/components/MapView'
|
||||
import moment from 'moment'
|
||||
|
||||
import { locationByName } from '@/utils/leifsbackend-api'
|
||||
|
||||
export default {
|
||||
components: {
|
||||
Gallery, MapView
|
||||
@@ -74,11 +76,7 @@ export default {
|
||||
const id = this.$route.params.id;
|
||||
console.log('id found', id)
|
||||
if (id) {
|
||||
fetch('http://localhost:5000/api/adventure/' + id)
|
||||
.then(resp => {
|
||||
console.log('resp', resp)
|
||||
resp.json()
|
||||
})
|
||||
adventureById(id)
|
||||
.then(data => this.eventData = data)
|
||||
}
|
||||
},
|
||||
@@ -93,11 +91,7 @@ export default {
|
||||
return
|
||||
}
|
||||
|
||||
var url = new URL('http://localhost:5000/api/location')
|
||||
url.search = new URLSearchParams({ name: this.eventData.locationName })
|
||||
|
||||
fetch(url)
|
||||
.then(resp => resp.json())
|
||||
locationByName(this.eventData.locationName)
|
||||
.then(data => {
|
||||
this.mapboxData = data.mapboxData;
|
||||
this.showMap = true;
|
||||
|
||||
@@ -21,6 +21,9 @@ import GalleryText from '@/components/GalleryText'
|
||||
import { mapGetters } from 'vuex'
|
||||
import store from '@/store'
|
||||
|
||||
import { imagesByAdventureId } from '@/utils/leifsbackend-api'
|
||||
|
||||
|
||||
export default {
|
||||
name: 'Gallery-Item',
|
||||
components: { GalleryImage, GalleryText },
|
||||
@@ -49,8 +52,7 @@ export default {
|
||||
}
|
||||
},
|
||||
created() {
|
||||
fetch('http://localhost:5000/api/images/' + this.id)
|
||||
.then(resp => resp.json())
|
||||
imagesByAdventureId(this.id)
|
||||
.then(images => {
|
||||
console.log('events', images)
|
||||
images.forEach(image => {
|
||||
|
||||
@@ -26,6 +26,8 @@ import EventPage from '@/components/EventPage'
|
||||
import Calendar from '@/components/Calendar'
|
||||
import Footer from '@/components/Footer'
|
||||
|
||||
import { adventureList } from '@/utils/leifsbackend-api'
|
||||
|
||||
export default {
|
||||
components: { Header, EventPage, Calendar, Footer },
|
||||
data() {
|
||||
@@ -41,8 +43,7 @@ export default {
|
||||
|
||||
methods: {
|
||||
fetchEvents() {
|
||||
fetch('http://localhost:5000/api/adventure')
|
||||
.then(resp => resp.json())
|
||||
adventureList()
|
||||
.then((data) => {
|
||||
console.log('response from fetch events', data)
|
||||
this.events = data;
|
||||
|
||||
@@ -17,6 +17,8 @@
|
||||
import axios from 'axios'
|
||||
import store from '@/store'
|
||||
|
||||
import { createImages } from '@/utils/leifsbackend-api'
|
||||
|
||||
export default {
|
||||
data() {
|
||||
return {
|
||||
@@ -43,9 +45,11 @@ export default {
|
||||
}
|
||||
|
||||
console.log('formdata', formData)
|
||||
axios.post('http://localhost:5000/api/upload/' + 1, formData, { onUploadProgress: progressEvent => console.log(100 * (progressEvent.loaded / progressEvent.totalSize))} )
|
||||
.then((resp) => console.log('response from posting to server:', resp))
|
||||
.catch((error) => console.error('error from post request:', error))
|
||||
createImages(formData, 1, this.progressEvent) // passes formData (fileList of images) and adventureId
|
||||
},
|
||||
|
||||
progressEvent(event) {
|
||||
console.log(100 * (event.loaded / event.totalSize))
|
||||
},
|
||||
|
||||
processFiles(event) {
|
||||
|
||||
68
src/utils/leifsbackend-api.js
Normal file
68
src/utils/leifsbackend-api.js
Normal file
@@ -0,0 +1,68 @@
|
||||
import axios from 'axios'
|
||||
|
||||
const BASE_URL = 'http://localhost:5000/api/'
|
||||
|
||||
function adventureList() {
|
||||
const url = BASE_URL + 'adventure';
|
||||
|
||||
return fetch(url)
|
||||
.then(resp => resp.json())
|
||||
.catch(err => console.log('unable to fetch adventure.'))
|
||||
}
|
||||
|
||||
function adventureById(id) {
|
||||
const url = BASE_URL + 'adventure/' + id;
|
||||
|
||||
return fetch(url)
|
||||
.then(resp => resp.json())
|
||||
.catch(err => console.log('unable to fetch adventure.'))
|
||||
}
|
||||
|
||||
function createAdventure(data) {
|
||||
const url = BASE_URL + 'adventure';
|
||||
const jsonBody = JSON.stringify(data);
|
||||
const headers = {
|
||||
'Accept': 'application/json',
|
||||
'Content-Type': 'application/json'
|
||||
};
|
||||
|
||||
return fetch(url, { method: 'POST', headers: headers, body: jsonBody })
|
||||
.then((resp) => console.log('response from posting to server:', resp))
|
||||
.catch((error) => console.error('error from post request:', error))
|
||||
}
|
||||
|
||||
|
||||
function locationByName(locationName) {
|
||||
let url = new URL(BASE_URL + 'location')
|
||||
url.search = new URLSearchParams({ name: locationName })
|
||||
|
||||
return fetch(url)
|
||||
.then(resp => resp.json())
|
||||
.catch(err => console.log('unable to fetch location.'))
|
||||
}
|
||||
|
||||
function imagesByAdventureId(adventureId) {
|
||||
const url = BASE_URL + 'images/' + adventureId;
|
||||
|
||||
return fetch(url)
|
||||
.then(resp => resp.json())
|
||||
.catch(err => console.log('unable to fetch images.'))
|
||||
}
|
||||
|
||||
function createImages(FileFormData, adventureId, progressEvent=undefined) {
|
||||
const url = BASE_URL + 'upload/' + adventureId;
|
||||
|
||||
axios.post(url, FileFormData, { onUploadProgress: progressEvent } )
|
||||
.then((resp) => console.log('response from posting to server:', resp))
|
||||
.catch((error) => console.error('error from post request:', error))
|
||||
}
|
||||
|
||||
|
||||
export {
|
||||
adventureList,
|
||||
adventureById,
|
||||
createAdventure,
|
||||
locationByName,
|
||||
imagesByAdventureId,
|
||||
createImages
|
||||
}
|
||||
Reference in New Issue
Block a user