Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const state = {
- loading: false,
- geolocation: {},
- };
- const mutations = {
- TOGGLE_LOADING(state, payload) {
- state.loading = !!payload;
- },
- CHANGE_GEOLOCATION(state, payload) {
- state.geolocation = payload;
- },
- };
- const actions = {
- currentLocation({ commit }) {
- const { geolocation } = navigator;
- commit('TOGGLE_LOADING', true);
- return new Promise((resolve, reject) => {
- if (geolocation) {
- geolocation
- .getCurrentPosition(({ coords }) => {
- const currentPosition = {
- long: coords.longitude,
- lat: coords.latitude,
- };
- commit('CHANGE_GEOLOCATION', currentPosition);
- commit('TOGGLE_LOADING', false);
- resolve(currentPosition);
- },
- (err) => {
- commit('TOGGLE_LOADING', false);
- reject(err);
- });
- } else {
- console.info('Geolocation is not supported by this browser.');
- commit('TOGGLE_LOADING', false);
- reject();
- }
- });
- },
- };
- export const store = {
- namespaced: true,
- state,
- mutations,
- actions,
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement