api.js 로부터 데이터 가져오기
import React from "react";
import HomePresenter from "./HomePresenter";
import { moviesApi } from "../../api";
export default class extends React.Component {
state = {
nowPlaying: null,
upcoming: null,
popular: null,
error: null,
loading: true
};
async componentDidMount() {
try {
const {
data: { results: nowPlaying }
} = await moviesApi.nowPlaying();
const {
data: { results: upcoming }
} = await moviesApi.upcoming();
const {
data: { results: popular }
} = await moviesApi.popular;
this.setState({
nowPlaying,
upcoming,
popular
});
} catch (error) {
this.setState({
error: "Can't find movies information."
});
} finally {
this.setState({
loading: false
});
}
}
render() {
const { nowPlaying, upcoming, popular, error, loading } = this.state;
return (
<HomePresenter
nowPlaying={nowPlaying}
upcoming={upcoming}
popular={popular}
error={error}
loading={loading}
/>
);
}
}
import React from "react";
import TVPresenter from "./TVPresenter";
import { tvApi } from "../../api";
export default class extends React.Component {
state = {
topRated: null,
popular: null,
airingToday: null,
loading: true,
error: null
};
async componentDidMount() {
try {
const {
data: { resulat: topRated }
} = await tvApi.topRated();
const {
data: { resulat: popular }
} = await tvApi.popular();
const {
data: { resulat: airingToday }
} = await tvApi.airingToday();
this.setState({ topRated, popular, airingToday });
} catch (error) {
this.setState({
error: "Can't find TV information."
});
} finally {
this.setState({ loading: false });
}
}
render() {
const { topRated, popular, airingToday, loading, error } = this.state;
return (
<TVPresenter
topRated={topRated}
popular={popular}
airingToday={airingToday}
loading={loading}
error={error}
/>
);
}
}
import React from "react";
import SearchPresenter from "./SearchPresenter";
import { moviesApi, tvApi } from "../../api";
export default class extends React.Component {
state = {
movieResults: null,
tvResults: null,
searchTerm: "",
loading: false,
error: null
};
handleSubmit = () => {
const { searchTerm } = this.state;
if (searchTerm !== "") {
this.searchByTerm();
}
};
searchByTerm = async () => {
const { searchTerm } = this.state;
this.setState({ loading: true });
try {
const {
data: { results: movieResults }
} = await moviesApi.search(searchTerm);
const {
data: { results: tvResults }
} = await tvApi.search(searchTerm);
this.setState({
movieResults,
tvResults
});
} catch {
this.setState({ error: "Can't find results." });
} finally {
this.setState({ loading: false });
}
};
render() {
const { movieResults, tvResults, searchTerm, loading, error } = this.state;
return (
<SearchPresenter
movieResults={movieResults}
tvResults={tvResults}
searchTerm={searchTerm}
loading={loading}
error={error}
handleSubmit={this.handleSubmit}
/>
);
}
}