Skip to content

Commit 17782f3

Browse files
committed
fixing parent render issue with pagination
1 parent 5cfd5a5 commit 17782f3

File tree

3 files changed

+30
-31
lines changed

3 files changed

+30
-31
lines changed

src/App.js

+1-12
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
import React, { useState } from 'react';
22
import Movies from './Movies';
33
import SearchBar from './SearchBar';
4-
import PageNum from './PageNum';
54

65
const App = () => {
76
const [searchTerm, setSearchTerm] = useState({});
8-
const [totalResults, setTotalResults] = useState(1);
97

108
return (
119
<div className='container-fluid'>
@@ -14,16 +12,7 @@ const App = () => {
1412
<SearchBar setSearchTerm={setSearchTerm} />
1513
</div>
1614
</div>
17-
<div className='row'>
18-
<div className='col-12' style={{ paddingLeft: '15%', paddingRight: '15%' }}>
19-
<Movies searchTerm={searchTerm} setTotalPages={setTotalResults} />
20-
</div>
21-
</div>
22-
<div className='row'>
23-
<div className='col-12' style={{ paddingLeft: '15%', paddingRight: '15%' }}>
24-
<PageNum setSearchTerm={setSearchTerm} searchTerm={searchTerm} totalResults={totalResults} />
25-
</div>
26-
</div>
15+
<Movies searchTerm={searchTerm} setSearchTerm={setSearchTerm} />
2716
</div>
2817
);
2918
};

src/Movies.js

+28-18
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React from 'react';
22
import MovieCard from './MovieCard';
3+
import PageNum from './PageNum';
34
import { useFetch } from './useFetch';
45

56
const Movies = (props) => {
@@ -27,26 +28,35 @@ const Movies = (props) => {
2728

2829
return (
2930
<React.Fragment>
30-
{isSearch === false ? (
31-
<div className='card mt-4'>
32-
<div className='card-body'>
33-
<div className='text-secondary pt-2 text-centerr'>Hit search to get some results</div>
34-
</div>
31+
<div className='row'>
32+
<div className='col-12' style={{ paddingLeft: '15%', paddingRight: '15%' }}>
33+
{isSearch === false ? (
34+
<div className='card mt-4'>
35+
<div className='card-body'>
36+
<div className='text-secondary pt-2 text-centerr'>Hit search to get some results</div>
37+
</div>
38+
</div>
39+
) : data['Response'] !== 'True' ? (
40+
<div className='card mt-4'>
41+
<div className='card-body'>
42+
<div className='text-secondary pt-2 text-centerr'>{data['Error']}</div>
43+
</div>
44+
</div>
45+
) : (
46+
<div className='row p-5'>
47+
{data['Search']?.map((dataItem) => {
48+
return <MovieCard key={dataItem['imdbID']} {...dataItem} />;
49+
})}
50+
</div>
51+
)}
3552
</div>
36-
) : data['Response'] !== 'True' ? (
37-
<div className='card mt-4'>
38-
<div className='card-body'>
39-
<div className='text-secondary pt-2 text-centerr'>{data['Error']}</div>
40-
</div>
41-
</div>
42-
) : (
43-
<div className='row p-5'>
44-
{props['setTotalPages'](data['totalResults']) &&
45-
data['Search']?.map((dataItem) => {
46-
return <MovieCard key={dataItem['imdbID']} {...dataItem} />;
47-
})}
53+
</div>
54+
55+
<div className='row'>
56+
<div className='col-12' style={{ paddingLeft: '15%', paddingRight: '15%' }}>
57+
<PageNum setSearchTerm={props['setSearchTerm']} searchTerm={props['searchTerm']} totalResults={data['totalResults']} />
4858
</div>
49-
)}
59+
</div>
5060
</React.Fragment>
5161
);
5262
};

src/PageNum.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react';
22

33
const PageNum = (props) => {
4-
const n = Math.ceil(props['totalResults'] / 10);
4+
const n = props['totalResults'] === undefined ? 1 : Math.ceil(props['totalResults'] / 10);
55

66
const handleUpdatePage = (e) => {
77
props.setSearchTerm({ ...props['searchTerm'], page: e.target.value });

0 commit comments

Comments
 (0)