-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ios.js
115 lines (98 loc) · 2.4 KB
/
index.ios.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/**
* Sample React Native App
* https://github.com/facebook/react-native
*/
'use strict';
var React = require('react-native');
var {
AppRegistry,
StyleSheet,
Text,
TextInput,
View,
ScrollView,
} = React;
var WORDNIK_URL_BASE = 'http://api.wordnik.com:80/v4/word.json/';
var SECRETS = require('./secrets.json')
var Dictionary = React.createClass({
getInitialState: function() {
return {
inputText: '',
resultJSX: <Text></Text>
};
},
updateText: function(newText) {
this.setState((state) => {
return {
inputText: newText
};
});
},
lookUp: function(text) {
this.setState((state) => {
return {
resultJSX: <Text>{'Loooking up "' + state.inputText + '"...'}</Text>
};
});
fetch(WORDNIK_URL_BASE + text + "/definitions?api_key=" + SECRETS.api_key)
.then((response) => {
this.setState((state) => {
var results = [];
for (let result of JSON.parse(response._bodyText)) {
results.push(
<View style={styles.container}>
<Text style={styles.definitionType}>{result.partOfSpeech}</Text>
<Text style={styles.definition}>{result.text}</Text>
<Text style={styles.definitionSource}>{result.attributionText}</Text>
</View>
);
}
return {
resultJSX:
<ScrollView>{results}</ScrollView>
};
});
})
.catch((error) => console.warn(error));
},
render: function() {
return (
<ScrollView style={[styles.container, styles['container--main']]}>
<TextInput
autoCapitalize="none"
style={styles.wordPrompt}
value={this.state.inputText}
onChange={(event) => this.updateText(event.nativeEvent.text)}
onEndEditing={() => this.lookUp(this.state.inputText)}
/>
{this.state.resultJSX}
</ScrollView>
);
},
});
var styles = StyleSheet.create({
container: {
marginTop: 20
},
'container--main': {
margin: 20,
marginTop: 40
},
wordPrompt: {
height: 40,
paddingLeft: 10,
borderColor: 'gray',
borderWidth: 1
},
definition: {
marginTop: 5,
marginBottom: 5
},
definitionType: {
fontStyle: 'italic'
},
definitionSource: {
color: 'gray'
}
});
AppRegistry.registerComponent('Dictionary', () => Dictionary);