|
| 1 | +var ReactNative = require('react-native'); |
| 2 | +var React = require('react'); |
| 3 | + |
| 4 | +var { |
| 5 | + AppRegistry, |
| 6 | + AsyncStorage, |
| 7 | + StyleSheet, |
| 8 | + Text, |
| 9 | + View, |
| 10 | + TouchableHighlight, |
| 11 | + AlertIOS, |
| 12 | +} = ReactNative; |
| 13 | + |
| 14 | +var demo_jwt = null; |
| 15 | + |
| 16 | +var AwesomeProject = React.createClass({ |
| 17 | + componentDidMount() { |
| 18 | + demo_jwt = this._loadInitialState().done(); |
| 19 | + }, |
| 20 | + |
| 21 | + async _loadInitialState() { |
| 22 | + var value = await AsyncStorage.getItem('demo_jwt'); |
| 23 | + if (value !== null){ |
| 24 | + this.setState({'demo_jwt': value}); |
| 25 | + return(value); |
| 26 | + } else { |
| 27 | + return(null); |
| 28 | + } |
| 29 | + }, |
| 30 | + |
| 31 | + _getQuote: function() { |
| 32 | + fetch("http://localhost:3001/api/random-quote", { |
| 33 | + method: "GET", |
| 34 | + }) |
| 35 | + .then((response) => response) |
| 36 | + .then((responseData) => { |
| 37 | + AlertIOS.alert( |
| 38 | + "Chuck Norris Quote:", |
| 39 | + responseData._bodyText) |
| 40 | + }) |
| 41 | + .done(); |
| 42 | + }, |
| 43 | + |
| 44 | + _getProtectedQuote: function() { |
| 45 | + fetch("http://localhost:3001/protected/random-quote", { |
| 46 | + method: "GET", |
| 47 | + Authorization: 'Bearer {jwt}' |
| 48 | + }) |
| 49 | + .then((response) => response) |
| 50 | + .then((responseData) => { |
| 51 | + AlertIOS.alert( |
| 52 | + "Chuck Norris Quote:", |
| 53 | + responseData._bodyText) |
| 54 | + }) |
| 55 | + .done(); |
| 56 | + }, |
| 57 | + |
| 58 | + _userSignup: function() { |
| 59 | + fetch("http://localhost:3001/users", { |
| 60 | + method: "POST", |
| 61 | + headers: { |
| 62 | + 'Accept': 'application/json', |
| 63 | + 'Content-Type': 'application/json' |
| 64 | + }, |
| 65 | + body: JSON.stringify({ |
| 66 | + username: "testuser3", |
| 67 | + password: "testpassword3", |
| 68 | + extra: "He's a test user" |
| 69 | + }) |
| 70 | + }) |
| 71 | + .then((response) => response) |
| 72 | + .then((responseData) => { |
| 73 | + AlertIOS.alert( |
| 74 | + "Successfully Signed Up! (Token):", |
| 75 | + responseData.id_token) |
| 76 | + }) |
| 77 | + .done(); |
| 78 | + AsyncStorage.setItem('demo_jwt', responseData.id_token); |
| 79 | + }, |
| 80 | + |
| 81 | + _userLogin: function() { |
| 82 | + fetch("http://localhost:3001/users", { |
| 83 | + method: "POST", |
| 84 | + headers: { |
| 85 | + 'Accept': 'application/json', |
| 86 | + 'Content-Type': 'application/json' |
| 87 | + }, |
| 88 | + body: JSON.stringify({ |
| 89 | + username: "testuser2", |
| 90 | + password: "testpassword2", |
| 91 | + }) |
| 92 | + }) |
| 93 | + .then((response) => response.json()) |
| 94 | + .then((responseData) => { |
| 95 | + AlertIOS.alert( |
| 96 | + "POST Response", |
| 97 | + "Response Body -> " + JSON.stringify(responseData.body) |
| 98 | + ) |
| 99 | + }) |
| 100 | + .done(); |
| 101 | + }, |
| 102 | + |
| 103 | + render: function() { |
| 104 | + return ( |
| 105 | + <View style={styles.container}> |
| 106 | + <View style={styles.row}> |
| 107 | + <Text style={styles.title}>Click below to get a Chuck Norris Quote!</Text> |
| 108 | + </View> |
| 109 | + |
| 110 | + <View style={styles.row}> |
| 111 | + <TouchableHighlight onPress={this._getQuote} style={styles.button}> |
| 112 | + <Text>Get a Chuck Norris Quote!</Text> |
| 113 | + </TouchableHighlight> |
| 114 | + </View> |
| 115 | + <View style={styles.row}> |
| 116 | + <Text style={styles.body}>Signup (or Login) to be even more satisfied with your Chuck Norris quotes!</Text> |
| 117 | + </View> |
| 118 | + <View style={styles.row}> |
| 119 | + <TouchableHighlight onPress={this._userSignup} style={styles.button}> |
| 120 | + <Text>Signup</Text> |
| 121 | + </TouchableHighlight> |
| 122 | + <TouchableHighlight onPress={this._userLogin} style={styles.button}> |
| 123 | + <Text>Login</Text> |
| 124 | + </TouchableHighlight> |
| 125 | + </View> |
| 126 | + <View style={styles.row}> |
| 127 | + <TouchableHighlight onPress={this._getProtectedQuote} style={styles.button}> |
| 128 | + <Text>Get More Satisfying Quotes!</Text> |
| 129 | + </TouchableHighlight> |
| 130 | + </View> |
| 131 | + </View> |
| 132 | + ); |
| 133 | + }, |
| 134 | +}); |
| 135 | + |
| 136 | +var styles = StyleSheet.create({ |
| 137 | + container: { |
| 138 | + padding:20, |
| 139 | + flex: 1, |
| 140 | + }, |
| 141 | + row: { |
| 142 | + flexDirection: 'row', |
| 143 | + margin: 10, |
| 144 | + flexWrap: 'wrap', |
| 145 | + justifyContent: 'center', |
| 146 | + }, |
| 147 | + button: { |
| 148 | + backgroundColor: '#eeeeee', |
| 149 | + padding: 10, |
| 150 | + marginRight: 5, |
| 151 | + marginLeft: 5, |
| 152 | + }, |
| 153 | + title: { |
| 154 | + justifyContent: 'center', |
| 155 | + fontSize: 16, |
| 156 | + fontWeight: 'bold', |
| 157 | + }, |
| 158 | + body: { |
| 159 | + justifyContent: 'center', |
| 160 | + fontSize: 12, |
| 161 | + flexWrap: 'wrap', |
| 162 | + flex: 1, |
| 163 | + } |
| 164 | +}); |
| 165 | + |
| 166 | +AppRegistry.registerComponent('AwesomeProject', () => AwesomeProject); |
| 167 | + |
0 commit comments