Skip to content

Commit

Permalink
Merge pull request #1249 from tomivm/feature/localize-logged-user
Browse files Browse the repository at this point in the history
Localize user
  • Loading branch information
martinbedouret authored Aug 3, 2022
2 parents 96a3bea + bc93895 commit 50b4c39
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 3 deletions.
5 changes: 5 additions & 0 deletions src/api/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,11 @@ class API {
);
return data;
}

async getUserLocation() {
const { data } = await this.axiosInstance.get(`/location`);
return data;
}
}

const API_INSTANCE = new API({});
Expand Down
59 changes: 58 additions & 1 deletion src/components/App/App.actions.js
Original file line number Diff line number Diff line change
@@ -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 = {}) {
Expand Down Expand Up @@ -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');
}
};
}
2 changes: 2 additions & 0 deletions src/components/App/App.constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
21 changes: 20 additions & 1 deletion src/components/App/App.container.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
/**
Expand All @@ -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 = () => {
Expand Down Expand Up @@ -99,7 +116,9 @@ const mapStateToProps = state => ({
});

const mapDispatchToProps = {
showNotification
showNotification,
updateLoggedUserLocation,
updateUnloggedUserLocation
};

export default connect(
Expand Down
8 changes: 7 additions & 1 deletion src/components/App/App.reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 50b4c39

Please sign in to comment.