Add a SetField helper.

This commit is contained in:
Dillon Kearns 2022-06-03 17:20:18 -07:00
parent e13f3def99
commit eed054a7b7
4 changed files with 43 additions and 0 deletions

View File

@ -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)

View File

@ -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

View File

@ -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 ->

View File

@ -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