Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/update user data on app mount #1339

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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