24
loading...
This website collects cookies to deliver better user experience
await fetch("https://segunda.tech/sobre")
https://pokeapi.co/api/v2/pokemon?limit=5
e o retorno será um json contendo o resultado abaixo.{
"count": 1118,
"next": "https://pokeapi.co/api/v2/pokemon?offset=5&limit=5",
"previous": null,
"results": [
{
"name": "bulbasaur",
"url": "https://pokeapi.co/api/v2/pokemon/1/"
},
{
"name": "ivysaur",
"url": "https://pokeapi.co/api/v2/pokemon/2/"
},
{
"name": "venusaur",
"url": "https://pokeapi.co/api/v2/pokemon/3/"
},
{
"name": "charmander",
"url": "https://pokeapi.co/api/v2/pokemon/4/"
},
{
"name": "charmeleon",
"url": "https://pokeapi.co/api/v2/pokemon/5/"
}
]
}
<!doctype html>
<html lang="pt-BR">
<head>
<meta charset="utf-8">
<title>Lista de Pokémons em HTML e JavaScript</title>
<meta name="author" content="Marcio Frayze David">
</head>
<body>
<p id="loading-message">
Carregando lista de nomes dos Pokémons, aguarde...
</p>
<ul id="pokemon-names-list">
</ul>
<script>
(async function() {
await fetch("https://pokeapi.co/api/v2/pokemon?limit=5")
.then(data => data.json())
.then(dataJson => dataJson.results)
.then(results => results.map(pokemon => pokemon.name))
.then(names => addNamesToDOM(names))
hideLoadingMessage()
})();
function addNamesToDOM(names) {
let pokemonNamesListElement = document.getElementById('pokemon-names-list')
names.forEach(name => addNameToDOM(pokemonNamesListElement, name))
}
function addNameToDOM(pokemonNamesListElement, name) {
let newListElement = document.createElement('li')
newListElement.innerHTML = name
pokemonNamesListElement.append(newListElement)
}
function hideLoadingMessage() {
document.getElementById('loading-message').style.visibility = 'hidden'
}
</script>
</body>
</html>
<!doctype html>
<html lang="pt-BR">
<head>
<meta charset="utf-8">
<title>Lista de Pokémons em HTML e Elm</title>
<meta name="author" content="Marcio Frayze David">
</head>
<body>
<main></main>
<script>
Elm.Main.init({ node: document.querySelector('main') })
</script>
</body>
</html>
module Main exposing (..)
import Browser
import Html exposing (..)
import Http
import Json.Decode exposing (Decoder)
-- MAIN
main =
Browser.element
{ init = init
, update = update
, subscriptions = subscriptions
, view = view
}
-- MODEL
type alias PokemonInfo = { name : String }
type Model
= Failure
| Loading
| Success (List PokemonInfo)
init : () -> (Model, Cmd Msg)
init _ =
(Loading, fetchPokemonNames)
-- UPDATE
type Msg
= FetchedPokemonNames (Result Http.Error (List PokemonInfo))
update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
case msg of
FetchedPokemonNames result ->
case result of
Ok pokemonsInfo ->
(Success pokemonsInfo, Cmd.none)
Err _ ->
(Failure, Cmd.none)
-- SUBSCRIPTIONS
subscriptions : Model -> Sub Msg
subscriptions model =
Sub.none
-- VIEW
view : Model -> Html Msg
view model =
case model of
Failure ->
text "Por alguma razão, não foi possível carregar a lista com nome dos Pokémons. 😧"
Loading ->
text "Carregando lista de nomes dos Pokémons, aguarde..."
Success pokemonsInfo ->
ul []
(List.map viewPokemonInfo pokemonsInfo)
viewPokemonInfo : PokemonInfo -> Html Msg
viewPokemonInfo pokemonInfo =
li [] [ text pokemonInfo.name ]
-- HTTP
fetchPokemonNames : Cmd Msg
fetchPokemonNames =
Http.get
{ url = "https://pokeapi.co/api/v2/pokemon?limit=5"
, expect = Http.expectJson FetchedPokemonNames decoder
}
pokemonInfoDecoder : Decoder PokemonInfo
pokemonInfoDecoder =
Json.Decode.map PokemonInfo
(Json.Decode.field "name" Json.Decode.string)
decoder : Decoder (List PokemonInfo)
decoder =
Json.Decode.field "results" (Json.Decode.list pokemonInfoDecoder)
type alias PokemonInfo = { name : String }
type Model
= Loading
| Failure
| Success (List PokemonInfo)
view : Model -> Html Msg
view model =
case model of
Failure ->
text "Por alguma razão, não foi possível carregar a lista com nome dos Pokémons. 😧"
Loading ->
text "Carregando lista de nomes dos Pokémons, aguarde..."
Success pokemonsInfo ->
ul []
(List.map viewPokemonInfo pokemonsInfo)
viewPokemonInfo : PokemonInfo -> Html Msg
viewPokemonInfo pokemonInfo =
li [] [ text pokemonInfo.name ]
Line 70, Column 3
This `case` does not have branches for all possibilities:
70|> case model of
71|> Failure ->
72|> text "Por alguma razão, não foi possível carregar a lista com nome dos Pokémons. 😧"
73|>
74|> Success pokemonsInfo ->
75|> ul []
76|> (List.map viewPokemonInfo pokemonsInfo)
Missing possibilities include:
Loading
I would have to crash if I saw one of those. Add branches for them!
Hint: If you want to write the code for each branch later, use `Debug.todo` as a
placeholder. Read <https://elm-lang.org/0.19.1/missing-patterns> for more
guidance on this workflow.
fetchPokemonNames : Cmd Msg
fetchPokemonNames =
Http.get
{ url = "https://pokeapi.co/api/v2/pokemon?limit=5"
, expect = Http.expectJson FetchedPokemonNames decoder
}
pokemonInfoDecoder : Decoder PokemonInfo
pokemonInfoDecoder =
Json.Decode.map PokemonInfo
(Json.Decode.field "name" Json.Decode.string)
decoder : Decoder (List PokemonInfo)
decoder =
Json.Decode.field "results" (Json.Decode.list pokemonInfoDecoder)
update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
case msg of
FetchedPokemonNames result ->
case result of
Ok pokemonsInfo ->
(Success pokemonsInfo, Cmd.none)
Err _ ->
(Failure, Cmd.none)