ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Home ,TV, Search, Detail Container
    React 2019. 11. 14. 09:51

    api.js 로부터 데이터 가져오기

     

     

    • HomeContainer.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}
          />
        );
      }
    }
    
    • TVContainer.js
    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}
          />
        );
      }
    }
    
    • SearchContainer.js
    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}
          />
        );
      }
    }
    

    'React' 카테고리의 다른 글

    Detail Container 2  (0) 2019.11.18
    Detail Container 1  (0) 2019.11.14
    #5 Container Presenter Pattern  (0) 2019.11.13
    api 파트 2  (0) 2019.11.11
    api 파트 1  (0) 2019.11.11

    댓글

Designed by Tistory.