-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
406 lines (359 loc) · 10.7 KB
/
App.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
import React from 'react';
import { StyleSheet, Image, ActivityIndicator, View, AppRegistry, ScrollView , AsyncStorage , Alert} from 'react-native';
import { Button, Header, Card, Text, Rating, List, ListItem } from 'react-native-elements';
import { StackNavigator, TabNavigator } from 'react-navigation';
import RateScreen from './components/RateScreen';
import queryString from 'query-string';
/*
class Buttons extends React.Component {
render() {
const {fetchImage, like, email} = this.props;
return (
<View>
<Button buttonStyle={styles.button} backgroundColor='#6699ff' title="I want another!" onPress={fetchImage} />
<Button buttonStyle={styles.button} backgroundColor='#ff6699' title="I love it!" onPress={like} />
<Button buttonStyle={styles.button} backgroundColor='#99ff66' title="Share it!" onPress={email} />
</View>
);
}
}
Old CardBundle which takes entire screen
class CardBundle extends React.Component {
constructor(props){
super(props);
this.state = {imageUrl: '', loading: '' };
}
componentWillMount(){
this.handlePress();
}
handlePress = () => {
this.setState({ loading: true })
fetch('http://thecatapi.com/api/images/get')
.then(response => {
this.setState({ imageUrl: response.url, loading: false });
});
}
handleLike = () => {
console.log("I like " + this.state.imageUrl);
}
handleEmail = () => {
//Custom email feature
this.setState({ emailing: true }); // Mark beginning of load
const apiKey = 'key-ae1f6f860d25b28a2de63f76ad55758e';
const body = {
from: 'Something original <[email protected]>',
to: ['Sam <[email protected]>'],
subject: 'MOM! GET THE CAMERSA!',
html: `I found something really cool: <img src=${this.state.imageUrl} />`,
};
const options = {
method: 'POST',
headers: {
'content-type': 'x-www-form-urlencoded',
'Authorization': `api:${apiKey}`,
},
body: queryString.stringify(body),
};
fetch('https://api.mailgun.net/v3/sambalana.com/message', options).then((response) => {
return response.json();
}).then((json) => {
console.log(json);
console.log(options);
this.setState({ emailing: false });
})
console.log("Hey mom! I found something really cool " + this.state.imageUrl);
}
render() {
const title = this.props.title;
const { loading } = this.state;
return(
<View>
<StatusBarBackground />
{loading === true ? (
<ActivityIndicator />
) : (
<Image source={{ uri: this.state.imageUrl }} style={{ width: 300, height: 300 }}/>
)}
<Text style={styles.text}>{title}</Text>
<Buttons
fetchImage={this.handlePress}
like={this.handleLike}
email={this.handleEmail}
/>
</View>
);
}
}
*/
//NEW STUFFZ
class CardLeft extends React.Component {
constructor(props){
super(props);
this.state = {imageUrl: '', loading: ''};
}
componentWillMount(){
this.handlePress();
}
handlePress = () => {
this.setState({ loading: true })
fetch('http://thecatapi.com/api/images/get')
.then(response => {
this.setState({ imageUrl: response.url, loading: false });
});
}
render(){
const { marginRight, marginLeft } = this.props;
const { navigate } = this.props.navigation;
return(
<View>
{(this.state.loading == true ? (
<Card containerStyle={{marginRight: 7, marginLeft: 15}} >
<ActivityIndicator />
<Button
icon={{name: 'details'}}
backgroundColor='#03A9F4'
buttonStyle={{borderRadius: 0, marginLeft: 0, marginRight: 0, marginBottom: 0, marginTop: 15}}
title='RATE ME' />
</Card>
) : (
<Card containerStyle={{marginRight: 7, marginLeft: 15}}
image={{ uri: this.state.imageUrl}} >
<Button
icon={{name: 'details'}}
backgroundColor='#03A9F4'
buttonStyle={{borderRadius: 0, marginLeft: 0, marginRight: 0, marginBottom: 0}}
onPress={() => navigate("Rate", { url: this.state.imageUrl } )}
title='RATE ME' />
</Card>
)
)}
</View>
);
}
}
class CardRight extends React.Component {
constructor(props){
super(props);
this.state = {imageUrl: '', loading: ''};
}
componentWillMount(){
this.handlePress();
}
handlePress = () => {
this.setState({ loading: true })
fetch('http://thecatapi.com/api/images/get')
.then(response => {
this.setState({ imageUrl: response.url, loading: false });
});
}
render(){
const { marginRight, marginLeft } = this.props;
const { navigate } = this.props.navigation;
return(
<View>
{(this.state.loading == true ? (
<Card containerStyle={{marginRight: 15, marginLeft: 7}} >
<ActivityIndicator />
<Button
icon={{name: 'details'}}
backgroundColor='#03A9F4'
buttonStyle={{borderRadius: 0, marginLeft: 0, marginRight: 0, marginBottom: 0, marginTop: 15}}
title='RATE ME' />
</Card>
) : (
<Card containerStyle={{marginRight: 15, marginLeft: 7}}
image={{ uri: this.state.imageUrl}} >
<Button
icon={{name: 'details'}}
backgroundColor='#03A9F4'
buttonStyle={{borderRadius: 0, marginLeft: 0, marginRight: 0, marginBottom: 0}}
onPress={() => navigate("Rate", { url: this.state.imageUrl } )}
title='RATE ME' />
</Card>
)
)}
</View>
);
}
}
class CardStack extends React.Component {
constructor(props){
super(props);
}
render(){
return(
<View style={styles.lineSet}>
<CardLeft navigation={this.props.navigation} />
<CardRight navigation={this.props.navigation} />
</View>
);
}
}
class App extends React.Component {
static navigationOptions = {
title: 'View Cats',
}
clearData = () => {
Alert.alert(
'Confirm',
'Are you sure you want to clear ALL data? There will be no way to recover this data',
[
{text: 'Cancel', onPress: () => console.log('Cancel Pressed'), style: 'cancel'},
{text: 'Confirm', onPress: () => AsyncStorage.clear()},
],
{ cancelable: false }
)
}
render() {
return (
<View style={styles.container}>
{/*} <StatusBarBackground /> */}
<ScrollView style={{ width: 375 }}>
<Button title="Clear data" onPress={() =>
this.clearData()
} />
<CardStack navigation={this.props.navigation} />
<CardStack navigation={this.props.navigation} />
<CardStack navigation={this.props.navigation} />
<CardStack navigation={this.props.navigation} />
<CardStack navigation={this.props.navigation} />
</ScrollView>
</View>
);
}
}
class Rankings extends React.Component {
static navigationOptions = {
title: 'Rankings',
}
clearData = () => {
Alert.alert(
'Confirm',
'Are you sure you want to clear ALL data? There will be no way to recover this data',
[
{text: 'Cancel', onPress: () => console.log('Cancel Pressed'), style: 'cancel'},
{text: 'Confirm', onPress: () => AsyncStorage.clear()},
],
{ cancelable: false }
)
}
list = [];
refreshData = () => {
this.setState({ loading: true })
AsyncStorage.getItem("map").then((obj) =>{
if(obj != null){
//Exists
console.log("parsing")
console.log(JSON.parse(obj))
tmplist = Array.from(JSON.parse(obj));
this.list = tmplist.sort(function(a, b) {
return b[1] - a[1];
});
this.setState({ loading: false })
}else{
this.setState({ loading: false })
console.log("NULL")
}
})
}
constructor(props){
super(props);
this.state = { loading: true,}
AsyncStorage.getItem("map").then((obj) =>{
if(obj != null){
//Exists
console.log("parsing")
console.log(JSON.parse(obj))
tmplist = Array.from(JSON.parse(obj));
this.list = tmplist.sort(function(a, b) {
return b[1] - a[1];
});
this.setState({ loading: false })
}else{
this.setState({ loading: false })
console.log("NULL")
}
})
}
render() {
const { navigation } = this.props;
/*
AsyncStorage.getItem("map").then((map) => {
console.log("ASYNC START")
})
*/
if(this.state.loading){
return (<ActivityIndicator style={styles.container} />);
}else if(this.list.length == 0){
return(
<View style={styles.container}>
<View style={{ flex: 1, flexDirection: 'row', width: 375, alignItems: 'center', }}>
<Button title="Clear data" onPress={() => this.clearData() } />
<Button title="Refresh data" onPress={() => this.refreshData() } />
</View>
<Text>You have no voted for any cats!</Text>
<Text>Return to View Cats and choose your favorite!</Text>
</View>
);
}else{
return (
<View style={styles.container}>
<ScrollView style={{ width: 375 }}>
<View style={{ flex: 1, flexDirection: 'row', alignItems: 'center', }}>
<Button title="Clear data" onPress={() => this.clearData() } />
<Button title="Refresh data" onPress={() => this.refreshData() } />
</View>
<List containerStyle={{marginBottom: 20 }}>
{console.log(this.list)}
{
this.list.map((l, i) => (
<ListItem
roundAvatar
avatar={{ uri: l[0] }}
key={i}
onPress={() => navigation.navigate("Rate", { url: l[0] } )}
title={l[0]}
subtitle={l[1]}
/>
))
}
</List>
</ScrollView>
</View>
);
}
}
}
const StackRoutes = StackNavigator({
Home: { screen: App },
Rate: { screen: RateScreen},
});
const StackRanking = StackNavigator({
Rankings: { screen: Rankings }
});
const TabRoutes = TabNavigator({
CatView: { screen: StackRoutes },
Rankings: { screen: StackRanking },
});
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
text: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
button: {
marginTop: 15
},
lineSet: {
flex: 1,
flexDirection: 'row'
},
});
export default TabRoutes;