diff --git a/src/App.vue b/src/App.vue index adc5459..35cb75a 100755 --- a/src/App.vue +++ b/src/App.vue @@ -13,6 +13,7 @@ diff --git a/src/helpers/index.js b/src/helpers/index.js index 4d267fc..8b662f1 100644 --- a/src/helpers/index.js +++ b/src/helpers/index.js @@ -7,10 +7,11 @@ const isJSON = json => { if (json && typeof json === 'object' && json !== null) { return true } - } catch (err) {} - return false + } catch (error) { + return error + } } -export default { +export { isJSON } diff --git a/src/router.js b/src/router.js index 2b251ae..9be62dd 100755 --- a/src/router.js +++ b/src/router.js @@ -4,20 +4,9 @@ import Home from '@/views/Home' import Splash from '@/views/Splash' import About from '@/views/About' import Store from '@/views/Store' -import user from '@/user' Vue.use(Router) -const requireUser = (to, from, next) => { - if (!user.checkUserGeoData()) { - next({ - path: '/' - }) - } else { - next() - } -} - export default new Router({ scrollBehavior () { return new Promise(resolve => { @@ -41,8 +30,7 @@ export default new Router({ { path: '/home', name: 'home', - component: Home, - beforeEnter: requireUser + component: Home }, { path: '/store/:id', diff --git a/src/store.js b/src/store.js index b9fe184..2cf8832 100755 --- a/src/store.js +++ b/src/store.js @@ -1,6 +1,6 @@ import Vue from 'vue' import Vuex from 'vuex' -import user from '@/user' +import { getUserData } from '@/user' Vue.use(Vuex) @@ -16,8 +16,8 @@ export default new Vuex.Store({ setLoading (state, value) { state.loading = value }, - async setUserGeoData (state, { force }) { - const data = await user.getUserData(force) + async setUserGeoData (state, { force = false }) { + const data = await getUserData(force) state.userData = data }, setMenu (state, value) { diff --git a/src/user.js b/src/user.js index b893757..9947611 100755 --- a/src/user.js +++ b/src/user.js @@ -1,27 +1,63 @@ -import helpers from '@/helpers' +import { isJSON } from '@/helpers' /** * Get user geo from device * @return {Promise} + * https://developer.mozilla.org/en-US/docs/Web/API/Geolocation_API */ const getUserGeoDataFromDevice = () => { return new Promise((resolve, reject) => { - if (!navigator.geolocation) { - return reject( - new Error('geo error') - ) - } else { - navigator.geolocation.getCurrentPosition(result => { - const data = { + if ('geolocation' in navigator) { + navigator.geolocation.getCurrentPosition(result => + resolve({ lat: result.coords.latitude, long: result.coords.longitude + }), error => reject(parseError(error)) + ) + const parseError = error => { + switch (error.code) { + case error.PERMISSION_DENIED: + return { + code: 0, + message: 'User denied the request for geolocation.' + } + case error.POSITION_UNAVAILABLE: + return { + code: 1, + message: 'Location information is unavailable.' + } + case error.TIMEOUT: + return { + code: 2, + message: 'The request to get user location timed out.' + } + case error.UNKNOWN_ERROR: + return { + code: 3, + message: 'An unknown error occurred.' + } } - return resolve(data) + } + } else { + // eslint-disable-next-line prefer-promise-reject-errors + reject({ + code: 3, + message: 'An unknown error occurred.' }) } }) } +/** + * Fetch user geo data + * @return {object} user geo data + */ +const fetchUserGeoData = async () => { + const data = await getUserGeoDataFromDevice() + setUserGeoDataToStorage(data) + return data +} + /** * Set flag if user has joined to app */ @@ -60,17 +96,7 @@ const getUserGeoDataFromStorage = () => { */ const checkUserGeoData = () => { const data = getUserGeoDataFromStorage() - return helpers.isJSON(data) && data.lat && data.long -} - -/** - * Fetch user geo data - * @return {object} user geo data - */ -const fetchUserGeoData = async () => { - const data = await getUserGeoDataFromDevice() - setUserGeoDataToStorage(data) - return data + return isJSON(data) && data.lat && data.long } /** @@ -78,19 +104,24 @@ const fetchUserGeoData = async () => { * @param {boolean} force * @return {Promise} user geo data */ -const getUserData = async (force = false) => { - if (force) { - return fetchUserGeoData() - } else { - return checkUserGeoData() - ? getUserGeoDataFromStorage() - : fetchUserGeoData() +const getUserData = (force = false) => { + try { + if (force) { + return fetchUserGeoData() + } else { + return checkUserGeoData() + ? getUserGeoDataFromStorage() + : fetchUserGeoData() + } + } catch (error) { + return error } } -export default { +export { getUserData, checkUserGeoData, checkHasJoined, - setUserHasJoined + setUserHasJoined, + fetchUserGeoData } diff --git a/src/views/Splash.vue b/src/views/Splash.vue index 1296d9f..bf9365f 100755 --- a/src/views/Splash.vue +++ b/src/views/Splash.vue @@ -26,10 +26,18 @@

Obteniendo tu ubicación...

- @@ -50,7 +58,7 @@