diff --git a/examples/end-to-end/app/Route/DarkMode.elm b/examples/end-to-end/app/Route/DarkMode.elm index 6f047790..0e7d32fe 100644 --- a/examples/end-to-end/app/Route/DarkMode.elm +++ b/examples/end-to-end/app/Route/DarkMode.elm @@ -211,12 +211,14 @@ view app shared model = ) ] [ form - --|> Form.toDynamicFetcher - |> Pages.Form.renderStyledHtml "dark-mode" + |> Pages.Form.renderStyledHtml [] --(.formResponse >> Just) + (Pages.Form.options "dark-mode" + |> Pages.Form.withInput app.data.isDarkMode + |> Pages.Form.withParallel + ) app - app.data.isDarkMode , Html.text <| "Current mode: " ++ (if app.data.isDarkMode then diff --git a/examples/end-to-end/app/Route/Fetcher.elm b/examples/end-to-end/app/Route/Fetcher.elm index d459956d..ab5858e9 100644 --- a/examples/end-to-end/app/Route/Fetcher.elm +++ b/examples/end-to-end/app/Route/Fetcher.elm @@ -238,22 +238,22 @@ view app sharedModel model = [ Html.p [] [ Html.text <| String.fromInt model.itemIndex ] , form - -- TODO - --|> Form.withOnSubmit (\_ -> AddItemSubmitted) - |> Pages.Form.renderStyledHtml ("add-item-" ++ String.fromInt model.itemIndex) + |> Pages.Form.renderStyledHtml [] + (Pages.Form.options ("add-item-" ++ String.fromInt model.itemIndex) + |> Pages.Form.withOnSubmit (\_ -> AddItemSubmitted) + ) --(\_ -> Nothing) app - () , Html.div [] [ deleteForm - -- TODO - --|> Form.toDynamicFetcher - |> Pages.Form.renderStyledHtml "delete-all" + |> Pages.Form.renderStyledHtml [] + (Pages.Form.options "delete-all" + |> Pages.Form.withParallel + ) --(\_ -> Nothing) app - () ] , optimisticItems |> List.map diff --git a/examples/end-to-end/app/Route/Form.elm b/examples/end-to-end/app/Route/Form.elm index 2bc6bb07..9bdc56b0 100644 --- a/examples/end-to-end/app/Route/Form.elm +++ b/examples/end-to-end/app/Route/Form.elm @@ -247,15 +247,17 @@ view app shared = [] [ Html.text <| "Edit profile " ++ user.first ++ " " ++ user.last ] , form - |> Pages.Form.renderHtml "user-form" + |> Pages.Form.renderHtml [ Attr.style "display" "flex" , Attr.style "flex-direction" "column" , Attr.style "gap" "20px" ] + (Pages.Form.options "user-form" + |> Pages.Form.withInput defaultUser + ) -- TODO --(.formResponse >> Just) app - defaultUser ] |> List.map Html.Styled.fromUnstyled } diff --git a/examples/end-to-end/app/Route/GetForm.elm b/examples/end-to-end/app/Route/GetForm.elm index 6d5d4dd2..ffce7bc2 100644 --- a/examples/end-to-end/app/Route/GetForm.elm +++ b/examples/end-to-end/app/Route/GetForm.elm @@ -126,14 +126,16 @@ view app shared = , body = [ form |> Form.withGetMethod - |> Pages.Form.renderHtml "user-form" + |> Pages.Form.renderHtml [ Attr.style "display" "flex" , Attr.style "flex-direction" "column" , Attr.style "gap" "20px" ] + (Pages.Form.options "user-form" + |> Pages.Form.withInput app.data.filters + ) --(\_ -> Nothing) app - app.data.filters , Html.h2 [] [ Html.text <| "Current page: " ++ String.fromInt app.data.filters.page ] diff --git a/examples/end-to-end/app/Route/Login.elm b/examples/end-to-end/app/Route/Login.elm index 2d9e1ad3..c33c3053 100644 --- a/examples/end-to-end/app/Route/Login.elm +++ b/examples/end-to-end/app/Route/Login.elm @@ -220,12 +220,12 @@ view app shared = ) ] , form - |> Pages.Form.renderStyledHtml "form" + |> Pages.Form.renderStyledHtml [] + (Pages.Form.options "form") -- TODO --(.errors >> Just) app - () ] } diff --git a/src/Pages/Form.elm b/src/Pages/Form.elm index 4d3aba3b..062948a3 100644 --- a/src/Pages/Form.elm +++ b/src/Pages/Form.elm @@ -1,4 +1,4 @@ -module Pages.Form exposing (renderHtml, renderStyledHtml) +module Pages.Form exposing (Options, options, renderHtml, renderStyledHtml, withInput, withOnSubmit, withParallel) import Dict exposing (Dict) import Form @@ -10,11 +10,52 @@ import Pages.Transition import PagesMsg exposing (PagesMsg) +type alias Options error parsed input msg = + --{ serverResponse : Maybe (Form.ServerResponse error) } + --path : Path + { id : String + , input : input + , parallel : Bool + , onSubmit : Maybe (Form.Validated error parsed -> msg) + } + + +options : String -> Options error parsed () msg +options id = + { id = id + , input = () + , parallel = False + , onSubmit = Nothing + } + + +withParallel : Options error parsed input msg -> Options error parsed input msg +withParallel options_ = + { options_ | parallel = True } + + +withInput : input -> Options error parsed () msg -> Options error parsed input msg +withInput input options_ = + { id = options_.id + , input = input + , parallel = options_.parallel + , onSubmit = options_.onSubmit + } + + +withOnSubmit : (Form.Validated error parsed -> msg) -> Options error parsed input previousMsg -> Options error parsed input msg +withOnSubmit onSubmit options_ = + { id = options_.id + , input = options_.input + , parallel = options_.parallel + , onSubmit = Just onSubmit + } + + renderHtml : - String - -> List (Html.Attribute (PagesMsg userMsg)) + List (Html.Attribute (PagesMsg userMsg)) + -> Options error parsed input userMsg -> - --{ submitting : Bool, serverResponse : Maybe (Form.ServerResponse error), state : Form.Model } { --path : Path --, url : Maybe PageUrl --, action : Maybe action @@ -23,18 +64,17 @@ renderHtml : , transition : Maybe Pages.Transition.Transition , fetchers : Dict String (Pages.Transition.FetcherState (Maybe action)) } - -> input -> Form.Form error { combine : Validation error parsed named constraints, view : Form.Context error input -> List (Html.Html (PagesMsg userMsg)) } parsed input (PagesMsg userMsg) -> Html.Html (PagesMsg userMsg) -renderHtml formId attrs app input form_ = +renderHtml attrs options_ app form_ = form_ |> Form.renderHtml - formId + options_.id attrs { state = app.pageFormState , serverResponse = Nothing -- TODO , submitting = - (case app.fetchers |> Dict.get formId of + (case app.fetchers |> Dict.get options_.id of Just { status } -> case status of Pages.Transition.FetcherComplete _ -> @@ -51,10 +91,10 @@ renderHtml formId attrs app input form_ = ) || (case app.transition of Just (Pages.Transition.Submitting formData) -> - formData.id == Just formId + formData.id == Just options_.id Just (Pages.Transition.LoadAfterSubmit submitData _ _) -> - submitData.id == Just formId + submitData.id == Just options_.id Just (Pages.Transition.Loading _ _) -> False @@ -69,33 +109,32 @@ renderHtml formId attrs app input form_ = case parsed of Form.Valid _ -> Pages.Internal.Msg.Submit - { useFetcher = False -- TODO + { useFetcher = options_.parallel , action = action , fields = fields - , msg = Nothing -- TODO - , id = formId + , msg = options_.onSubmit |> Maybe.map (\onSubmit -> onSubmit parsed) + , id = options_.id , valid = True } Form.Invalid _ _ -> Pages.Internal.Msg.Submit - { useFetcher = False -- TODO + { useFetcher = options_.parallel , action = action , fields = fields - , msg = Nothing -- TODO - , id = formId + , msg = options_.onSubmit |> Maybe.map (\onSubmit -> onSubmit parsed) + , id = options_.id , valid = False } ) } - input + options_.input renderStyledHtml : - String - -> List (Html.Styled.Attribute (PagesMsg userMsg)) + List (Html.Styled.Attribute (PagesMsg userMsg)) + -> Options error parsed input userMsg -> - --{ submitting : Bool, serverResponse : Maybe (Form.ServerResponse error), state : Form.Model } { --path : Path --, url : Maybe PageUrl --, action : Maybe action @@ -104,18 +143,17 @@ renderStyledHtml : , transition : Maybe Pages.Transition.Transition , fetchers : Dict String (Pages.Transition.FetcherState (Maybe action)) } - -> input -> Form.Form error { combine : Validation error parsed named constraints, view : Form.Context error input -> List (Html.Styled.Html (PagesMsg userMsg)) } parsed input (PagesMsg userMsg) -> Html.Styled.Html (PagesMsg userMsg) -renderStyledHtml formId attrs app input form_ = +renderStyledHtml attrs options_ app form_ = form_ |> Form.renderStyledHtml - formId + options_.id attrs { state = app.pageFormState , serverResponse = Nothing -- TODO , submitting = - (case app.fetchers |> Dict.get formId of + (case app.fetchers |> Dict.get options_.id of Just { status } -> case status of Pages.Transition.FetcherComplete _ -> @@ -132,10 +170,10 @@ renderStyledHtml formId attrs app input form_ = ) || (case app.transition of Just (Pages.Transition.Submitting formData) -> - formData.id == Just formId + formData.id == Just options_.id Just (Pages.Transition.LoadAfterSubmit submitData _ _) -> - submitData.id == Just formId + submitData.id == Just options_.id Just (Pages.Transition.Loading _ _) -> False @@ -150,23 +188,23 @@ renderStyledHtml formId attrs app input form_ = case parsed of Form.Valid _ -> Pages.Internal.Msg.Submit - { useFetcher = False -- TODO + { useFetcher = options_.parallel , fields = fields - , msg = Nothing -- TODO - , id = formId + , msg = options_.onSubmit |> Maybe.map (\onSubmit -> onSubmit parsed) + , id = options_.id , valid = True , action = action } Form.Invalid _ _ -> Pages.Internal.Msg.Submit - { useFetcher = False -- TODO + { useFetcher = options_.parallel , fields = fields - , msg = Nothing -- TODO - , id = formId + , msg = options_.onSubmit |> Maybe.map (\onSubmit -> onSubmit parsed) + , id = options_.id , valid = False , action = action } ) } - input + options_.input