2022-03-05 21:35:17 +03:00
|
|
|
module Route.Greet exposing (Data, Model, Msg, route)
|
2021-12-23 02:28:51 +03:00
|
|
|
|
|
|
|
import DataSource exposing (DataSource)
|
2022-01-21 05:00:38 +03:00
|
|
|
import Dict exposing (Dict)
|
2021-12-23 02:28:51 +03:00
|
|
|
import Head
|
|
|
|
import Head.Seo as Seo
|
2022-01-23 00:47:51 +03:00
|
|
|
import Html exposing (Html)
|
2021-12-23 08:29:26 +03:00
|
|
|
import Html.Attributes as Attr
|
2022-01-23 01:40:04 +03:00
|
|
|
import MySession
|
2021-12-23 02:28:51 +03:00
|
|
|
import Pages.PageUrl exposing (PageUrl)
|
|
|
|
import Pages.Url
|
2022-03-05 20:50:01 +03:00
|
|
|
import RouteBuilder exposing (StatelessRoute, StaticPayload)
|
2022-03-08 19:57:46 +03:00
|
|
|
import Server.Request as Request exposing (Parser)
|
2022-01-21 05:00:38 +03:00
|
|
|
import Server.Response exposing (Response)
|
2022-01-28 20:27:43 +03:00
|
|
|
import Server.Session as Session
|
2021-12-23 02:28:51 +03:00
|
|
|
import Shared
|
2021-12-24 00:34:54 +03:00
|
|
|
import Time
|
2021-12-23 02:28:51 +03:00
|
|
|
import View exposing (View)
|
|
|
|
|
|
|
|
|
|
|
|
type alias Model =
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
|
type alias Msg =
|
2022-01-06 01:09:37 +03:00
|
|
|
()
|
2021-12-23 02:28:51 +03:00
|
|
|
|
|
|
|
|
|
|
|
type alias RouteParams =
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
2022-03-05 21:35:17 +03:00
|
|
|
route : StatelessRoute RouteParams Data
|
|
|
|
route =
|
2022-03-05 20:50:01 +03:00
|
|
|
RouteBuilder.serverRender
|
2021-12-23 02:28:51 +03:00
|
|
|
{ head = head
|
|
|
|
, data = data
|
|
|
|
}
|
2022-03-05 20:50:01 +03:00
|
|
|
|> RouteBuilder.buildNoState { view = view }
|
2021-12-23 02:28:51 +03:00
|
|
|
|
|
|
|
|
2022-03-08 19:57:46 +03:00
|
|
|
data : RouteParams -> Request.Parser (DataSource (Server.Response.Response Data))
|
2021-12-27 08:56:33 +03:00
|
|
|
data routeParams =
|
2021-12-31 22:14:23 +03:00
|
|
|
Request.oneOf
|
2022-01-23 00:47:51 +03:00
|
|
|
[ Request.map2 (\a b -> Data a b Nothing)
|
2022-01-21 08:34:33 +03:00
|
|
|
(Request.expectQueryParam "name")
|
|
|
|
Request.requestTime
|
|
|
|
|> Request.map
|
|
|
|
(\requestData ->
|
|
|
|
requestData
|
|
|
|
|> Server.Response.render
|
|
|
|
|> Server.Response.withHeader
|
|
|
|
"x-greeting"
|
|
|
|
("hello there " ++ requestData.username ++ "!")
|
|
|
|
|> DataSource.succeed
|
|
|
|
)
|
2022-02-23 21:41:04 +03:00
|
|
|
, Request.requestTime
|
|
|
|
|> MySession.expectSessionOrRedirect
|
|
|
|
(\requestTime session ->
|
|
|
|
let
|
|
|
|
username : String
|
|
|
|
username =
|
|
|
|
session
|
|
|
|
|> Session.get "name"
|
|
|
|
|> Maybe.withDefault "NONAME"
|
|
|
|
|
|
|
|
flashMessage : Maybe String
|
|
|
|
flashMessage =
|
|
|
|
session
|
|
|
|
|> Session.get "message"
|
|
|
|
in
|
|
|
|
( session
|
|
|
|
, { username = username
|
|
|
|
, requestTime = requestTime
|
|
|
|
, flashMessage = flashMessage
|
|
|
|
}
|
|
|
|
|> Server.Response.render
|
|
|
|
|> Server.Response.withHeader
|
|
|
|
"x-greeting"
|
|
|
|
("hello " ++ username ++ "!")
|
|
|
|
)
|
|
|
|
|> DataSource.succeed
|
2022-01-24 00:01:04 +03:00
|
|
|
)
|
2021-12-27 08:56:33 +03:00
|
|
|
]
|
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 =
|
2021-12-24 00:34:54 +03:00
|
|
|
{ username : String
|
|
|
|
, requestTime : Time.Posix
|
2022-01-23 00:47:51 +03:00
|
|
|
, flashMessage : Maybe String
|
2021-12-24 00:34:54 +03:00
|
|
|
}
|
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 =
|
2022-01-23 00:47:51 +03:00
|
|
|
[ static.data.flashMessage
|
|
|
|
|> Maybe.map (\message -> flashView (Ok message))
|
|
|
|
|> Maybe.withDefault (Html.p [] [ Html.text "No flash" ])
|
|
|
|
, Html.text <| "Hello " ++ static.data.username ++ "!"
|
2021-12-24 00:34:54 +03:00
|
|
|
, 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
|
|
|
}
|
2022-01-23 00:47:51 +03:00
|
|
|
|
|
|
|
|
|
|
|
flashView : Result String String -> Html msg
|
|
|
|
flashView message =
|
|
|
|
Html.p
|
|
|
|
[ Attr.style "background-color" "rgb(163 251 163)"
|
|
|
|
]
|
|
|
|
[ Html.text <|
|
|
|
|
case message of
|
|
|
|
Ok okMessage ->
|
|
|
|
okMessage
|
|
|
|
|
|
|
|
Err error ->
|
|
|
|
"Something went wrong: " ++ error
|
|
|
|
]
|