From 3143f0de46637149142b6dacde296377a96a8e5e Mon Sep 17 00:00:00 2001 From: Raul Hernandez Date: Wed, 16 Oct 2019 22:21:52 -0300 Subject: [PATCH 1/3] fix: mejora logica de deteccion geo y catch de errores --- src/helpers/index.js | 7 ++-- src/store.js | 6 +-- src/user.js | 89 +++++++++++++++++++++++++++++--------------- 3 files changed, 67 insertions(+), 35 deletions(-) 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/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 } From b236fdfb4a01f0694628a685957ad3216b39ada0 Mon Sep 17 00:00:00 2001 From: Raul Hernandez Date: Wed, 16 Oct 2019 22:24:04 -0300 Subject: [PATCH 2/3] fix: remueve validacion innecesaria de routes --- src/router.js | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) 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', From 4704839e8b1ff6fe2c364795ff4ce247acdc9709 Mon Sep 17 00:00:00 2001 From: Raul Hernandez Date: Wed, 16 Oct 2019 22:24:41 -0300 Subject: [PATCH 3/3] fix: mejora validaciones y mensajes para detecctar geo --- src/App.vue | 13 ++++++++-- src/views/Splash.vue | 58 +++++++++++++++++++++++++------------------- 2 files changed, 44 insertions(+), 27 deletions(-) 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/views/Splash.vue b/src/views/Splash.vue index 93097e1..b867e53 100755 --- a/src/views/Splash.vue +++ b/src/views/Splash.vue @@ -27,10 +27,18 @@

Obteniendo tu ubicación...

- @@ -51,7 +59,7 @@