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)
|
2022-01-26 20:01:05 +03:00
|
|
|
import Json.Decode as Decode
|
2022-05-13 19:03:09 +03:00
|
|
|
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)
|
2022-01-18 07:56:37 +03:00
|
|
|
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 =
|
2022-01-06 01:09:37 +03:00
|
|
|
()
|
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
|
2022-03-05 21:35:17 +03:00
|
|
|
route =
|
2022-03-05 20:50:01 +03:00
|
|
|
RouteBuilder.preRenderWithFallback
|
2021-06-01 20:57:08 +03:00
|
|
|
{ head = head
|
2021-12-11 03:03:07 +03:00
|
|
|
, 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
|
|
|
|
|
|
|
|
2021-12-11 03:03:07 +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)
|
2021-12-23 22:14:46 +03:00
|
|
|
data { pokedexNumber } =
|
|
|
|
let
|
|
|
|
asNumber : Int
|
|
|
|
asNumber =
|
|
|
|
String.toInt pokedexNumber |> Maybe.withDefault -1
|
|
|
|
in
|
|
|
|
if asNumber < 1 then
|
2022-03-31 21:01:47 +03:00
|
|
|
Response.errorPage (ErrorPage.InvalidPokedexNumber pokedexNumber)
|
2022-03-29 21:48:04 +03:00
|
|
|
|> DataSource.succeed
|
2021-12-23 22:14:46 +03:00
|
|
|
|
|
|
|
else if asNumber > 898 && asNumber < 10001 || asNumber > 10194 then
|
2022-03-31 21:01:47 +03:00
|
|
|
Response.errorPage (ErrorPage.MissingPokedexNumber asNumber)
|
|
|
|
|> DataSource.succeed
|
2021-12-23 22:14:46 +03:00
|
|
|
|
|
|
|
else
|
|
|
|
DataSource.map2 Data
|
2022-01-28 02:45:51 +03:00
|
|
|
(DataSource.Http.get "https://elm-pages-pokedex.netlify.app/.netlify/functions/time"
|
2021-12-23 22:14:46 +03:00
|
|
|
Decode.string
|
|
|
|
)
|
2022-01-28 02:45:51 +03:00
|
|
|
(DataSource.Http.get ("https://pokeapi.co/api/v2/pokemon/" ++ pokedexNumber)
|
2021-12-23 22:14:46 +03:00
|
|
|
(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
|
|
|
)
|
2022-01-18 07:56:37 +03:00
|
|
|
|> DataSource.map Response.render
|
2021-12-23 22:14:46 +03:00
|
|
|
|
|
|
|
|
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
|
2022-05-13 19:03:09 +03:00
|
|
|
-> 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"
|