@@ -17,13 +17,19 @@ export default class Movie extends Component {
1717 } ;
1818
1919 componentDidMount ( ) {
20- this . setState ( { loading : true } ) ;
21-
22- // Movie Data
23- const endpoint = `${ API_URL } movie/${
24- this . props . match . params . movieId
25- } ?api_key=${ API_KEY } &language=en-US`;
26- this . fetchItems ( endpoint ) ;
20+ if ( localStorage . getItem ( `${ this . props . match . paramas . movieId } ` ) ) {
21+ const state = JSON . parse (
22+ localStorage . getItem ( `${ this . props . match . paramas . movieId } ` )
23+ ) ;
24+ this . setState ( { ...state } ) ;
25+ } else {
26+ this . setState ( { loading : true } ) ;
27+ // Movie Data
28+ const endpoint = `${ API_URL } movie/${
29+ this . props . match . params . movieId
30+ } ?api_key=${ API_KEY } &language=en-US`;
31+ this . fetchItems ( endpoint ) ;
32+ }
2733 }
2834
2935 fetchItems = endpoint => {
@@ -45,7 +51,15 @@ export default class Movie extends Component {
4551 const directors = res . crew . filter (
4652 member => member . job === "Director"
4753 ) ;
48- this . setState ( { actors : res . cast , directors, loading : false } ) ;
54+ this . setState (
55+ { actors : res . cast , directors, loading : false } ,
56+ ( ) => {
57+ localStorage . setItem (
58+ `${ this . props . match . params . movieId } ` ,
59+ JSON . stringify ( this . state )
60+ ) ;
61+ }
62+ ) ;
4963 } ) ;
5064 } ) ;
5165 }
0 commit comments