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

View File

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

View File

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

View File

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

View File

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