-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
101 lines (90 loc) · 2.72 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
import React from "react";
import { Dimensions } from "react-native";
// import { SplashScreen } from "expo";
import { MoviesScreen } from "./screens/movies.js";
import { InitialScreen } from "./screens/initial.js";
export default class App extends React.Component {
constructor(props) {
super(props);
let { height, width } = Dimensions.get("window");
this.state = {
movieGenre: "",
animating: false,
movies: [],
deviceWidth: width,
screen: "initialScreen"
};
this.posters = [
require(`./assets/posters/0.jpg`),
require(`./assets/posters/3.jpg`),
require(`./assets/posters/4.jpg`),
require(`./assets/posters/5.jpg`),
require(`./assets/posters/6.jpg`),
require(`./assets/posters/7.jpg`),
require(`./assets/posters/8.jpg`),
require(`./assets/posters/10.jpg`),
require(`./assets/posters/13.jpg`),
require(`./assets/posters/15.jpg`),
require(`./assets/posters/16.jpg`),
require(`./assets/posters/17.jpg`),
require(`./assets/posters/18.jpg`),
require(`./assets/posters/19.jpg`),
require(`./assets/posters/20.jpg`)
];
let posternumber = Math.floor(Math.random() * this.posters.length);
this.poster = this.posters[posternumber];
}
onButtonClick = () => {
this.setState({ animating: true });
this.getMoviesFromApi();
};
onPress = () => {
this.setState({ screen: "initialScreen" });
};
onValueChange = itemValue => {
this.setState({ movieGenre: itemValue });
};
async getMoviesFromApi() {
try {
let page = Math.floor(Math.random() * 15);
if (page == 0) {
page = 1;
}
let url = `https://api.themoviedb.org/3/discover/movie?with_genres=${
this.state.movieGenre
}&page=${page}&include_adult=false&api_key=8df1ff267409edc4e0275609debe6fde`;
console.log(url);
let response = await fetch(url);
let responseJson = await response.json();
let movies = responseJson.results.filter(
result => !(result.poster_path == null)
);
this.setState({
movies: movies,
animating: false,
screen: "movieScreen"
});
} catch (error) {
console.error(error);
}
}
render() {
// SplashScreen.preventAutoHide();
// SplashScreen.hide();
return this.state.screen == "initialScreen" ? (
<InitialScreen
movieGenre={this.state.movieGenre}
animating={this.state.animating}
poster={this.poster}
onButtonClick={this.onButtonClick}
onValueChange={this.onValueChange}
/>
) : (
<MoviesScreen
movies={this.state.movies}
deviceWidth={this.state.deviceWidth}
onPress={this.onPress}
/>
);
}
}