Skip to content

Commit

Permalink
Merge pull request #173 from QUACK-INTEC/cr/feature/SyncRequired
Browse files Browse the repository at this point in the history
Cr/feature/sync required
  • Loading branch information
jtvargas authored Jan 15, 2020
2 parents efb3f99 + cc459ac commit 73b60a2
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 75 deletions.
5 changes: 5 additions & 0 deletions App/Core/Api/Api.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ const TokenRefresh = () => {
return MemoApi.get(`auth/refresh`);
};

const CheckSync = () => {
return MemoApi.get(`sync/check`);
};

const Login = objUserLoginParams => {
return MemoApi.post(`auth/login`, objUserLoginParams);
};
Expand Down Expand Up @@ -169,6 +173,7 @@ const ValidateRecoveryCode = objUserOTPParam => {
const Api = {
AuthCheck,
TokenRefresh,
CheckSync,
Login,
Register,
UploadProfilePicture,
Expand Down
8 changes: 1 addition & 7 deletions App/Navigation/AppToken.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ class AppToken extends Component {
}, 1050);
});
}

return this.setState({ tokenReady: true });
};

Expand Down Expand Up @@ -83,12 +82,7 @@ const mapDispatchToProps = dispatch => {
);
};

const AppTokenWithProps = WithLogger(
connect(
mapStateToProps,
mapDispatchToProps
)(AppToken)
);
const AppTokenWithProps = WithLogger(connect(mapStateToProps, mapDispatchToProps)(AppToken));

AppToken.defaultProps = {
setUserToken: () => null,
Expand Down
6 changes: 6 additions & 0 deletions App/Navigation/MainTabNavigator.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ const HomeStack = createStackNavigator(
PostComments: PostCommentsScreen,
PostResources: PostResourcesScreen,
ViewResource: ViewResourceScreen,
Sync: {
screen: SyncScreen,
navigationOptions: {
gesturesEnabled: false,
},
},
},
config
);
Expand Down
1 change: 1 addition & 0 deletions App/Redux/Common/UserManager/ActionTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export const SET_USER_TOKEN = 'SET_USER_TOKEN';
export const SET_USER_INFO = 'SET_USER_INFO';
export const SET_USER_UNIVERSITY_SYNC = 'SET_USER_UNIVERSITY_SYNC';
export const LOG_OUT = 'LOG_OUT';
export const SET_SYNC_REQUIRED = 'SET_SYNC_REQUIRED';
1 change: 1 addition & 0 deletions App/Redux/Common/UserManager/Actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export const setUserToken = createAction(types.SET_USER_TOKEN);
export const setUserInfo = createAction(types.SET_USER_INFO);
export const setUserSync = createAction(types.SET_USER_UNIVERSITY_SYNC);
export const logout = createAction(types.LOG_OUT);
export const setSyncRequired = createAction(types.SET_SYNC_REQUIRED);
10 changes: 10 additions & 0 deletions App/Redux/Common/UserManager/Reducers.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,22 @@ const onSetLogout = () => {
};
};

// SET_SYNC_REQUIRED
const onSetSyncRequired = (state, action) => {
const required = Lodash.get(action, ['payload'], '');
return {
...state,
syncRequired: required,
};
};

const contentReducer = typeToReducer(
{
[types.SET_USER_TOKEN]: onSetUserToken,
[types.SET_USER_INFO]: onSetUserInfo,
[types.SET_USER_UNIVERSITY_SYNC]: onSetUserUniversity,
[types.LOG_OUT]: onSetLogout,
[types.SET_SYNC_REQUIRED]: onSetSyncRequired,
},
initialState
);
Expand Down
106 changes: 39 additions & 67 deletions App/Redux/Common/UserManager/Selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,80 +3,51 @@ import Lodash from 'lodash';

const getMyUserManager = objState => Lodash.get(objState, ['userManager'], {});

export const isLogged = createSelector(
getMyUserManager,
objState => {
const value = Lodash.get(objState, ['token'], null);
return value != null;
}
);
export const isLogged = createSelector(getMyUserManager, objState => {
const value = Lodash.get(objState, ['token'], null);
return value != null;
});

export const getUserToken = createSelector(
getMyUserManager,
objState => {
return Lodash.get(objState, ['token'], null);
}
);
export const getUserToken = createSelector(getMyUserManager, objState => {
return Lodash.get(objState, ['token'], null);
});

export const getUserId = createSelector(
getMyUserManager,
objState => {
return Lodash.get(objState, ['user', 'id'], null);
}
);
export const getUserId = createSelector(getMyUserManager, objState => {
return Lodash.get(objState, ['user', 'id'], null);
});

export const getFirstName = createSelector(
getMyUserManager,
objState => {
return Lodash.get(objState, ['user', 'firstName'], null);
}
);
export const getFirstName = createSelector(getMyUserManager, objState => {
return Lodash.get(objState, ['user', 'firstName'], null);
});

export const getLastName = createSelector(
getMyUserManager,
objState => {
return Lodash.get(objState, ['user', 'lastName'], null);
}
);
export const getLastName = createSelector(getMyUserManager, objState => {
return Lodash.get(objState, ['user', 'lastName'], null);
});

export const getAvatarUser = createSelector(
getMyUserManager,
objState => {
return Lodash.get(objState, ['user', 'avatarURL'], null);
}
);
export const getEmail = createSelector(
getMyUserManager,
objState => {
return Lodash.get(objState, ['user', 'email'], null);
}
);
export const getPoints = createSelector(
getMyUserManager,
objState => {
return Lodash.get(objState, ['user', 'points'], 0);
}
);
export const getRank = createSelector(
getMyUserManager,
objState => {
return Lodash.get(objState, ['user', 'rank'], 'unranked');
}
);
export const getAvatarUser = createSelector(getMyUserManager, objState => {
return Lodash.get(objState, ['user', 'avatarURL'], null);
});
export const getEmail = createSelector(getMyUserManager, objState => {
return Lodash.get(objState, ['user', 'email'], null);
});
export const getPoints = createSelector(getMyUserManager, objState => {
return Lodash.get(objState, ['user', 'points'], 0);
});
export const getRank = createSelector(getMyUserManager, objState => {
return Lodash.get(objState, ['user', 'rank'], 'unranked');
});

export const getRankName = createSelector(
getMyUserManager,
objState => {
return Lodash.get(objState, ['user', 'rank', 'name'], '');
}
);
export const getRankName = createSelector(getMyUserManager, objState => {
return Lodash.get(objState, ['user', 'rank', 'name'], '');
});

export const getBadgeUrl = createSelector(
getMyUserManager,
objState => {
return Lodash.get(objState, ['user', 'rank', 'badgeUrl'], '');
}
);
export const getBadgeUrl = createSelector(getMyUserManager, objState => {
return Lodash.get(objState, ['user', 'rank', 'badgeUrl'], '');
});

export const isSyncRequired = createSelector(getMyUserManager, objState => {
return Lodash.get(objState, ['syncRequired'], false);
});

export default {
getEmail,
Expand All @@ -90,4 +61,5 @@ export default {
getRank,
getRankName,
getBadgeUrl,
isSyncRequired,
};
44 changes: 43 additions & 1 deletion App/Screens/Home/Home.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import PropTypes from 'prop-types';

import LoadingState from '../../Components/LoadingState';
import HomeComponent from '../../Components/Home';

import EnforcedToast from '../../Components/Common/EnforcedToast';
import { colors, fonts, spacers } from '../../Core/Theme';
import Api, { MemoApi, RegisterForNotifications } from '../../Core/Api';
import WithLogger, { MessagesKey } from '../../HOCs/WithLogger';
Expand All @@ -25,12 +27,15 @@ import { selectors as EventFormSelectors } from '../EventForm/Redux';
import LoadingList from '../../Components/LoadingList';
import { Classes, Events } from '../../Models';

const SYNC_MSG = 'Sincronización de materias requerida!';

class Home extends React.Component {
constructor(props) {
super(props);
this.state = {
isLoading: true,
isRefreshing: false,
syncRequired: false,
};
}

Expand All @@ -43,6 +48,7 @@ class Home extends React.Component {
await RegisterForNotifications();

this.fetchEventsAndClasses();
this.isSyncRequired();
}

componentDidUpdate(prevProps) {
Expand All @@ -67,6 +73,29 @@ class Home extends React.Component {
return Api.GetEvents(today, null, false);
};

isSyncRequired = () => {
const { logger, userToken, setSyncRequired } = this.props;

MemoApi.defaults.headers.common.Authorization = `Bearer ${userToken}`;
return Api.CheckSync()
.then(objResponse => {
const required = Lodash.get(objResponse, ['data', 'syncRequired'], false);
setSyncRequired(required);
if (required) {
// this.goToSync();
this.setState({ syncRequired: true });
}
})
.catch(objError => {
return setTimeout(() => {
logger.error({
key: MessagesKey.SIGN_IN_FAILED,
data: objError,
});
}, 1050);
});
};

fetchEventsAndClasses = () => {
const { setMyClasses, logger } = this.props;

Expand Down Expand Up @@ -117,6 +146,14 @@ class Home extends React.Component {
return push('ClassHub', { id: idSection });
};

goToSync = () => {
const {
navigation: { push },
} = this.props;
this.setState({ syncRequired: false });
return push('Sync', { canNavigate: false, nextScreen: 'Home', showMSG: true });
};

handleOnEventPress = objEvent => {
const {
navigation: { push },
Expand Down Expand Up @@ -213,6 +250,7 @@ class Home extends React.Component {
};

handleOnRefresh = () => {
this.isSyncRequired();
return this.setState(
{
isRefreshing: true,
Expand Down Expand Up @@ -284,9 +322,10 @@ class Home extends React.Component {
};

render() {
const { isLoading, isRefreshing } = this.state;
const { isLoading, isRefreshing, syncRequired } = this.state;
return (
<>
<EnforcedToast isVisible={syncRequired} title={SYNC_MSG} onPress={() => this.goToSync()} />
<LoadingState.withoutLottie isVisible={isLoading} />
<HomeComponent
isLoading={isLoading}
Expand Down Expand Up @@ -324,12 +363,14 @@ Home.defaultProps = {
myClasses: [],
setMyClasses: () => null,
myClassesLookup: {},
setSyncRequired: () => null,
};

Home.propTypes = {
myClasses: PropTypes.arrayOf(PropTypes.string),
setMyClasses: PropTypes.func,
myClassesLookup: PropTypes.shape({}),
setSyncRequired: PropTypes.func,
};

const mapStateToProps = (state, props) => {
Expand All @@ -350,6 +391,7 @@ const mapDispatchToProps = dispatch => {
{
setMyClasses: classesActions.setClasses,
setUserToken: userActions.setUserToken,
setSyncRequired: userActions.setSyncRequired,
},
dispatch
);
Expand Down

0 comments on commit 73b60a2

Please sign in to comment.