Last active
October 26, 2017 10:07
-
-
Save 8ui/73c75d950400198fe403d9ab98f50b52 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import { connect } from 'react-redux'; | |
import Intercom from 'react-native-intercom'; | |
import CodePush from 'react-native-code-push'; | |
import { | |
AppState, | |
} from 'react-native'; | |
import { | |
websocket, | |
user, | |
store, | |
staff, | |
} from 'cloudshop/app/actions'; | |
import { | |
Progress, | |
} from 'cloudshop/app/common'; | |
import Entry from 'cloudshop/app/containers/Entry'; | |
export default () => ( | |
Component => { | |
class AuthenticatedComponent extends React.Component { | |
state = { | |
loaded: false, | |
login: false, | |
} | |
componentWillMount(){ | |
if (!this.props.user.isAuthenticated){ | |
this.load(); | |
} | |
} | |
componentDidMount(){ | |
AppState.addEventListener('change', this.handleAppStateChange); | |
CodePush.sync({installMode: CodePush.InstallMode.ON_NEXT_RESUME}); | |
} | |
componentWillReceiveProps({user: {isAuthenticated}}){ | |
if (isAuthenticated && !this.props.user.isAuthenticated){ | |
setTimeout(() => this.load()); | |
} else if (!isAuthenticated && this.props.user.isAuthenticated){ | |
this.setState({login: true}); | |
} | |
} | |
handleAppStateChange(appState){ | |
if (appState === 'active'){ | |
CodePush.sync({installMode: CodePush.InstallMode.ON_NEXT_RESUME}); | |
} | |
} | |
load = () => { | |
const { dispatch } = this.props; | |
this.setState({loaded: false, login: false}); | |
dispatch(user.load()) | |
.then(this.initIntercom) | |
.then(() => dispatch(store.load())) | |
.then(() => dispatch(staff.load())) | |
.then(() => dispatch(websocket.init())) | |
.then(() => this.setState({loaded: true})) | |
.catch(e => { | |
this.setState({login: true}); | |
}); | |
} | |
initIntercom = data => { | |
Intercom.registerIdentifiedUser({ | |
user_id: data._id, | |
name: data.name, | |
email: data.email, | |
company: { | |
id: data._getPositions()._id, | |
name: data._getCompany().name || data._getPositions().email, | |
created_at: data._getPositions().created, | |
Phone: 0, | |
}, | |
}); | |
Intercom.setBottomPadding(40); | |
} | |
renderLoading = () => ( | |
<Progress /> | |
) | |
render() { | |
const { loaded, login } = this.state; | |
if (login){ | |
return <Entry />; | |
} else if (loaded){ | |
return <Component {...this.props} />; | |
} | |
return this.renderLoading(); | |
} | |
} | |
return connect(({user}) => ({user}))(AuthenticatedComponent); | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment