elm-pages-v3-beta/examples/pokedex/src/Page/PokedexNumber_.elm

122 lines
2.9 KiB
Elm
Raw Normal View History

2021-06-01 20:57:08 +03:00
module Page.PokedexNumber_ exposing (Data, Model, Msg, page)
import DataSource exposing (DataSource)
import DataSource.Http
import Head
import Head.Seo as Seo
import Html exposing (..)
import Html.Attributes exposing (src)
import OptimizedDecoder as Decode
import Page exposing (Page, PageWithState, StaticPayload)
import Pages.PageUrl exposing (PageUrl)
import Pages.Url
2021-06-02 00:52:41 +03:00
import Path
2021-06-01 20:57:08 +03:00
import Secrets
import Shared
import View exposing (View)
type alias Model =
()
type alias Msg =
Never
type alias RouteParams =
2021-06-03 21:58:35 +03:00
{ pokedexNumber : String }
2021-06-01 20:57:08 +03:00
page : Page RouteParams Data
page =
Page.prerenderedRouteWithFallback
{ head = head
, routes = routes
, data = data
, handleFallback =
2021-06-03 21:58:35 +03:00
\{ pokedexNumber } ->
2021-06-01 20:57:08 +03:00
let
asNumber : Int
asNumber =
2021-06-03 21:58:35 +03:00
String.toInt pokedexNumber |> Maybe.withDefault -1
2021-06-01 20:57:08 +03:00
in
DataSource.succeed
2021-06-02 06:58:04 +03:00
(asNumber > 0 && asNumber <= 150)
2021-06-01 20:57:08 +03:00
}
|> Page.buildNoState { view = view }
routes : DataSource (List RouteParams)
routes =
DataSource.succeed []
data : RouteParams -> DataSource Data
data routeParams =
2021-06-02 00:38:03 +03:00
DataSource.map2 Data
2021-06-02 01:00:04 +03:00
(DataSource.Http.get (Secrets.succeed "https://elm-pages-pokedex.netlify.app/.netlify/functions/time")
2021-06-02 00:38:03 +03:00
Decode.string
2021-06-01 20:57:08 +03:00
)
2021-06-03 21:58:35 +03:00
(DataSource.Http.get (Secrets.succeed ("https://pokeapi.co/api/v2/pokemon/" ++ routeParams.pokedexNumber))
2021-06-02 00:38:03 +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))))
)
)
type alias Pokemon =
{ name : String
, abilities : List String
}
2021-06-01 20:57:08 +03:00
head :
StaticPayload Data RouteParams
-> List Head.Tag
head static =
Seo.summary
{ canonicalUrlOverride = Nothing
, siteName = "elm-pages"
, image =
{ url = Pages.Url.external "TODO"
, alt = "elm-pages logo"
, dimensions = Nothing
, mimeType = Nothing
}
, description = "TODO"
, locale = Nothing
, title = "TODO title" -- metadata.title -- TODO
}
|> 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
}
view :
Maybe PageUrl
-> Shared.Model
-> StaticPayload Data RouteParams
-> View Msg
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 =
[ 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-06-03 21:58:35 +03:00
[ src <| "https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/" ++ static.routeParams.pokedexNumber ++ ".png"
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
]
}