Last active
August 6, 2019 15:54
-
-
Save dkblay/08700e9563e45e9144debbcaca0c22ee to your computer and use it in GitHub Desktop.
App
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from "react"; | |
import { getGames } from "./api/api"; | |
import Pagination from "./components/Pagination"; | |
class App extends Component { | |
size = 12; | |
state = { | |
games: [], | |
totoal: null | |
}; | |
componentDidMount() { | |
this.loadGames(); | |
} | |
loadGames = async (page = 1) => { | |
try { | |
const { games, total } = await getGames({ | |
size: this.size, | |
page | |
}); | |
this.setState({ games, total }); | |
} catch (e) { | |
console.error(e); | |
} | |
}; | |
renderCards() { | |
const { games } = this.state; | |
if (!games.length) { | |
return <div>No Items Found...</div>; | |
} | |
return games.map(({ id, color }) => ( | |
<div | |
key={id} | |
className="games__item" | |
style={{ backgroundColor: color }} | |
/> | |
)); | |
} | |
render() { | |
const { total } = this.state; | |
return ( | |
<div className="container"> | |
<div className="games">{this.renderCards()}</div> | |
{!!total && ( | |
<Pagination onClick={this.loadGames} total={total} size={this.size} /> | |
)} | |
</div> | |
); | |
} | |
} | |
export default App; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment