diff --git a/examples/smoothies/app/Effect.elm b/examples/smoothies/app/Effect.elm index 6383d1cb..f98e1acc 100644 --- a/examples/smoothies/app/Effect.elm +++ b/examples/smoothies/app/Effect.elm @@ -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) diff --git a/src/Pages/Form.elm b/src/Pages/Form.elm index 00d257a0..2b28eb81 100644 --- a/src/Pages/Form.elm +++ b/src/Pages/Form.elm @@ -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 diff --git a/src/Pages/Internal/Platform.elm b/src/Pages/Internal/Platform.elm index 2ace131e..6b565ee0 100644 --- a/src/Pages/Internal/Platform.elm +++ b/src/Pages/Internal/Platform.elm @@ -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 -> diff --git a/src/Pages/ProgramConfig.elm b/src/Pages/ProgramConfig.elm index dbdb1076..4143da62 100644 --- a/src/Pages/ProgramConfig.elm +++ b/src/Pages/ProgramConfig.elm @@ -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