diff --git a/src/api/api.js b/src/api/api.js index f17b46ff9..5a1b427dc 100644 --- a/src/api/api.js +++ b/src/api/api.js @@ -178,6 +178,18 @@ class API { return data; } + async getUserData(userId) { + const authToken = getAuthToken(); + const headers = { + Authorization: `Bearer ${authToken}` + }; + + const { data } = await this.axiosInstance.get(`/user/${userId}`, { + headers + }); + return data; + } + async getBoards({ page = 1, limit = 10, diff --git a/src/components/App/App.actions.js b/src/components/App/App.actions.js index a3062fd69..8f2e325df 100644 --- a/src/components/App/App.actions.js +++ b/src/components/App/App.actions.js @@ -85,6 +85,23 @@ export function updateLoggedUserLocation() { }; } +export function updateUserDataFromAPI() { + return async (dispatch, getState) => { + const { + app: { userData } + } = getState(); + if (!userData) return; + try { + const { id } = userData; + const newUserData = await API.getUserData(id); + dispatch(updateUserData({ ...userData, ...newUserData })); + } catch (error) { + console.error(error); + //could show an alert and offer the posibility of rerun de update. + } + }; +} + export function updateUnloggedUserLocation() { return async (dispatch, getState) => { const { diff --git a/src/components/App/App.container.js b/src/components/App/App.container.js index 79d187001..0330dd8e3 100644 --- a/src/components/App/App.container.js +++ b/src/components/App/App.container.js @@ -11,6 +11,7 @@ import App from './App.component'; import { DISPLAY_SIZE_STANDARD } from '../Settings/Display/Display.constants'; import { + updateUserDataFromAPI, updateLoggedUserLocation, updateUnloggedUserLocation } from '../App/App.actions'; @@ -43,12 +44,18 @@ export class AppContainer extends Component { const localizeUser = () => { const { isLogged, + updateUserDataFromAPI, updateLoggedUserLocation, updateUnloggedUserLocation } = this.props; - if (isLogged) return updateLoggedUserLocation(); + if (isLogged) return loggedActions(); return updateUnloggedUserLocation(); + + async function loggedActions() { + await updateUserDataFromAPI(); + updateLoggedUserLocation(); + } }; registerServiceWorker( @@ -117,6 +124,7 @@ const mapStateToProps = state => ({ const mapDispatchToProps = { showNotification, + updateUserDataFromAPI, updateLoggedUserLocation, updateUnloggedUserLocation };