elm-pages-v3-beta/examples/pokedex/app/Route/FormEvent.elm

173 lines
3.9 KiB
Elm
Raw Normal View History

2022-05-03 21:51:32 +03:00
module Route.FormEvent exposing (ActionData, Data, Model, Msg, route)
2022-01-25 02:50:25 +03:00
import Browser.Navigation
import DataSource exposing (DataSource)
2022-03-29 01:11:07 +03:00
import Effect exposing (Effect)
import FormDecoder exposing (FormData)
2022-01-25 02:50:25 +03:00
import Head
import Head.Seo as Seo
import Html exposing (Html)
import Html.Attributes as Attr
import Pages.Msg
2022-01-25 02:50:25 +03:00
import Pages.PageUrl exposing (PageUrl)
import Pages.Url
import Path exposing (Path)
2022-03-05 20:50:01 +03:00
import RouteBuilder exposing (StatefulRoute, StaticPayload)
2022-01-25 02:50:25 +03:00
import Shared
import View exposing (View)
type alias Model =
{ formAsString : Maybe String
}
type Msg
= OnSubmit FormData
2022-01-25 02:50:25 +03:00
type alias RouteParams =
{}
2022-05-03 21:51:32 +03:00
route : StatefulRoute RouteParams Data ActionData Model Msg
route =
2022-03-05 20:50:01 +03:00
RouteBuilder.single
2022-01-25 02:50:25 +03:00
{ head = head
, data = data
}
2022-03-05 20:50:01 +03:00
|> RouteBuilder.buildWithLocalState
2022-01-25 02:50:25 +03:00
{ view = view
, update = update
, subscriptions = subscriptions
, init = init
}
init :
Maybe PageUrl
-> Shared.Model
2022-05-03 21:51:32 +03:00
-> StaticPayload Data ActionData RouteParams
2022-03-29 01:11:07 +03:00
-> ( Model, Effect Msg )
2022-01-25 02:50:25 +03:00
init maybePageUrl sharedModel static =
2022-03-29 01:11:07 +03:00
( { formAsString = Nothing }, Effect.none )
2022-01-25 02:50:25 +03:00
update :
PageUrl
-> Shared.Model
2022-05-03 21:51:32 +03:00
-> StaticPayload Data ActionData RouteParams
2022-01-25 02:50:25 +03:00
-> Msg
-> Model
2022-03-29 01:11:07 +03:00
-> ( Model, Effect Msg )
update pageUrl sharedModel static msg model =
2022-01-25 02:50:25 +03:00
case msg of
OnSubmit formAsString ->
2022-03-29 01:11:07 +03:00
( { model | formAsString = Just (toString formAsString) }, Effect.none )
2022-01-25 02:50:25 +03:00
toString : FormData -> String
2022-01-25 02:50:25 +03:00
toString formAsString =
formAsString.fields
2022-01-25 02:50:25 +03:00
|> List.map (\( key, value ) -> key ++ "=" ++ value)
|> String.join "\n"
subscriptions : Maybe PageUrl -> RouteParams -> Path -> Shared.Model -> Model -> Sub Msg
subscriptions maybePageUrl routeParams path sharedModel model =
Sub.none
type alias Data =
{}
2022-05-03 21:51:32 +03:00
type alias ActionData =
{}
2022-01-25 02:50:25 +03:00
data : DataSource Data
data =
DataSource.succeed {}
head :
2022-05-03 21:51:32 +03:00
StaticPayload Data ActionData RouteParams
2022-01-25 02:50:25 +03:00
-> List Head.Tag
head static =
Seo.summary
{ canonicalUrlOverride = Nothing
, siteName = "elm-pages"
, image =
{ url = Pages.Url.external ""
, alt = ""
, dimensions = Nothing
, mimeType = Nothing
}
, description = ""
, locale = Nothing
, title = "Test case for form event decoder"
}
|> Seo.website
view :
Maybe PageUrl
-> Shared.Model
-> Model
2022-05-03 21:51:32 +03:00
-> StaticPayload Data ActionData RouteParams
-> View (Pages.Msg.Msg Msg)
2022-01-25 02:50:25 +03:00
view maybeUrl sharedModel model static =
{ title = "Placeholder"
, body =
[ Html.p []
[ (case model.formAsString of
Nothing ->
"No submission received."
Just string ->
string
)
|> Html.text
]
, exampleForm
]
}
exampleForm : Html (Pages.Msg.Msg Msg)
2022-01-25 02:50:25 +03:00
exampleForm =
Html.form
[ FormDecoder.formDataOnSubmit
]
[ Html.div []
[ Html.label []
[ Html.text "First"
, Html.input
[ Attr.name "first"
, Attr.type_ "text"
, Attr.value "my-first-name"
]
[]
]
]
, Html.div []
[ Html.label []
[ Html.text "Last"
, Html.input
[ Attr.name "last"
, Attr.type_ "text"
, Attr.value "my-last-name"
]
[]
]
]
, Html.button
[ Attr.type_ "submit"
, Attr.name "my-button"
, Attr.value "hello-from-button"
]
[ Html.text "Submit" ]
]
|> Html.map (OnSubmit >> Pages.Msg.UserMsg)