mirror of
https://github.com/dillonkearns/elm-pages-v3-beta.git
synced 2024-11-23 22:26:16 +03:00
Add a SetField helper.
This commit is contained in:
parent
e13f3def99
commit
eed054a7b7
@ -15,6 +15,7 @@ type Effect msg
|
||||
| Cmd (Cmd msg)
|
||||
| Batch (List (Effect msg))
|
||||
| GetStargazers (Result Http.Error Int -> msg)
|
||||
| SetField { formId : String, name : String, value : String }
|
||||
| FetchRouteData
|
||||
{ data : Maybe FormDecoder.FormData
|
||||
, toMsg : Result Http.Error Url -> msg
|
||||
@ -74,6 +75,9 @@ map fn effect =
|
||||
, toMsg = fetchInfo.toMsg >> fn
|
||||
}
|
||||
|
||||
SetField info ->
|
||||
SetField info
|
||||
|
||||
SubmitFetcher fetcher ->
|
||||
fetcher
|
||||
|> Pages.Fetcher.map fn
|
||||
@ -96,6 +100,7 @@ perform :
|
||||
-> Cmd msg
|
||||
, fromPageMsg : pageMsg -> msg
|
||||
, key : Browser.Navigation.Key
|
||||
, setField : { formId : String, name : String, value : String } -> Cmd msg
|
||||
}
|
||||
-> Effect pageMsg
|
||||
-> Cmd msg
|
||||
@ -107,6 +112,9 @@ perform ({ fromPageMsg, key } as helpers) effect =
|
||||
Cmd cmd ->
|
||||
Cmd.map fromPageMsg cmd
|
||||
|
||||
SetField info ->
|
||||
helpers.setField info
|
||||
|
||||
Batch list ->
|
||||
Cmd.batch (List.map (perform helpers) list)
|
||||
|
||||
|
@ -89,6 +89,33 @@ update eventObject pageFormState =
|
||||
pageFormState
|
||||
|
||||
|
||||
setField : { formId : String, name : String, value : String } -> PageFormState -> PageFormState
|
||||
setField info pageFormState =
|
||||
pageFormState
|
||||
|> Dict.update info.formId
|
||||
(\previousValue_ ->
|
||||
let
|
||||
previousValue : FormState
|
||||
previousValue =
|
||||
previousValue_
|
||||
|> Maybe.withDefault Dict.empty
|
||||
in
|
||||
previousValue
|
||||
|> Dict.update info.name
|
||||
(\previousFieldValue_ ->
|
||||
let
|
||||
previousFieldValue : FieldState
|
||||
previousFieldValue =
|
||||
previousFieldValue_
|
||||
|> Maybe.withDefault { value = "", status = NotVisited }
|
||||
in
|
||||
{ previousFieldValue | value = info.value }
|
||||
|> Just
|
||||
)
|
||||
|> Just
|
||||
)
|
||||
|
||||
|
||||
updateForm : FieldEvent -> FormState -> FormState
|
||||
updateForm fieldEvent formState =
|
||||
formState
|
||||
|
@ -303,6 +303,7 @@ type Msg userMsg pageData actionData sharedData errorPage
|
||||
= LinkClicked Browser.UrlRequest
|
||||
| UrlChanged Url
|
||||
| UserMsg (Pages.Msg.Msg userMsg)
|
||||
| SetField { formId : String, name : String, value : String }
|
||||
| UpdateCacheAndUrlNew Bool Url (Maybe userMsg) (Result Http.Error ( Url, ResponseSketch pageData actionData sharedData ))
|
||||
| FetcherComplete Int (Result Http.Error (Maybe userMsg))
|
||||
| FetcherStarted FormDecoder.FormData
|
||||
@ -381,6 +382,11 @@ update config appMsg model =
|
||||
, BrowserLoadUrl href
|
||||
)
|
||||
|
||||
SetField info ->
|
||||
( { model | pageFormState = Pages.Form.setField info model.pageFormState }
|
||||
, NoEffect
|
||||
)
|
||||
|
||||
UrlChanged url ->
|
||||
( { model
|
||||
-- update the URL in case query params or fragment changed
|
||||
@ -746,6 +752,7 @@ perform config model effect =
|
||||
startFetcher options model
|
||||
, fromPageMsg = Pages.Msg.UserMsg >> UserMsg
|
||||
, key = key
|
||||
, setField = \info -> Task.succeed (SetField info) |> Task.perform identity
|
||||
}
|
||||
|
||||
Nothing ->
|
||||
|
@ -114,6 +114,7 @@ type alias ProgramConfig userMsg userModel route pageData actionData sharedData
|
||||
, fromPageMsg : userMsg -> mappedMsg
|
||||
, runFetcher : Pages.Fetcher.Fetcher userMsg -> Cmd mappedMsg
|
||||
, key : Browser.Navigation.Key
|
||||
, setField : { formId : String, name : String, value : String } -> Cmd mappedMsg
|
||||
}
|
||||
-> effect
|
||||
-> Cmd mappedMsg
|
||||
|
Loading…
Reference in New Issue
Block a user