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

150 lines
3.7 KiB
Elm
Raw Normal View History

2021-12-23 02:28:51 +03:00
module Page.Greet exposing (Data, Model, Msg, page)
2022-01-21 05:00:38 +03:00
import Codec exposing (Codec)
2021-12-23 02:28:51 +03:00
import DataSource exposing (DataSource)
2022-01-21 05:00:38 +03:00
import DataSource.Http
import Dict exposing (Dict)
2021-12-23 02:28:51 +03:00
import Head
import Head.Seo as Seo
import Html
2021-12-23 08:29:26 +03:00
import Html.Attributes as Attr
2022-01-21 05:00:38 +03:00
import Json.Decode
import Json.Encode
import OptimizedDecoder
2021-12-23 02:28:51 +03:00
import Page exposing (Page, PageWithState, StaticPayload)
import Pages.PageUrl exposing (PageUrl)
2022-01-21 05:00:38 +03:00
import Pages.Secrets as Secrets
2021-12-23 02:28:51 +03:00
import Pages.Url
2022-01-21 05:00:38 +03:00
import Server.Request as Request exposing (Request)
import Server.Response exposing (Response)
import Server.SetCookie as SetCookie
import Session exposing (Session)
2021-12-23 02:28:51 +03:00
import Shared
import Time
2021-12-23 02:28:51 +03:00
import View exposing (View)
type alias Model =
{}
type alias Msg =
()
2021-12-23 02:28:51 +03:00
type alias RouteParams =
{}
page : Page RouteParams Data
page =
2021-12-24 00:34:19 +03:00
Page.serverRender
2021-12-23 02:28:51 +03:00
{ head = head
, data = data
}
|> Page.buildNoState { view = view }
2022-01-21 05:00:38 +03:00
keys =
{ userId = ( "userId", Codec.int )
}
withSession =
Session.withSession
{ name = "mysession"
, secrets =
Secrets.succeed
[ "secret4", "secret3", "secret2" ]
, sameSite = "lax" -- TODO custom type
}
(OptimizedDecoder.field "name" OptimizedDecoder.string)
data : RouteParams -> Request.Request (DataSource (Server.Response.Response Data))
data routeParams =
Request.oneOf
[ Request.map2 Data
(Request.expectQueryParam "name")
Request.requestTime
|> Request.map
(\requestData ->
requestData
|> Server.Response.render
|> Server.Response.withHeader
"x-greeting"
("hello there " ++ requestData.username ++ "!")
|> DataSource.succeed
)
, withSession
Request.requestTime
(\requestTime userIdResult ->
let
username =
userIdResult
|> Result.withDefault "TODO username"
in
( Session.noUpdates
, { username = username
, requestTime = requestTime
}
|> Server.Response.render
|> Server.Response.withHeader
"x-greeting"
("hello " ++ username ++ "!")
)
|> DataSource.succeed
)
]
2021-12-23 02:28:51 +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 =
{ username : String
, requestTime : Time.Posix
}
2021-12-23 02:28:51 +03:00
view :
Maybe PageUrl
-> Shared.Model
-> StaticPayload Data RouteParams
-> View Msg
view maybeUrl sharedModel static =
{ title = "Hello!"
2021-12-23 08:29:26 +03:00
, body =
[ Html.text <| "Hello " ++ static.data.username ++ "!"
, Html.text <| "Requested page at " ++ String.fromInt (Time.posixToMillis static.data.requestTime)
2021-12-23 08:29:26 +03:00
, Html.div []
[ Html.form
[ Attr.method "post"
, Attr.action "/api/logout"
]
[ Html.button
[ Attr.type_ "submit"
]
[ Html.text "Logout" ]
]
]
]
2021-12-23 02:28:51 +03:00
}