Add options builder for form options.

This commit is contained in:
Dillon Kearns 2023-04-05 10:27:29 -07:00
parent 1134af8293
commit 92a8057c63
6 changed files with 94 additions and 50 deletions

View File

@ -211,12 +211,14 @@ view app shared model =
) )
] ]
[ form [ form
--|> Form.toDynamicFetcher |> Pages.Form.renderStyledHtml
|> Pages.Form.renderStyledHtml "dark-mode"
[] []
--(.formResponse >> Just) --(.formResponse >> Just)
(Pages.Form.options "dark-mode"
|> Pages.Form.withInput app.data.isDarkMode
|> Pages.Form.withParallel
)
app app
app.data.isDarkMode
, Html.text <| , Html.text <|
"Current mode: " "Current mode: "
++ (if app.data.isDarkMode then ++ (if app.data.isDarkMode then

View File

@ -238,22 +238,22 @@ view app sharedModel model =
[ Html.p [] [ Html.p []
[ Html.text <| String.fromInt model.itemIndex ] [ Html.text <| String.fromInt model.itemIndex ]
, form , form
-- TODO |> Pages.Form.renderStyledHtml
--|> Form.withOnSubmit (\_ -> AddItemSubmitted)
|> Pages.Form.renderStyledHtml ("add-item-" ++ String.fromInt model.itemIndex)
[] []
(Pages.Form.options ("add-item-" ++ String.fromInt model.itemIndex)
|> Pages.Form.withOnSubmit (\_ -> AddItemSubmitted)
)
--(\_ -> Nothing) --(\_ -> Nothing)
app app
()
, Html.div [] , Html.div []
[ deleteForm [ deleteForm
-- TODO |> Pages.Form.renderStyledHtml
--|> Form.toDynamicFetcher
|> Pages.Form.renderStyledHtml "delete-all"
[] []
(Pages.Form.options "delete-all"
|> Pages.Form.withParallel
)
--(\_ -> Nothing) --(\_ -> Nothing)
app app
()
] ]
, optimisticItems , optimisticItems
|> List.map |> List.map

View File

@ -247,15 +247,17 @@ view app shared =
[] []
[ Html.text <| "Edit profile " ++ user.first ++ " " ++ user.last ] [ Html.text <| "Edit profile " ++ user.first ++ " " ++ user.last ]
, form , form
|> Pages.Form.renderHtml "user-form" |> Pages.Form.renderHtml
[ Attr.style "display" "flex" [ Attr.style "display" "flex"
, Attr.style "flex-direction" "column" , Attr.style "flex-direction" "column"
, Attr.style "gap" "20px" , Attr.style "gap" "20px"
] ]
(Pages.Form.options "user-form"
|> Pages.Form.withInput defaultUser
)
-- TODO -- TODO
--(.formResponse >> Just) --(.formResponse >> Just)
app app
defaultUser
] ]
|> List.map Html.Styled.fromUnstyled |> List.map Html.Styled.fromUnstyled
} }

View File

@ -126,14 +126,16 @@ view app shared =
, body = , body =
[ form [ form
|> Form.withGetMethod |> Form.withGetMethod
|> Pages.Form.renderHtml "user-form" |> Pages.Form.renderHtml
[ Attr.style "display" "flex" [ Attr.style "display" "flex"
, Attr.style "flex-direction" "column" , Attr.style "flex-direction" "column"
, Attr.style "gap" "20px" , Attr.style "gap" "20px"
] ]
(Pages.Form.options "user-form"
|> Pages.Form.withInput app.data.filters
)
--(\_ -> Nothing) --(\_ -> Nothing)
app app
app.data.filters
, Html.h2 [] , Html.h2 []
[ Html.text <| "Current page: " ++ String.fromInt app.data.filters.page [ Html.text <| "Current page: " ++ String.fromInt app.data.filters.page
] ]

View File

@ -220,12 +220,12 @@ view app shared =
) )
] ]
, form , form
|> Pages.Form.renderStyledHtml "form" |> Pages.Form.renderStyledHtml
[] []
(Pages.Form.options "form")
-- TODO -- TODO
--(.errors >> Just) --(.errors >> Just)
app app
()
] ]
} }

View File

