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

126 lines
3.4 KiB
Elm
Raw Normal View History

2021-12-23 02:28:51 +03:00
module Page.Greet exposing (Data, Model, Msg, page)
import CookieParser
import DataSource exposing (DataSource)
import DataSource.ServerRequest as ServerRequest exposing (ServerRequest)
import Dict
import Head
import Head.Seo as Seo
import Html
2021-12-23 08:29:26 +03:00
import Html.Attributes as Attr
2021-12-23 02:28:51 +03:00
import Page exposing (Page, PageWithState, StaticPayload)
import PageServerResponse exposing (PageServerResponse)
import Pages.PageUrl exposing (PageUrl)
import Pages.Url
import ServerResponse
import Shared
import View exposing (View)
type alias Model =
{}
type alias Msg =
Never
type alias RouteParams =
{}
page : Page RouteParams Data
page =
Page.serverless
{ head = head
, data = data
}
|> Page.buildNoState { view = view }
data : ServerRequest.IsAvailable -> RouteParams -> DataSource (PageServerResponse Data)
data serverRequestKey routeParams =
let
serverReq : ServerRequest (Maybe String)
serverReq =
ServerRequest.init identity
|> ServerRequest.optionalHeader "cookie"
in
serverReq
|> ServerRequest.toDataSource serverRequestKey
|> DataSource.andThen
(\cookies ->
--DataSource.succeed (PageServerResponse.ServerResponse (ServerResponse.temporaryRedirect "/"))
--DataSource.succeed (PageServerResponse.ServerResponse (ServerResponse.stringBody (foo |> Maybe.withDefault "NOT FOUND")))
case
cookies
|> Maybe.withDefault ""
|> CookieParser.parse
|> Dict.get "username"
of
Just username ->
DataSource.succeed
(PageServerResponse.RenderPage { username = username })
--(PageServerResponse.ServerResponse
-- (ServerResponse.stringBody
-- "Alright, here's the secret! This is all running with elm-pages serverless :D"
-- )
--)
Nothing ->
DataSource.succeed
(PageServerResponse.ServerResponse (ServerResponse.temporaryRedirect "/login"))
)
--DataSource.succeed (PageServerResponse.RenderPage {})
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 }
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.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
}