Currently, I am using the Vuex store for my authentication logic and I understand why being able to access stuff like user token, user id and username from anywhere in my application is extremely useful. I'm 100% sure that the authentication logic belongs to the store.
However, I'm currently playing around with Google Maps and have made a component which has a Google map in it and 3 functions for displaying, adding and removing Google markers from the map. Now I can't figure out if I should move this logic to the Vuex store. I think I will might have to use what is inside the markers: [] property in some other components in the future but everything else seems like it won't be used in any other components.
One of the pros of using Vuex store in this case is that it would make this component more readable and all my logic will be in one place but I'm not sure if that warrants moving the logic.
<template>
<div class="container">
<div class="map">
<GmapMap
@click='addMarker'
:center="center"
:zoom="zoom"
:map-type-id="map"
style="width: 100%; height: 800px"
>
<GmapMarker
:key="index"
v-for="(marker, index) in markers"
:position="marker"
:clickable="true"
:draggable="false"
@click="removeMarker(index)"
/>
</GmapMap>
</div>
</div>
</template>
<script>
import axios from 'axios'
export default {
data: function() {
return {
markers: [],
center: {lat: 42.150527, lng: 24.746477},
zoom: 15,
map: 'roadmap'
}
},
methods: {
addMarker(event){
axios.post('/location', {
userId: this.$store.state.auth.userId,
marker: event.latLng
}).then(response => {
this.markers.push({ 'id': response.data.locations.id,
'lat': response.data.locations.lat,
'lng': response.data.locations.lng})
}).catch((error) => console.log(error));
},
removeMarker(index){
const markerId = this.markers[index].id
this.markers.splice(index, 1);
axios.post('/deleteLocation', {
userId: this.$store.state.auth.userId,
markerId: markerId
})
}
},
mounted(){
axios.get('/location', {
userId: 1
}).then(response => {
this.markers = response.data.locations
}).catch((error) => console.log(error));
}
}
</script>