@ -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 Dict exposing (Dict)
import Form import Form
@ -10,11 +10,52 @@ import Pages.Transition
import PagesMsg exposing (PagesMsg) 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 : 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 { --path : Path
--, url : Maybe PageUrl --, url : Maybe PageUrl
--, action : Maybe action --, action : Maybe action
@ -23,18 +64,17 @@ renderHtml :
, transition : Maybe Pages.Transition.Transition , transition : Maybe Pages.Transition.Transition
, fetchers : Dict String (Pages.Transition.FetcherState (Maybe action)) , 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) -> 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) -> Html.Html (PagesMsg userMsg)
renderHtml formId attrs app input form_ = renderHtml attrs options_ app form_ =
form_ form_
|> Form.renderHtml |> Form.renderHtml
formId options_.id
attrs attrs
{ state = app.pageFormState { state = app.pageFormState
, serverResponse = Nothing -- TODO , serverResponse = Nothing -- TODO
, submitting = , submitting =
(case app.fetchers |> Dict.get formId of (case app.fetchers |> Dict.get options_.id of
Just { status } -> Just { status } ->
case status of case status of
Pages.Transition.FetcherComplete _ -> Pages.Transition.FetcherComplete _ ->
@ -51,10 +91,10 @@ renderHtml formId attrs app input form_ =
) )
|| (case app.transition of || (case app.transition of
Just (Pages.Transition.Submitting formData) -> Just (Pages.Transition.Submitting formData) ->
formData.id == Just formId formData.id == Just options_.id
Just (Pages.Transition.LoadAfterSubmit submitData _ _) -> Just (Pages.Transition.LoadAfterSubmit submitData _ _) ->
submitData.id == Just formId submitData.id == Just options_.id
Just (Pages.Transition.Loading _ _) -> Just (Pages.Transition.Loading _ _) ->
False False
@ -69,33 +109,32 @@ renderHtml formId attrs app input form_ =
case parsed of case parsed of
Form.Valid _ -> Form.Valid _ ->
Pages.Internal.Msg.Submit Pages.Internal.Msg.Submit
{ useFetcher = False -- TODO { useFetcher = options_.parallel
, action = action , action = action
, fields = fields , fields = fields
, msg = Nothing -- TODO , msg = options_.onSubmit |> Maybe.map (\onSubmit -> onSubmit parsed)
, id = formId , id = options_.id
, valid = True , valid = True
} }
Form.Invalid _ _ -> Form.Invalid _ _ ->
Pages.Internal.Msg.Submit Pages.Internal.Msg.Submit
{ useFetcher = False -- TODO { useFetcher = options_.parallel
, action = action , action = action
, fields = fields , fields = fields
, msg = Nothing -- TODO , msg = options_.onSubmit |> Maybe.map (\onSubmit -> onSubmit parsed)
, id = formId , id = options_.id
, valid = False , valid = False
} }
) )
} }
input options_.input
renderStyledHtml : 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 { --path : Path
--, url : Maybe PageUrl --, url : Maybe PageUrl
--, action : Maybe action --, action : Maybe action
@ -104,18 +143,17 @@ renderStyledHtml :
, transition : Maybe Pages.Transition.Transition , transition : Maybe Pages.Transition.Transition
, fetchers : Dict String (Pages.Transition.FetcherState (Maybe action)) , 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) -> 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) -> Html.Styled.Html (PagesMsg userMsg)
renderStyledHtml formId attrs app input form_ = renderStyledHtml attrs options_ app form_ =
form_ form_
|> Form.renderStyledHtml |> Form.renderStyledHtml
formId options_.id
attrs attrs
{ state = app.pageFormState { state = app.pageFormState
, serverResponse = Nothing -- TODO , serverResponse = Nothing -- TODO
, submitting = , submitting =
(case app.fetchers |> Dict.get formId of (case app.fetchers |> Dict.get options_.id of
Just { status } -> Just { status } ->
case status of case status of
Pages.Transition.FetcherComplete _ -> Pages.Transition.FetcherComplete _ ->
@ -132,10 +170,10 @@ renderStyledHtml formId attrs app input form_ =
) )
|| (case app.transition of || (case app.transition of
Just (Pages.Transition.Submitting formData) -> Just (Pages.Transition.Submitting formData) ->
formData.id == Just formId formData.id == Just options_.id
Just (Pages.Transition.LoadAfterSubmit submitData _ _) -> Just (Pages.Transition.LoadAfterSubmit submitData _ _) ->
submitData.id == Just formId submitData.id == Just options_.id
Just (Pages.Transition.Loading _ _) -> Just (Pages.Transition.Loading _ _) ->
False False
@ -150,23 +188,23 @@ renderStyledHtml formId attrs app input form_ =
case parsed of case parsed of
Form.Valid _ -> Form.Valid _ ->
Pages.Internal.Msg.Submit Pages.Internal.Msg.Submit
{ useFetcher = False -- TODO { useFetcher = options_.parallel
, fields = fields , fields = fields
, msg = Nothing -- TODO , msg = options_.onSubmit |> Maybe.map (\onSubmit -> onSubmit parsed)
, id = formId , id = options_.id
, valid = True , valid = True
, action = action , action = action
} }
Form.Invalid _ _ -> Form.Invalid _ _ ->
Pages.Internal.Msg.Submit Pages.Internal.Msg.Submit
{ useFetcher = False -- TODO { useFetcher = options_.parallel
, fields = fields , fields = fields
, msg = Nothing -- TODO , msg = options_.onSubmit |> Maybe.map (\onSubmit -> onSubmit parsed)
, id = formId , id = options_.id
, valid = False , valid = False
, action = action , action = action
} }
) )
} }
input options_.input