Last active
June 15, 2019 04:01
-
-
Save sanhuang/4526dbd00051695c80b7b36dd7230ef8 to your computer and use it in GitHub Desktop.
無法於登入畫面導回會員畫面時正常載出已登入狀態...
This file contains hidden or 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, { Component } from 'react'; | |
import { | |
Root, | |
Container, | |
Content, | |
Button, | |
Right, | |
Body, | |
Text, | |
H2, | |
List, | |
ListItem, | |
ActionSheet, | |
} from 'native-base'; | |
import Frisbee from 'frisbee'; | |
import ExStorage from 'ProjectName/src/components/ExStorage'; | |
import L from 'ProjectName/src/components/Layout'; | |
import styles from 'ProjectName/src/constants/styles'; | |
const api = new Frisbee({ | |
baseURI: 'http://somewhere/api', | |
headers: { | |
'Accept': 'application/json', | |
'Content-Type': 'application/json' | |
} | |
}); | |
export default class ProfileSreen extends Component { | |
static navigationOptions = ({ navigation }) => { | |
return { | |
headerRight: navigation.state.params.isSignIn ? ( | |
// removeItem before logout | |
<Button transparent | |
onPress={() => { | |
ExStorage.removeItem("member"); | |
navigation.navigate('index'); | |
}} | |
> | |
<Text>Logout</Text> | |
</Button> | |
) : null, | |
} | |
}; | |
constructor(props) { | |
super(props) | |
this.state = { | |
isLoading: true, | |
isSignIn: false, | |
member: undefined | |
} | |
this._getMemberStorage = this._getMemberStorage.bind(this); | |
} | |
componentDidMount() { | |
this._getMemberStorage(); | |
} | |
async _getMemberStorage(): Promise<void> { | |
try{ | |
// let member = null; | |
let member = await ExStorage.getItem("member"); | |
// console.log(member); | |
if ( member !== null ){ | |
await this.setState({ | |
member, | |
isSignIn: true | |
}); | |
}else{ | |
await this.promisedSetState({ | |
member: null, | |
isSignIn: false | |
}); | |
} | |
await this.promisedSetState({ | |
isLoading: false | |
}); | |
return true; | |
}catch(e) { | |
console.error(e); | |
} | |
} | |
promisedSetState = (newState) => { | |
return new Promise((resolve) => { | |
this.setState(newState, () => { | |
resolve() | |
}); | |
}); | |
} | |
protected _renderLogin() { | |
return ( | |
<ListItem> | |
<Button small rounded | |
onPress={() => { | |
this.props.navigation.navigate('Profile_Login'); | |
}} | |
> | |
<Text>Login</Text> | |
</Button> | |
</ListItem> | |
) | |
} | |
protected _renderMember() { | |
return ( | |
<ListItem> | |
<Body> | |
<H2>{this.state.member.cmt} {this.state.member.unit}{this.state.member.name},您好</H2> | |
<Text note>xxx</Text> | |
</Body> | |
</ListItem> | |
) | |
} | |
protected render() { | |
let infocoms = null; | |
if (this.state.isLoading ) { | |
return ( | |
<L.PageLoading /> | |
) | |
}else{ | |
if (this.state.isSignIn) { | |
infocoms = this._renderMember(); | |
} else { | |
infocoms = this._renderLogin(); | |
} | |
return ( | |
<Root> | |
<Container> | |
<Content> | |
<List> | |
{infocoms} | |
</List> | |
<Settings /> | |
</Content> | |
{<L.IFooter />} | |
</Container> | |
</Root> | |
); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment