Skip to content

Commit

Permalink
Merge pull request #1339 from tomivm/feature/update-userData-on-app-m…
Browse files Browse the repository at this point in the history
…ount

Feature/update user data on app mount
  • Loading branch information
martinbedouret authored Mar 5, 2023
2 parents 06adc49 + 35bd1df commit 03a4db2
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/api/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
17 changes: 17 additions & 0 deletions src/components/App/App.actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
10 changes: 9 additions & 1 deletion src/components/App/App.container.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -117,6 +124,7 @@ const mapStateToProps = state => ({

const mapDispatchToProps = {
showNotification,
updateUserDataFromAPI,
updateLoggedUserLocation,
updateUnloggedUserLocation
};
Expand Down

0 comments on commit 03a4db2

Please sign in to comment.