elm-pages-v3-beta/examples/pokedex/app/Route/PokedexNumber_.elm

144 lines
3.6 KiB
Elm
Raw Normal View History

2022-05-03 21:51:32 +03:00
module Route.PokedexNumber_ exposing (ActionData, Data, Model, Msg, route)
2021-06-01 20:57:08 +03:00
import DataSource exposing (DataSource)
import DataSource.Http
2022-03-29 21:48:04 +03:00
import ErrorPage exposing (ErrorPage)
2021-06-01 20:57:08 +03:00
import Head
import Head.Seo as Seo
import Html exposing (..)
import Html.Attributes exposing (src)
import Json.Decode as Decode
import Pages.Msg
2021-06-01 20:57:08 +03:00
import Pages.PageUrl exposing (PageUrl)
import Pages.Url
2022-03-05 20:50:01 +03:00
import RouteBuilder exposing (StatefulRoute, StatelessRoute, StaticPayload)
import Server.Response as Response exposing (Response)
2021-06-01 20:57:08 +03:00
import Shared
import View exposing (View)
type alias Model =
2021-12-15 20:26:23 +03:00
{}
2021-06-01 20:57:08 +03:00
type alias Msg =
()
2021-06-01 20:57:08 +03:00
type alias RouteParams =
2022-03-03 19:52:33 +03:00
{ pokedexNumber : String }
2021-06-01 20:57:08 +03:00
2022-05-03 21:51:32 +03:00
route : StatelessRoute RouteParams Data ActionData
route =
2022-03-05 20:50:01 +03:00
RouteBuilder.preRenderWithFallback
2021-06-01 20:57:08 +03:00
{ head = head
, pages = pages
2021-06-01 20:57:08 +03:00
, data = data
}
2022-03-05 20:50:01 +03:00
|> RouteBuilder.buildNoState { view = view }
2021-06-01 20:57:08 +03:00
pages : DataSource (List RouteParams)
pages =
2021-06-01 20:57:08 +03:00
DataSource.succeed []
2022-03-29 21:48:04 +03:00
data : RouteParams -> DataSource (Response Data ErrorPage)
data { pokedexNumber } =
let
asNumber : Int
asNumber =
2022-05-17 00:40:13 +03:00
String.toInt pokedexNumber
|> Maybe.withDefault -1
in
if asNumber < 1 then
Response.errorPage (ErrorPage.InvalidPokedexNumber pokedexNumber)
2022-03-29 21:48:04 +03:00
|> DataSource.succeed
else if asNumber > 898 && asNumber < 10001 || asNumber > 10194 then
Response.errorPage (ErrorPage.MissingPokedexNumber asNumber)
|> DataSource.succeed
else
DataSource.map2 Data
2022-01-28 02:45:51 +03:00
(DataSource.Http.get "https://elm-pages-pokedex.netlify.app/.netlify/functions/time"
Decode.string
)
2022-01-28 02:45:51 +03:00
(DataSource.Http.get ("https://pokeapi.co/api/v2/pokemon/" ++ pokedexNumber)
(Decode.map2 Pokemon
(Decode.field "forms" (Decode.index 0 (Decode.field "name" Decode.string)))
(Decode.field "types" (Decode.list (Decode.field "type" (Decode.field "name" Decode.string))))
)
2021-06-02 00:38:03 +03:00
)
|> DataSource.map Response.render
2021-06-02 00:38:03 +03:00
type alias Pokemon =
{ name : String
, abilities : List String
}
2021-06-01 20:57:08 +03:00
head :
2022-05-03 21:51:32 +03:00
StaticPayload Data ActionData RouteParams
2021-06-01 20:57:08 +03:00
-> List Head.Tag
head static =
Seo.summary
{ canonicalUrlOverride = Nothing
2021-12-15 23:35:50 +03:00
, siteName = "elm-pages Pokedex"
2021-06-01 20:57:08 +03:00
, image =
2021-12-15 23:35:50 +03:00
{ url = static.routeParams |> pokemonImage |> Pages.Url.external
, alt = static.data.pokemon.name
2021-06-01 20:57:08 +03:00
, dimensions = Nothing
, mimeType = Nothing
}
, description = "TODO"
, locale = Nothing
2021-12-15 23:35:50 +03:00
, title =
"Pokedex #"
++ static.routeParams.pokedexNumber
++ " "
++ static.data.pokemon.name
2021-06-01 20:57:08 +03:00
}
|> Seo.website
type alias Data =
2021-06-02 00:38:03 +03:00
{ time : String
, pokemon : Pokemon
2021-06-01 20:57:08 +03:00
}
2022-05-03 21:51:32 +03:00
type alias ActionData =
{}
2021-06-01 20:57:08 +03:00
view :
Maybe PageUrl
-> Shared.Model
2022-05-03 21:51:32 +03:00
-> StaticPayload Data ActionData RouteParams
-> View (Pages.Msg.Msg Msg)
2021-06-01 20:57:08 +03:00
view maybeUrl sharedModel static =
2021-06-02 00:38:03 +03:00
{ title = static.data.pokemon.name
2021-06-01 20:57:08 +03:00
, body =
2022-04-06 01:53:12 +03:00
[ h1
[]
2021-06-02 00:38:03 +03:00
[ text static.data.pokemon.name
2021-06-01 20:57:08 +03:00
]
2021-06-02 00:38:03 +03:00
, text (static.data.pokemon.abilities |> String.join ", ")
2021-06-01 20:57:08 +03:00
, img
2021-12-15 23:35:50 +03:00
[ static.routeParams |> pokemonImage |> src
2021-06-01 20:57:08 +03:00
]
[]
2021-06-02 00:38:03 +03:00
, p []
[ text static.data.time
]
2021-06-01 20:57:08 +03:00
]
}
2021-12-15 23:35:50 +03:00
pokemonImage : RouteParams -> String
pokemonImage routeParams =
"https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/" ++ routeParams.pokedexNumber ++ ".png"