Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. const state = {
  2. loading: false,
  3. geolocation: {},
  4. };
  5.  
  6. const mutations = {
  7. TOGGLE_LOADING(state, payload) {
  8. state.loading = !!payload;
  9. },
  10. CHANGE_GEOLOCATION(state, payload) {
  11. state.geolocation = payload;
  12. },
  13. };
  14.  
  15. const actions = {
  16. currentLocation({ commit }) {
  17. const { geolocation } = navigator;
  18. commit('TOGGLE_LOADING', true);
  19.  
  20. return new Promise((resolve, reject) => {
  21. if (geolocation) {
  22. geolocation
  23. .getCurrentPosition(({ coords }) => {
  24. const currentPosition = {
  25. long: coords.longitude,
  26. lat: coords.latitude,
  27. };
  28.  
  29. commit('CHANGE_GEOLOCATION', currentPosition);
  30. commit('TOGGLE_LOADING', false);
  31. resolve(currentPosition);
  32. },
  33. (err) => {
  34. commit('TOGGLE_LOADING', false);
  35. reject(err);
  36. });
  37. } else {
  38. console.info('Geolocation is not supported by this browser.');
  39. commit('TOGGLE_LOADING', false);
  40. reject();
  41. }
  42. });
  43. },
  44. };
  45.  
  46. export const store = {
  47. namespaced: true,
  48. state,
  49. mutations,
  50. actions,
  51. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement