diff --git a/src/index.js b/src/index.js index 0abd52f..0ee2dad 100644 --- a/src/index.js +++ b/src/index.js @@ -10,13 +10,28 @@ nunjucks.configure("views", { app.use(express.static("public")); -app.get("/", function(request, response) { - console.log("Someone is requesting the page /"); - response.render("index.html"); +app.get("/", async function(req, res) { + const page = parseInt(req.query.page) || 0; //req.query returns a string ! + console.log(`Someone is requesting the page ${page}`); + const limit = 20; + const offset = page * limit; + const url = `https://pokeapi.co/api/v2/pokemon/?offset=${offset}&limit=${limit}`; + const pokemonList = await axios.get(url); + const maxNumOfPokemons = pokemonList.data.count; + const maxNumOfPages = Math.ceil(maxNumOfPokemons / limit); + const promises = pokemonList.data.results.map(pokemon => + axios.get(pokemon.url) + ); + const pokemon = await Promise.all(promises); + res.render("index.html", { + pokemons: pokemon, + page, + maxNumOfPages + }); }); app.get("/pokemon/:id", async (req, res) => { - console.log("Someone is request a pokemon"); + console.log("Someone is requesting a pokemon"); const id = req.params.id; const pokemon = await getPokemon(id); const moves = []; diff --git a/views/index.html b/views/index.html index f73bebf..4cb5c84 100644 --- a/views/index.html +++ b/views/index.html @@ -11,6 +11,24 @@

Welcome to My Pokédex

+ +
    + {%for pokemon in pokemons%} + +

    {{pokemon.data.name}}

    + +
    +

    {%for type in pokemon.data.types%} {{type.type.name}} {%endfor%}

    +
    + {%endfor%} + + {% if (page-1) >= 0%} + Previous + {%endif%} + {%if (page+1) < maxNumOfPages%} + Next + {%endif%} +
\ No newline at end of file diff --git a/views/pokemon.html b/views/pokemon.html index 88abb57..964a371 100644 --- a/views/pokemon.html +++ b/views/pokemon.html @@ -29,7 +29,7 @@

{{pokemon.id}} {{pokemon.name}}

{%for move in moves%}

move name: {{move.data.name}}

type: {{move.data.type.name}}

-

power : {{move.data.power}}

+

power : {{move.data.power or '100'}}

damage class: {{move.data.damage_class.name}}

accuracy: {{move.data.accuracy or '100'}} %

power point: {{move.data.pp}}