2022-05-18 02:07:07 +03:00
|
|
|
module Route.Search exposing (ActionData, Data, Model, Msg, route)
|
|
|
|
|
|
|
|
import DataSource exposing (DataSource)
|
|
|
|
import Effect exposing (Effect)
|
|
|
|
import ErrorPage exposing (ErrorPage)
|
2022-07-09 04:00:30 +03:00
|
|
|
import Form
|
2022-07-09 03:56:52 +03:00
|
|
|
import Form.Field as Field
|
2022-07-09 03:58:41 +03:00
|
|
|
import Form.FieldView
|
2022-07-09 03:51:26 +03:00
|
|
|
import Form.Validation as Validation
|
2022-05-18 02:07:07 +03:00
|
|
|
import Head
|
|
|
|
import Head.Seo as Seo
|
|
|
|
import Html exposing (Html)
|
|
|
|
import Html.Attributes as Attr
|
|
|
|
import Pages.Msg
|
|
|
|
import Pages.PageUrl exposing (PageUrl)
|
|
|
|
import Pages.Url
|
|
|
|
import Path exposing (Path)
|
|
|
|
import RouteBuilder exposing (StatefulRoute, StatelessRoute, StaticPayload)
|
|
|
|
import Server.Request as Request
|
|
|
|
import Server.Response as Response exposing (Response)
|
|
|
|
import Shared
|
|
|
|
import View exposing (View)
|
|
|
|
|
|
|
|
|
|
|
|
type alias Model =
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
|
type Msg
|
|
|
|
= NoOp
|
|
|
|
|
|
|
|
|
|
|
|
type alias RouteParams =
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
|
route : StatefulRoute RouteParams Data ActionData Model Msg
|
|
|
|
route =
|
|
|
|
RouteBuilder.serverRender
|
|
|
|
{ head = head
|
|
|
|
, data = data
|
|
|
|
, action = action
|
|
|
|
}
|
|
|
|
|> RouteBuilder.buildWithLocalState
|
|
|
|
{ view = view
|
|
|
|
, update = update
|
|
|
|
, subscriptions = subscriptions
|
|
|
|
, init = init
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
init :
|
|
|
|
Maybe PageUrl
|
|
|
|
-> Shared.Model
|
|
|
|
-> StaticPayload Data ActionData RouteParams
|
|
|
|
-> ( Model, Effect Msg )
|
|
|
|
init maybePageUrl sharedModel static =
|
|
|
|
( {}, Effect.none )
|
|
|
|
|
|
|
|
|
|
|
|
update :
|
|
|
|
PageUrl
|
|
|
|
-> Shared.Model
|
|
|
|
-> StaticPayload Data ActionData RouteParams
|
|
|
|
-> Msg
|
|
|
|
-> Model
|
|
|
|
-> ( Model, Effect Msg )
|
|
|
|
update pageUrl sharedModel static msg model =
|
|
|
|
case msg of
|
|
|
|
NoOp ->
|
|
|
|
( model, Effect.none )
|
|
|
|
|
|
|
|
|
|
|
|
subscriptions : Maybe PageUrl -> RouteParams -> Path -> Shared.Model -> Model -> Sub Msg
|
|
|
|
subscriptions maybePageUrl routeParams path sharedModel model =
|
|
|
|
Sub.none
|
|
|
|
|
|
|
|
|
|
|
|
type alias SearchResults =
|
|
|
|
{ query : String
|
|
|
|
, results : List String
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type alias Data =
|
|
|
|
{ results : Maybe SearchResults
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type alias ActionData =
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
|
data : RouteParams -> Request.Parser (DataSource (Response Data ErrorPage))
|
|
|
|
data routeParams =
|
|
|
|
Request.oneOf
|
2022-07-09 18:03:02 +03:00
|
|
|
[ Request.formDataWithoutServerValidation [ form ]
|
2022-07-05 19:57:02 +03:00
|
|
|
|> Request.map
|
|
|
|
(\formResult ->
|
|
|
|
DataSource.succeed
|
|
|
|
(Response.render
|
|
|
|
{ results =
|
|
|
|
formResult
|
|
|
|
|> Result.map
|
|
|
|
(\query ->
|
|
|
|
Just
|
|
|
|
{ query = query
|
|
|
|
, results = [ "Hello" ]
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|> Result.withDefault Nothing
|
|
|
|
}
|
2022-05-18 02:07:07 +03:00
|
|
|
)
|
2022-07-05 19:57:02 +03:00
|
|
|
)
|
2022-05-18 02:07:07 +03:00
|
|
|
, Request.succeed (DataSource.succeed (Response.render { results = Nothing }))
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2022-07-05 19:57:02 +03:00
|
|
|
form : Form.HtmlForm String String data msg
|
|
|
|
form =
|
|
|
|
Form.init
|
|
|
|
(\query ->
|
|
|
|
Validation.succeed identity
|
2022-07-09 00:29:30 +03:00
|
|
|
|> Validation.andMap query
|
2022-07-05 19:57:02 +03:00
|
|
|
)
|
|
|
|
(\info query ->
|
2022-07-08 21:09:52 +03:00
|
|
|
[ query |> fieldView info "Query"
|
|
|
|
, Html.button [] [ Html.text "Search" ]
|
|
|
|
]
|
2022-07-05 19:57:02 +03:00
|
|
|
)
|
|
|
|
|> Form.field "q" (Field.text |> Field.required "Required")
|
|
|
|
|
|
|
|
|
|
|
|
fieldView :
|
|
|
|
Form.Context String data
|
|
|
|
-> String
|
2022-07-09 04:01:54 +03:00
|
|
|
-> Form.ViewField String parsed Form.FieldView.Input
|
2022-07-05 19:57:02 +03:00
|
|
|
-> Html msg
|
|
|
|
fieldView formState label field =
|
|
|
|
Html.div []
|
|
|
|
[ Html.label []
|
|
|
|
[ Html.text (label ++ " ")
|
2022-07-09 04:01:54 +03:00
|
|
|
, field |> Form.FieldView.input []
|
2022-07-05 19:57:02 +03:00
|
|
|
]
|
|
|
|
, errorsForField formState field
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
errorsForField : Form.Context String data -> Form.ViewField String parsed kind -> Html msg
|
|
|
|
errorsForField formState field =
|
|
|
|
(if True then
|
|
|
|
field.errors
|
|
|
|
|> List.map (\error -> Html.li [] [ Html.text error ])
|
|
|
|
|
|
|
|
else
|
|
|
|
[]
|
|
|
|
)
|
|
|
|
|> Html.ul [ Attr.style "color" "red" ]
|
|
|
|
|
|
|
|
|
2022-05-18 02:07:07 +03:00
|
|
|
action : RouteParams -> Request.Parser (DataSource (Response ActionData ErrorPage))
|
|
|
|
action routeParams =
|
|
|
|
Request.skip "No action."
|
|
|
|
|
|
|
|
|
|
|
|
head :
|
|
|
|
StaticPayload Data ActionData 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
|
|
|
|
|
|
|
|
|
|
|
|
view :
|
|
|
|
Maybe PageUrl
|
|
|
|
-> Shared.Model
|
|
|
|
-> Model
|
|
|
|
-> StaticPayload Data ActionData RouteParams
|
|
|
|
-> View (Pages.Msg.Msg Msg)
|
|
|
|
view maybeUrl sharedModel model static =
|
|
|
|
{ title = "Search"
|
|
|
|
, body =
|
|
|
|
[ Html.h2 [] [ Html.text "Search" ]
|
2022-07-08 20:57:22 +03:00
|
|
|
, form
|
|
|
|
|> Form.toDynamicTransition "test1"
|
|
|
|
|> Form.withGetMethod
|
2022-07-08 21:09:52 +03:00
|
|
|
|> Form.renderHtml [] static ()
|
2022-05-18 02:07:07 +03:00
|
|
|
, static.data.results
|
|
|
|
|> Maybe.map resultsView
|
|
|
|
|> Maybe.withDefault (Html.div [] [])
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
resultsView : SearchResults -> Html msg
|
|
|
|
resultsView results =
|
|
|
|
Html.div []
|
|
|
|
[ Html.h2 [] [ Html.text <| "Results matching " ++ results.query ]
|
|
|
|
]
|