-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMoviesScreen.js
131 lines (130 loc) · 3.37 KB
/
MoviesScreen.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
import React, { Component } from 'react';
import { Picker, View, ActivityIndicator, Button }
from 'react-native';
export default class MoviesScreen extends Component {
componentDidMount() {
fetch('https://www.fullstackoasis.com/rnb/movies.php')
.then((response) => response.json())
.then((responseJson) => {
this.handleMoviesResponse(responseJson.movies);
})
.catch((error) => {
// TODO FIXME
console.error(error);
});
}
setMovieState(movies) {
this.setState({
isLoading: false,
dataSource: movies,
});
}
handleMoviesResponse(movies, delay) {
if (delay && delay > 0) {
const timer = setTimeout(function () {
this.setMovieState(movies);
}.bind(this), delay);
} else {
this.setMovieState(movies);
}
}
setNearbyTheatersState(theaters) {
this.setState({
isLoading: false,
nearbyTheaters: theaters,
});
}
handleNearbyTheatersResponse(theaters, delay) {
if (delay && delay > 0) {
const timer = setTimeout(function () {
this.setNearbyTheatersState(theaters);
}.bind(this), delay);
} else {
this.setNearbyTheatersState(theaters);
}
}
handleClick() {
fetch('https://www.fullstackoasis.com/rnb/theaters.php')
.then((response) => response.json())
.then((responseJson) => {
// TODO FIXME handle timeout / delay
this.handleNearbyTheatersResponse(responseJson);
})
.catch((error) => {
// TODO FIXME replace the red screen with something informative.
console.error(error);
});
}
handleShowTheaterClick() {
const { navigate } = this.props.navigation;
var index = 0; // set default if nothing is picked
if (this.state.theaterIndex) {
index = this.state.theaterIndex;
}
let selectedTheater = this.state.nearbyTheaters[index];
navigate('Maps', {
latLng: {
latitude: selectedTheater.lat,
longitude: selectedTheater.lng
},
name: selectedTheater.name,
address: selectedTheater.address1,
phone: selectedTheater.phone
});
}
render() {
if (this.state && !this.state.isLoading) {
let items = [];
var length = this.state.dataSource.length;
for (var i = 0; i < length; i++) {
var item = this.state.dataSource[i];
items.push(<Picker.Item label={item.title}
value={item.title} key={item.id} />);
}
var theatersPicker = null;
var goButton = null;
if (this.state.nearbyTheaters) {
let theaters = this.state.nearbyTheaters.map(
function (t, i) {
return <Picker.Item label={t.name}
value={t.name} key={i} />;
}
);
theatersPicker = <Picker selectedValue={this.state.theater}
onValueChange={(itemValue, itemIndex) =>
this.setState({
theater: itemValue,
theaterIndex: itemIndex
})
}>
{theaters}
</Picker>;
goButton = <Button title="Go"
onPress={this.handleShowTheaterClick.bind(this)}>
</Button>;
}
return <View>
<Picker selectedValue={this.state.movie}
onValueChange={(itemValue, itemIndex) =>
this.setState({ movie: itemValue })
}>
{items}
</Picker>
<Button onPress={this.handleClick.bind(this)}
title="Find This Movie Near Me"></Button>
{theatersPicker}
{goButton}
</View>;
} else {
return (<View style={
[ { flex: 1, justifyContent: 'center' },
{ flexDirection: 'row',
justifyContent: 'space-around',
padding: 10 }
]
}>
<ActivityIndicator size="large" color="#0000ff" />
</View>);
}
}
}