Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2019
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.37 KB | None | 0 0
  1. import React, {PureComponent} from 'react';
  2. import {View, Text, TouchableOpacity, TextInput, StatusBar, ImageBackground, AsyncStorage, Image, Keyboard} from 'react-native';
  3. import styles from './styles';
  4. import ProgressBar from '../../components/common/ProgressBar';
  5. import * as userActions from '../../actions/userActions';
  6. import { bindActionCreators } from 'redux';
  7. import {connect} from 'react-redux';
  8. import {isEmpty} from '../../utils/helper';
  9. import {loginBackground, icLogoVegaWhite, phone, icClearButton, nextPage} from '../../utils/icons'
  10. import SuccessNormal from './components/SuccessNormal';
  11. import Header from '../../components/Header';
  12. import ConfirmCode from '../../components/input/ConfirmCode';
  13. import CountDown from '../../components/buttons/CountDown';
  14. import Toast from '../../components/messages/Toast';
  15. import {DEFAULT_ERROR} from '../../constants/language'
  16. import ModalWrapper from 'react-native-modal-wrapper';
  17. import Success from './components/Success';
  18. import {converPhone} from '../../utils/helper';
  19. import Button from '../../components/buttons/Button';
  20. // import ModalScreen from '../ModalScreen';
  21. import ScreenParent from '../ScreenParent'
  22.  
  23. class OtpEmail extends ScreenParent {
  24. constructor(props) {
  25. super(props);
  26. this.state = {
  27. ...this.state,
  28. time: props.expiredIn,
  29. isLoading: true,
  30. username: '',
  31. password: '',
  32. repeatPassword: '',
  33. showModal: false,
  34. otp: 0,
  35. isError: false,
  36. resMessage: '',
  37. isWaiting: false,
  38. forgot: false,
  39. verifyPhone: false,
  40. btnText: 'HOÀN THÀNH'
  41. }
  42. }
  43.  
  44. componentWillMount() {
  45. super.componentWillMount();
  46. if(this.state.notConnected) {
  47. this.setState({
  48. isLoading: false
  49. })
  50. return;
  51. }
  52.  
  53. }
  54.  
  55. getFacebookOtp = async(phone) => {
  56. const data = await this.props.actions.getAccountKitUrl(phone);
  57. console.log("get top", data);
  58. if(data.status === 200) {
  59.  
  60. const form = data.data;
  61. this.setState({
  62. isLoading: false,
  63. isError: false,
  64. isWaiting: false,
  65. urlAccount: form.url,
  66. request_code: form.formData.request_code,
  67. phoneNumber: form.formData.phone_number
  68. })
  69. if(this._countDown) this._countDown._setTime(60);
  70. }else {
  71. this.setState({
  72. isLoading: false,
  73. isError: true,
  74. message: data.errors['phone'] ? data.errors['phone'] : data.message
  75. })
  76. }
  77. }
  78.  
  79. login() {
  80.  
  81. }
  82.  
  83. _next = () => {
  84. this._onFinishCheckingCode2(this.state.otp);
  85. }
  86.  
  87. _close() {
  88. this.props.navigator.dismissModal();
  89. }
  90.  
  91. _onFinishCheckingCode2 = (isValid, code) => {
  92. if(this.state.notConnected) {
  93. this.showNetWordError();
  94. return;
  95. }
  96. const {type, password} = this.props;
  97. Keyboard.dismiss();
  98. this.setState({
  99. isWaiting: true,
  100. otp: isValid
  101. }, async() => {
  102. var phone = this.props.phone || this.props.phoneNumber;
  103. const {urlAccount, request_code, phoneNumber} = this.state;
  104. const {fullname, phone, email, birthday, address, province, district, formData} = this.props;
  105. console.log("formData", formData);
  106. const res = await this.props.actions.updateUserInfo(fullname, phone, email, birthday, address, province, district, isValid, formData.url, formData.formData.phone_number, formData.formData.request_code );
  107. console.log("updateUserInfo otp", res);
  108. if(res.status === 200) {
  109. this.setState({
  110. isWaiting: false
  111. }, () => {
  112. this.setState({
  113. showModal: true
  114. })
  115. })
  116. }else {
  117. this.setState({
  118. isWaiting: false
  119. }, () => {
  120. if(res.errors) {
  121. const errors = res.errors;
  122. console.log(errors);
  123. Object.keys(errors).map((key) => {
  124. console.log(errors[key])
  125. this.toast.show(errors[key]);
  126. })
  127. // res.errors.forEach
  128. }else {
  129. this.toast.show(DEFAULT_ERROR)
  130. }
  131. });
  132. }
  133. // .then(res => {
  134. // console.log("updateUserInfo", res);
  135. // this.setState({
  136. // isLoading: false,
  137. // }, () => {
  138. // if(res.status === 200) {
  139.  
  140. // }
  141. //
  142. // })
  143. // }).catch(err => {
  144. // this.toast.show(DEFAULT_ERROR)
  145. // })
  146.  
  147. // const data = await this.props.actions.verifyAccountKitOtp(urlAccount, phoneNumber, request_code, isValid);
  148.  
  149. // if(data.status === 200) {
  150. // this._navigate(phone, data.data);
  151. // }else {
  152. // this.setState({
  153. // isLoading: false,
  154. // isWaiting: false,
  155. // }, () => {
  156. // this.toast.show(data.message)
  157. // })
  158. // }
  159. })
  160. }
  161.  
  162. _countDownCallBack = () => {
  163. this.setState({
  164. isWaiting: true
  165. }, () => {
  166. const phone = this.props.phone || this.props.phoneNumber;
  167. this.getFacebookOtp(phone);
  168. })
  169. }
  170.  
  171. _goHome = (data) => {
  172. this.props.actions.login(this.props.phone, data)
  173. .then(() => {
  174. //
  175. if(!isEmpty(this.props.user)) {
  176. this.setState({showModal: false}, () => {
  177. this.props.navigator.resetTo({
  178. screen: 'app.Home',
  179. navigatorStyle: {
  180. navBarTextColor: "#ffffff",
  181. navBarComponentAlignment: 'center', // center/fill
  182. navBarButtonColor: '#ffffff',
  183. navBarBackgroundColor: '#007ed2',
  184. statusBarHidden: false,
  185. navBarHidden: true,
  186. drawUnderNavBar: false,
  187. statusBarTextColorSchemeSingleScreen: 'light',
  188. },
  189. });
  190. })
  191. }
  192. }).catch(err => {
  193. // console.log("loginErr", err);
  194. if(err.data) {
  195. this.setState({
  196. isLoading: false
  197. }, () => {
  198. this.toast.show(err.data.message);
  199. })
  200. }else {
  201. this.setState({
  202. isLoading: false
  203. }, () => {
  204. this.toast.show(DEFAULT_ERROR);
  205. })
  206. }
  207. })
  208. }
  209.  
  210. _modalCallback = () => {
  211. this.setState({showModal: false}, () => {;
  212. this.props.navigator.resetTo({
  213. screen: 'app.Home',
  214. navigatorStyle: {
  215. navBarTextColor: "#ffffff",
  216. navBarComponentAlignment: 'center', // center/fill
  217. navBarButtonColor: '#ffffff',
  218. navBarBackgroundColor: '#007ed2',
  219. statusBarHidden: false,
  220. navBarHidden: true,
  221. drawUnderNavBar: false,
  222. statusBarTextColorSchemeSingleScreen: 'light',
  223. },
  224. });
  225. })
  226. }
  227.  
  228. renderModal(message, changePass, password) {
  229. return (
  230. <ModalWrapper
  231. style={{flex: 1, backgroundColor: 'transparent'}}
  232. visible={this.state.showModal}
  233. >
  234. <SuccessNormal callBack={this._modalCallback} message={'Cập nhật thông tin thành công!'} btnText={'VỀ TRANG CHỦ'}/>
  235. </ModalWrapper>
  236. )
  237. }
  238.  
  239. renderMessage(isError, message, phone, phoneNumber, time) {
  240. if(isError) {
  241. return (
  242. <View style={styles.errors}>
  243. <Text style={styles.error_message}>{message}</Text>
  244. </View>
  245. )
  246. }else {
  247. return (
  248. <View style={{flex: 1}}>
  249. <View style={styles.wrapTextOtp}>
  250. <Text style={styles.textOtp} numberOfLines={2}>Nhập mã OTP vừa được gửi về số điện thoại <Text style={{fontWeight: 'bold'}}>{phone ? converPhone(phone) : converPhone(phoneNumber)}</Text></Text>
  251. </View>
  252. <View style={[styles.block, {height: 132}]}>
  253. <View style={styles.blockItem}>
  254. <View style={{width: '100%', height: 60}}>
  255. <ConfirmCode
  256. ref="codeInputRef2"
  257. keyboardType="numeric"
  258. size={40}
  259. codeInputStyle={styles.confirmCode}
  260. initialCodeInputStyle={styles.confirmCode}
  261. codeLength={6}
  262. activeColor={'#ffffff'}
  263. inactiveColor={'#a3aeb8'}
  264. className={'border-circle'}
  265. autoFocus={true}
  266. codeInputStyle={{ fontWeight: '800' }}
  267. onFulfill={(isValid, code) => this._onFinishCheckingCode2(isValid, code)}
  268. />
  269. <View style={[styles.lineStyle]}></View>
  270. </View>
  271. <View style={styles.wrapResend}>
  272. <CountDown ref={c => this._countDown = c} time={time} callBack={() =>this._countDownCallBack()} title={'Không nhận được mã? Gửi lại sau'}/>
  273. </View>
  274. </View>
  275.  
  276. </View>
  277. <Toast
  278. ref={c => this.toast = c}
  279. style={{backgroundColor:'red'}}
  280. position='top'
  281. positionValue={200}
  282. fadeInDuration={100}
  283. fadeOutDuration={3000}
  284. opacity={0.8}
  285. textStyle={{color:'white'}}
  286. />
  287. <View style={{backgroundColor: '#ffffff', position: 'absolute', bottom: 0, width: '100%'}}>
  288. <Button style={{justifyContent: 'center', width: '100%', marginTop: 10, marginBottom: 10}} title={this.state.btnText} callBack={this._next} />
  289. </View>
  290. </View>
  291. )
  292. }
  293. }
  294.  
  295. render() {
  296. const {title, phone, phoneNumber, message, changePass, password} = this.props;
  297. const {isError, isLoading, resMessage, time} = this.state;
  298. return (
  299. <View style={[styles.container, {paddingTop: 0, backgroundColor: "#f0eff5"}]}>
  300. <Header title={'Xác nhận số điện thoại'} navigator={this.props.navigator} />
  301. <View style={{flex: 1}}>
  302. {this.renderMessage(isError, resMessage, phone, phoneNumber, time)}
  303. <ProgressBar isShow={this.state.isWaiting} />
  304. </View>
  305. {this.renderModal(message, changePass, password)}
  306. </View>
  307. )
  308. }
  309. }
  310.  
  311. function mapStateToProps(state, ownProps) {
  312. return {
  313. user: state.userReducers.user,
  314. }
  315. }
  316.  
  317. function mapDispatchToProps(dispatch) {
  318. return {
  319. actions: bindActionCreators(userActions, dispatch),
  320. };
  321. }
  322.  
  323. export default connect(mapStateToProps, mapDispatchToProps)(OtpEmail);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement