/** * Sample React Native App * https://github.com/facebook/react-native * @flow */ import React, { Component } from 'react'; import { AppRegistry, StyleSheet, Text, TextInput, TouchableOpacity, View, Platform, Alert } from 'react-native'; import Frisbee from 'frisbee'; import Spinner from 'react-native-loading-spinner-overlay'; import Form from 'react-native-form'; import CountryPicker from 'react-native-country-picker-modal'; const api = new Frisbee({ baseURI: 'http://localhost:3000', headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' } }); const MAX_LENGTH_CODE = 6; const MAX_LENGTH_NUMBER = 20; // if you want to customize the country picker const countryPickerCustomStyles = {}; // your brand's theme primary color const brandColor = '#744BAC'; const styles = StyleSheet.create({ countryPicker: { alignItems: 'center', justifyContent: 'center' }, container: { flex: 1 }, header: { textAlign: 'center', marginTop: 60, fontSize: 22, margin: 20, color: '#4A4A4A', }, form: { margin: 20 }, textInput: { padding: 0, margin: 0, flex: 1, fontSize: 20, color: brandColor }, button: { marginTop: 20, height: 50, backgroundColor: brandColor, alignItems: 'center', justifyContent: 'center', borderRadius: 5, }, buttonText: { color: '#fff', fontFamily: 'Helvetica', fontSize: 16, fontWeight: 'bold' }, wrongNumberText: { margin: 10, fontSize: 14, textAlign: 'center' }, disclaimerText: { marginTop: 30, fontSize: 12, color: 'grey' }, callingCodeView: { alignItems: 'center', justifyContent: 'center' }, callingCodeText: { fontSize: 20, color: brandColor, fontFamily: 'Helvetica', fontWeight: 'bold', paddingRight: 10 } }); export default class example extends Component { constructor(props) { super(props); this.state = { enterCode: false, spinner: false, country: { cca2: 'US', callingCode: '1' } }; } _getCode = () => { this.setState({ spinner: true }); setTimeout(async () => { try { const res = await api.post('/v1/verifications', { body: { ...this.refs.form.getValues(), ...this.state.country } }); if (res.err) throw res.err; this.setState({ spinner: false, enterCode: true, verification: res.body }); this.refs.form.refs.textInput.setNativeProps({ text: '' }); setTimeout(() => { Alert.alert('Sent!', "We've sent you a verification code", [{ text: 'OK', onPress: () => this.refs.form.refs.textInput.focus() }]); }, 100); } catch (err) { // this.setState({ spinner: false }); setTimeout(() => { Alert.alert('Oops!', err.message); }, 100); } }, 100); } _verifyCode = () => { this.setState({ spinner: true }); setTimeout(async () => { try { const res = await api.put('/v1/verifications', { body: { ...this.refs.form.getValues(), ...this.state.country } }); if (res.err) throw res.err; this.refs.form.refs.textInput.blur(); // this.setState({ spinner: false }); setTimeout(() => { Alert.alert('Success!', 'You have successfully verified your phone number'); }, 100); } catch (err) { // this.setState({ spinner: false }); setTimeout(() => { Alert.alert('Oops!', err.message); }, 100); } }, 100); } _onChangeText = (val) => { if (!this.state.enterCode) return; if (val.length === MAX_LENGTH_CODE) this._verifyCode(); } _tryAgain = () => { this.refs.form.refs.textInput.setNativeProps({ text: '' }) this.refs.form.refs.textInput.focus(); this.setState({ enterCode: false }); } _getSubmitAction = () => { this.state.enterCode ? this._verifyCode() : this._getCode(); } _changeCountry = (country) => { this.setState({ country }); this.refs.form.refs.textInput.focus(); } _renderFooter = () => { if (this.state.enterCode) return ( Enter the wrong number or need a new code? ); return ( By tapping "Send confirmation code" above, we will send you an SMS to confirm your phone number. Message & data rates may apply. ); } _renderCountryPicker = () => { if (this.state.enterCode) return ( ); return ( ); } _renderCallingCode = () => { if (this.state.enterCode) return ( ); return ( +{this.state.country.callingCode} ); } render() { let headerText = `What's your ${this.state.enterCode ? 'verification code' : 'phone number'}?` let buttonText = this.state.enterCode ? 'Verify confirmation code' : 'Send confirmation code'; let textStyle = this.state.enterCode ? { height: 50, textAlign: 'center', fontSize: 40, fontWeight: 'bold', fontFamily: 'Courier' } : {}; return ( {headerText}
{this._renderCountryPicker()} {this._renderCallingCode()} { buttonText } {this._renderFooter()}
); } } AppRegistry.registerComponent('example', () => example);