Created
September 16, 2019 16:17
-
-
Save yvsssantosh/3424a7ef218b431ea434aac0dff54839 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, { PureComponent } from 'react' | |
import { View, Text, Button, AsyncStorage } from 'react-native' | |
import { connect } from 'react-redux' | |
class LoginScreen extends PureComponent { | |
constructor (props) { | |
super(props) | |
this.attemptLogin = this.attemptLogin.bind(this) | |
} | |
attemptLogin () { | |
// Send request to server and validate login here | |
var token = 'somerandomtoken' | |
return token | |
} | |
render () { | |
return ( | |
<View style={{ flex: 1, backgroundColor: '#ffffff', alignItems: 'center', justifyContent: 'center' }}> | |
<Text children='Redux Login Example' /> | |
<Text children='Click on login to continue' /> | |
<Button color='#90100' title='Login' onPress={this.attemptLogin} /> | |
</View> | |
) | |
} | |
} | |
const mapStateToProps = (state, ownProps) => { | |
return {} | |
} | |
export const actionCreator = (action, payload = null) => ({ action, payload }) | |
const mapDispatchToProps = (dispatch, ownProps) => { | |
return { | |
authSuccess: (token) => { | |
AsyncStorage.multiSet([['token', token], ['authenticated', 1]]) | |
dispatch(actionCreator('LOGIN_SUCCESS')) | |
} | |
} | |
} | |
export const authStateReducer = (state = { authenticated: false, appStarted: false }, { type, payload }) => { | |
switch (type) { | |
case 'LOGIN_SUCCESS': | |
return { ...state, authenticated: true } | |
case 'LOGGOUT': | |
return { ...state, authenticated: false } | |
case 'APP_LOADED': | |
return { ...state, appStarted: true } | |
default: | |
return state | |
} | |
} | |
export default connect(mapStateToProps, mapDispatchToProps)(LoginScreen) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment