Skip to content

refactor(auth): get user data after login #502

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

Merged
merged 2 commits into from
Oct 20, 2017
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
10 changes: 4 additions & 6 deletions src/auth/auth.action.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { AsyncStorage } from 'react-native';

import uniqby from 'lodash.uniqby';
import { delay, resetNavigationTo, configureLocale, saveLocale } from 'utils';
import { delay, configureLocale, saveLocale } from 'utils';

import {
fetchAccessToken,
Expand All @@ -21,18 +21,16 @@ import {
GET_AUTH_STAR_COUNT,
} from './auth.type';

export const auth = (code, state, navigation) => {
export const auth = (code, state) => {
return dispatch => {
dispatch({ type: LOGIN.PENDING });

delay(fetchAccessToken(code, state), 2000)
return delay(fetchAccessToken(code, state), 2000)
.then(data => {
dispatch({
type: LOGIN.SUCCESS,
payload: data.access_token,
});

resetNavigationTo('Main', navigation);
})
.catch(error => {
dispatch({
Expand Down Expand Up @@ -68,7 +66,7 @@ export const getUser = () => {

dispatch({ type: GET_AUTH_USER.PENDING });

fetchAuthUser(accessToken)
return fetchAuthUser(accessToken)
.then(data => {
dispatch({
type: GET_AUTH_USER.SUCCESS,
Expand Down
21 changes: 5 additions & 16 deletions src/auth/screens/events.screen.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import moment from 'moment/min/moment-with-locales.min';
import { LoadingUserListItem, UserListItem, ViewContainer } from 'components';
import { colors, fonts, normalize } from 'config';
import { emojifyText, translate } from 'utils';
import { getUserEvents, getUser } from 'auth';
import { getUserEvents } from 'auth';
import { getNotificationsCount } from 'notifications';

const mapStateToProps = state => ({
Expand All @@ -25,7 +25,6 @@ const mapDispatchToProps = dispatch =>
{
getUserEvents,
getNotificationsCount,
getUser,
},
dispatch
);
Expand Down Expand Up @@ -79,13 +78,7 @@ const styles = StyleSheet.create({

class Events extends Component {
componentDidMount() {
const { user: { login }, getUser } = this.props;

if (login) {
this.getUserEvents();
} else {
getUser();
}
this.getUserEvents();
}

componentWillReceiveProps(nextProps) {
Expand Down Expand Up @@ -173,13 +166,9 @@ class Events extends Component {
}
);
} else if (action === 'edited') {
return translate(
'auth.events.pullRequestReviewEditedEvent',
locale,
{
action: translate(`auth.events.actions.${action}`, locale),
}
);
return translate('auth.events.pullRequestReviewEditedEvent', locale, {
action: translate(`auth.events.actions.${action}`, locale),
});
} else if (action === 'deleted') {
return translate(
'auth.events.pullRequestReviewDeletedEvent',
Expand Down
43 changes: 26 additions & 17 deletions src/auth/screens/login.screen.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,23 @@ import CookieManager from 'react-native-cookies';
import { ViewContainer } from 'components';
import { colors, fonts, normalize } from 'config';
import { CLIENT_ID } from 'api';
import { auth } from 'auth';
import { translate } from 'utils';
import { auth, getUser } from 'auth';
import { translate, resetNavigationTo } from 'utils';

let stateRandom = Math.random().toString();

const mapStateToProps = state => ({
locale: state.auth.locale,
isLoggingIn: state.auth.isLoggingIn,
isAuthenticated: state.auth.isAuthenticated,
hasInitialUser: state.auth.hasInitialUser,
});

const mapDispatchToProps = dispatch =>
bindActionCreators(
{
auth,
getUser,
},
dispatch
);
Expand Down Expand Up @@ -149,8 +151,10 @@ class Login extends Component {
props: {
isAuthenticated: boolean,
isLoggingIn: boolean,
hasInitialUser: boolean,
locale: string,
auth: Function,
getUser: Function,
navigation: Object,
};

Expand Down Expand Up @@ -214,7 +218,7 @@ class Login extends Component {
if (url && url.substring(0, 11) === 'gitpoint://') {
const [, queryStringFromUrl] = url.match(/\?(.*)/);
const { state, code } = queryString.parse(queryStringFromUrl);
const { auth, navigation } = this.props;
const { auth, getUser, navigation } = this.props;

if (stateRandom === state) {
this.setState({
Expand All @@ -226,7 +230,11 @@ class Login extends Component {
stateRandom = Math.random().toString();

CookieManager.clearAll().then(() => {
auth(code, state, navigation);
auth(code, state).then(() => {
getUser().then(() => {
resetNavigationTo('Main', navigation);
});
});
});
}
}
Expand All @@ -248,7 +256,7 @@ class Login extends Component {
}

render() {
const { locale, isLoggingIn } = this.props;
const { locale, isLoggingIn, hasInitialUser } = this.props;

return (
<ViewContainer barColor="light">
Expand Down Expand Up @@ -362,18 +370,19 @@ class Login extends Component {
</View>
</Modal>
)}
{isLoggingIn && (
<View style={styles.browserLoader}>
<Text style={styles.browserLoadingLabel}>
{this.state.loaderText}
</Text>
<ActivityIndicator
animating={isLoggingIn}
color={colors.white}
size="large"
/>
</View>
)}
{isLoggingIn &&
!hasInitialUser && (
<View style={styles.browserLoader}>
<Text style={styles.browserLoadingLabel}>
{this.state.loaderText}
</Text>
<ActivityIndicator
animating={isLoggingIn}
color={colors.white}
size="large"
/>
</View>
)}
</ViewContainer>
);
}
Expand Down