diff --git a/src/api/api.js b/src/api/api.js index 2d6b121af..f17b46ff9 100644 --- a/src/api/api.js +++ b/src/api/api.js @@ -476,6 +476,11 @@ class API { ); return data; } + + async getUserLocation() { + const { data } = await this.axiosInstance.get(`/location`); + return data; + } } const API_INSTANCE = new API({}); diff --git a/src/components/App/App.actions.js b/src/components/App/App.actions.js index 433c19cc6..a3062fd69 100644 --- a/src/components/App/App.actions.js +++ b/src/components/App/App.actions.js @@ -1,10 +1,12 @@ +import API from '../../api'; import { FINISH_FIRST_VISIT, UPDATE_DISPLAY_SETTINGS, UPDATE_NAVIGATION_SETTINGS, UPDATE_USER_DATA, DISABLE_TOUR, - ENABLE_ALL_TOURS + ENABLE_ALL_TOURS, + SET_UNLOGGED_USER_LOCATION } from './App.constants'; export function updateDisplaySettings(payload = {}) { @@ -46,3 +48,58 @@ export function updateUserData(userData) { userData }; } + +export function setUnloggedUserLocation(location) { + return { + type: SET_UNLOGGED_USER_LOCATION, + location + }; +} + +export function updateLoggedUserLocation() { + return async (dispatch, getState) => { + const { + app: { userData } + } = getState(); + if (!userData) return; + try { + const { id, location } = userData; + const APIGetAndUpdateLocation = async () => { + const { location: userLocation } = await API.updateUser({ + id: id, + location: {} + }); + return userLocation; + }; + + if (location) return; + const userLocation = await APIGetAndUpdateLocation(); + if (userLocation) { + dispatch(updateUserData({ ...userData, location: userLocation })); + return; + } + throw new Error('unable to get location'); + } catch { + console.error('error during localization of the logged user'); + } + }; +} + +export function updateUnloggedUserLocation() { + return async (dispatch, getState) => { + const { + app: { unloggedUserLocation } + } = getState(); + try { + if (unloggedUserLocation) return; + const location = await API.getUserLocation(); + if (location) { + dispatch(setUnloggedUserLocation(location)); + return; + } + throw new Error('unable to get location'); + } catch { + console.error('error during localization of the unlogged user'); + } + }; +} diff --git a/src/components/App/App.constants.js b/src/components/App/App.constants.js index 4aeaba8fd..003e2b8ab 100644 --- a/src/components/App/App.constants.js +++ b/src/components/App/App.constants.js @@ -7,6 +7,8 @@ export const UPDATE_DISPLAY_SETTINGS = 'cboard/App/UPDATE_DISPLAY_SETTINGS'; export const UPDATE_NAVIGATION_SETTINGS = 'cboard/App/UPDATE_NAVIGATION_SETTINGS'; export const UPDATE_USER_DATA = 'cboard/App/UPDATE_USER_DATA'; +export const SET_UNLOGGED_USER_LOCATION = + 'cboard/App/SET_UNLOGGED_USER_LOCATION'; // language constants export const DEFAULT_LANG = 'en-US'; export const APP_LANGS = [ diff --git a/src/components/App/App.container.js b/src/components/App/App.container.js index 1bec320db..79d187001 100644 --- a/src/components/App/App.container.js +++ b/src/components/App/App.container.js @@ -10,6 +10,10 @@ import messages from './App.messages'; import App from './App.component'; import { DISPLAY_SIZE_STANDARD } from '../Settings/Display/Display.constants'; +import { + updateLoggedUserLocation, + updateUnloggedUserLocation +} from '../App/App.actions'; export class AppContainer extends Component { static propTypes = { /** @@ -36,10 +40,23 @@ export class AppContainer extends Component { }; componentDidMount() { + const localizeUser = () => { + const { + isLogged, + updateLoggedUserLocation, + updateUnloggedUserLocation + } = this.props; + + if (isLogged) return updateLoggedUserLocation(); + return updateUnloggedUserLocation(); + }; + registerServiceWorker( this.handleNewContentAvailable, this.handleContentCached ); + + localizeUser(); } handleNewContentAvailable = () => { @@ -99,7 +116,9 @@ const mapStateToProps = state => ({ }); const mapDispatchToProps = { - showNotification + showNotification, + updateLoggedUserLocation, + updateUnloggedUserLocation }; export default connect( diff --git a/src/components/App/App.reducer.js b/src/components/App/App.reducer.js index beed787f2..52fe57fa3 100644 --- a/src/components/App/App.reducer.js +++ b/src/components/App/App.reducer.js @@ -5,7 +5,8 @@ import { UPDATE_NAVIGATION_SETTINGS, UPDATE_USER_DATA, DISABLE_TOUR, - ENABLE_ALL_TOURS + ENABLE_ALL_TOURS, + SET_UNLOGGED_USER_LOCATION } from './App.constants'; import { LOGIN_SUCCESS, LOGOUT } from '../Account/Login/Login.constants'; import { @@ -136,6 +137,11 @@ function appReducer(state = initialState, action) { ...state, userData: action.userData }; + case SET_UNLOGGED_USER_LOCATION: + return { + ...state, + unloggedUserLocation: action.location + }; default: return state; }