Update docs.

This commit is contained in:
Dillon Kearns 2022-06-15 09:25:49 -07:00
parent 145bc194ad
commit 082ba3e037
6 changed files with 143 additions and 7 deletions

File diff suppressed because one or more lines are too long

View File

@ -28,6 +28,8 @@
"Pages.Flags", "Pages.Flags",
"Pages.Fetcher", "Pages.Fetcher",
"Form", "Form",
"Pages.Field",
"Pages.FormParser",
"Form.Value" "Form.Value"
], ],
"elm-version": "0.19.0 <= v < 0.20.0", "elm-version": "0.19.0 <= v < 0.20.0",

View File

@ -1,14 +1,25 @@
module Pages.Field exposing (..) module Pages.Field exposing
( FieldInfo, checkbox, exactValue, int, required, text, withClientValidation, withInitialValue
, Field(..), No(..), Yes(..)
)
{-|
@docs Field, FieldInfo, No, Yes, checkbox, exactValue, int, required, text, withClientValidation, withInitialValue
-}
import DataSource exposing (DataSource) import DataSource exposing (DataSource)
import Form.Value import Form.Value
import Json.Encode as Encode import Json.Encode as Encode
{-| -}
type Field error parsed data constraints type Field error parsed data constraints
= Field (FieldInfo error parsed data) = Field (FieldInfo error parsed data)
{-| -}
type alias FieldInfo error parsed data = type alias FieldInfo error parsed data =
{ initialValue : Maybe (data -> String) { initialValue : Maybe (data -> String)
, type_ : String , type_ : String

View File

@ -1,4 +1,10 @@
module Pages.FormParser exposing (..) module Pages.FormParser exposing (CombinedParser(..), CompleteParser(..), Context, FieldDefinition(..), FieldErrors, HtmlForm, InputType(..), ParseResult(..), ParsedField, Parser(..), ParsingResult(..), RawField, TextType(..), addError, addErrors, andThen, andThenNew, fail, field, hiddenField, hiddenKind, init, input, int, map, map2, ok, optional, render, renderHelper, renderHtml, required, run, runNew, runOneOfServerSide, runServerSide, succeed, toResult, validate, withError)
{-|
@docs CombinedParser, CompleteParser, Context, FieldDefinition, FieldErrors, HtmlForm, InputType, ParseResult, ParsedField, Parser, ParsingResult, RawField, TextType, addError, addErrors, andThen, andThenNew, fail, field, hiddenField, hiddenKind, init, input, int, map, map2, ok, optional, render, renderHelper, renderHtml, required, run, runNew, runOneOfServerSide, runServerSide, succeed, toResult, validate, withError
-}
import Dict exposing (Dict) import Dict exposing (Dict)
import Dict.Extra import Dict.Extra
@ -11,6 +17,7 @@ import Pages.Msg
import Pages.Transition import Pages.Transition
{-| -}
type type
ParseResult error decoded ParseResult error decoded
-- TODO parse into both errors AND a decoded value -- TODO parse into both errors AND a decoded value
@ -19,10 +26,12 @@ type
| DecodeFailure (Dict String (List error)) | DecodeFailure (Dict String (List error))
{-| -}
type Parser error decoded type Parser error decoded
= Parser (Dict String (List error) -> Form.FormState -> ( Maybe decoded, Dict String (List error) )) = Parser (Dict String (List error) -> Form.FormState -> ( Maybe decoded, Dict String (List error) ))
{-| -}
optional : String -> Parser error (Maybe String) optional : String -> Parser error (Maybe String)
optional name = optional name =
(\errors form -> (\errors form ->
@ -31,6 +40,7 @@ optional name =
|> Parser |> Parser
{-| -}
init : Form.FormState init : Form.FormState
init = init =
{ fields = Dict.empty { fields = Dict.empty
@ -38,6 +48,7 @@ init =
} }
{-| -}
type alias Context error = type alias Context error =
{ errors : FieldErrors error { errors : FieldErrors error
, isTransitioning : Bool , isTransitioning : Bool
@ -45,6 +56,7 @@ type alias Context error =
} }
{-| -}
andThenNew : combined -> (Context String -> viewFn) -> CombinedParser String combined data (Context String -> viewFn) andThenNew : combined -> (Context String -> viewFn) -> CombinedParser String combined data (Context String -> viewFn)
andThenNew fn viewFn = andThenNew fn viewFn =
CombinedParser [] CombinedParser []
@ -56,6 +68,7 @@ andThenNew fn viewFn =
(\_ -> []) (\_ -> [])
{-| -}
field : field :
String String
-> Field error parsed data constraints -> Field error parsed data constraints
@ -142,6 +155,7 @@ field name (Field fieldParser) (CombinedParser definitions parseFn toInitialValu
) )
{-| -}
hiddenField : hiddenField :
String String
-> Field error parsed data constraints -> Field error parsed data constraints
@ -230,6 +244,7 @@ hiddenField name (Field fieldParser) (CombinedParser definitions parseFn toIniti
) )
{-| -}
hiddenKind : hiddenKind :
( String, String ) ( String, String )
-> error -> error
@ -302,14 +317,31 @@ hiddenKind ( name, value ) error_ (CombinedParser definitions parseFn toInitialV
) )
{-| -}
type ParsingResult a type ParsingResult a
= ParsingResult = ParsingResult
{-| -}
type CompleteParser error parsed type CompleteParser error parsed
= CompleteParser = CompleteParser
{-| -}
type InputType
= Text TextType
| TextArea
| Radio
| Checkbox
| Select (List ( String, String ))
{-| -}
type TextType
= Phone
{-| -}
input : List (Html.Attribute msg) -> RawField -> Html msg input : List (Html.Attribute msg) -> RawField -> Html msg
input attrs rawField = input attrs rawField =
Html.input Html.input
@ -322,10 +354,12 @@ input attrs rawField =
[] []
{-| -}
type alias FieldErrors error = type alias FieldErrors error =
Dict String (List error) Dict String (List error)
{-| -}
type alias AppContext app = type alias AppContext app =
{ app { app
| --, sharedData : Shared.Data | --, sharedData : Shared.Data
@ -341,6 +375,7 @@ type alias AppContext app =
} }
{-| -}
runNew : runNew :
AppContext app AppContext app
-> data -> data
@ -375,6 +410,7 @@ runNew app data (CombinedParser fieldDefinitions parser _) =
} }
{-| -}
runServerSide : runServerSide :
List ( String, String ) List ( String, String )
-> CombinedParser error parsed data (Context error -> view) -> CombinedParser error parsed data (Context error -> view)
@ -410,6 +446,7 @@ runServerSide rawFormData (CombinedParser fieldDefinitions parser _) =
parsed.result parsed.result
{-| -}
runOneOfServerSide : runOneOfServerSide :
List ( String, String ) List ( String, String )
-> List (CombinedParser error parsed data (Context error -> view)) -> List (CombinedParser error parsed data (Context error -> view))
@ -439,6 +476,41 @@ runOneOfServerSide rawFormData parsers =
( Nothing, Dict.empty ) ( Nothing, Dict.empty )
--Debug.todo ""
--let
-- parsed : { result : ( Maybe parsed, FieldErrors error ), view : Context error -> view }
-- parsed =
-- parser Nothing thisFormState
--
-- thisFormState : Form.FormState
-- thisFormState =
-- { init
-- | fields =
-- rawFormData
-- |> List.map
-- (Tuple.mapSecond
-- (\value ->
-- { value = value
-- , status = Form.NotVisited
-- }
-- )
-- )
-- |> Dict.fromList
-- }
--
-- context =
-- { errors = parsed.result |> Tuple.second
-- , isTransitioning = False
-- , submitAttempted = False
-- }
--in
--{ result = parsed.result
--, view = parsed.view context
--}
{-| -}
renderHtml : renderHtml :
AppContext app AppContext app
-> data -> data
@ -455,6 +527,23 @@ renderHtml app data combinedParser =
Html.Lazy.lazy3 renderHelper app data combinedParser Html.Lazy.lazy3 renderHelper app data combinedParser
--renderStyledHtml :
-- AppContext app data
-- ->
-- CombinedParser
-- error
-- parsed
-- data
-- (Context error
-- -> ( List (Html.Styled.Attribute (Pages.Msg.Msg msg)), List (Html (Pages.Msg.Msg msg)) )
-- )
-- -> Html (Pages.Msg.Msg msg)
--renderStyledHtml formState_ combinedParser =
-- Html.Lazy.lazy2 renderHelper formState_ combinedParser
{-| -}
renderHelper : renderHelper :
AppContext app AppContext app
-> data -> data
@ -583,6 +672,7 @@ renderHelper formState data (CombinedParser fieldDefinitions parser toInitialVal
(hiddenInputs ++ children) (hiddenInputs ++ children)
{-| -}
toResult : ( Maybe parsed, FieldErrors error ) -> Result (FieldErrors error) parsed toResult : ( Maybe parsed, FieldErrors error ) -> Result (FieldErrors error) parsed
toResult ( maybeParsed, fieldErrors ) = toResult ( maybeParsed, fieldErrors ) =
let let
@ -603,6 +693,7 @@ toResult ( maybeParsed, fieldErrors ) =
Err fieldErrors Err fieldErrors
{-| -}
render : render :
AppContext app AppContext app
-> data -> data
@ -650,6 +741,7 @@ render formState data (CombinedParser fieldDefinitions parser toInitialValues) =
parsed.view context parsed.view context
{-| -}
type alias HtmlForm error parsed data msg = type alias HtmlForm error parsed data msg =
CombinedParser CombinedParser
error error
@ -658,6 +750,7 @@ type alias HtmlForm error parsed data msg =
(Context error -> ( List (Html.Attribute (Pages.Msg.Msg msg)), List (Html (Pages.Msg.Msg msg)) )) (Context error -> ( List (Html.Attribute (Pages.Msg.Msg msg)), List (Html (Pages.Msg.Msg msg)) ))
{-| -}
type CombinedParser error parsed data view type CombinedParser error parsed data view
= CombinedParser = CombinedParser
-- TODO track hidden fields here - for renderHtml and renderStyled, automatically render them -- TODO track hidden fields here - for renderHtml and renderStyled, automatically render them
@ -676,11 +769,13 @@ type CombinedParser error parsed data view
(data -> List ( String, String )) (data -> List ( String, String ))
{-| -}
type FieldDefinition type FieldDefinition
= RegularField = RegularField
| HiddenField | HiddenField
{-| -}
type alias ParsedField error parsed = type alias ParsedField error parsed =
{ name : String { name : String
, value : parsed , value : parsed
@ -688,6 +783,7 @@ type alias ParsedField error parsed =
} }
{-| -}
type alias RawField = type alias RawField =
{ name : String { name : String
, value : Maybe String , value : Maybe String
@ -695,16 +791,20 @@ type alias RawField =
} }
{-| -}
ok : a -> a
ok result = ok result =
result result
{-| -}
withError : error -> ParsedField error parsed -> () withError : error -> ParsedField error parsed -> ()
withError _ _ = withError _ _ =
--Debug.todo "" --Debug.todo ""
() ()
{-| -}
required : String -> error -> Parser error String required : String -> error -> Parser error String
required name error = required name error =
(\errors form -> (\errors form ->
@ -721,6 +821,7 @@ required name error =
|> Parser |> Parser
{-| -}
int : String -> error -> Parser error Int int : String -> error -> Parser error Int
int name error = int name error =
(\errors form -> (\errors form ->
@ -742,6 +843,7 @@ int name error =
|> Parser |> Parser
{-| -}
map2 : (value1 -> value2 -> combined) -> Parser error value1 -> Parser error value2 -> Parser error combined map2 : (value1 -> value2 -> combined) -> Parser error value1 -> Parser error value2 -> Parser error combined
map2 combineFn (Parser parser1) (Parser parser2) = map2 combineFn (Parser parser1) (Parser parser2) =
(\errors form -> (\errors form ->
@ -765,6 +867,7 @@ map2 combineFn (Parser parser1) (Parser parser2) =
|> Parser |> Parser
{-| -}
map : (original -> mapped) -> Parser error original -> Parser error mapped map : (original -> mapped) -> Parser error original -> Parser error mapped
map mapFn (Parser parser) = map mapFn (Parser parser) =
(\errors form -> (\errors form ->
@ -779,6 +882,7 @@ map mapFn (Parser parser) =
|> Parser |> Parser
{-| -}
validate : String -> (original -> Result error mapped) -> Parser error original -> Parser error mapped validate : String -> (original -> Result error mapped) -> Parser error original -> Parser error mapped
validate name mapFn (Parser parser) = validate name mapFn (Parser parser) =
(\errors form -> (\errors form ->
@ -805,16 +909,19 @@ validate name mapFn (Parser parser) =
|> Parser |> Parser
{-| -}
succeed : value -> Parser error value succeed : value -> Parser error value
succeed value_ = succeed value_ =
Parser (\errors form -> ( Just value_, Dict.empty )) Parser (\errors form -> ( Just value_, Dict.empty ))
{-| -}
fail : error -> Parser error value fail : error -> Parser error value
fail error = fail error =
Parser (\errors form -> ( Nothing, Dict.fromList [ ( "global", [ error ] ) ] )) Parser (\errors form -> ( Nothing, Dict.fromList [ ( "global", [ error ] ) ] ))
{-| -}
andThen : (value1 -> Parser error value2) -> Parser error value1 -> Parser error value2 andThen : (value1 -> Parser error value2) -> Parser error value1 -> Parser error value2
andThen andThenFn (Parser parser1) = andThen andThenFn (Parser parser1) =
(\errors form -> (\errors form ->
@ -848,6 +955,7 @@ andThen andThenFn (Parser parser1) =
|> Parser |> Parser
{-| -}
run : Form.FormState -> Parser error decoded -> ( Maybe decoded, Dict String (List error) ) run : Form.FormState -> Parser error decoded -> ( Maybe decoded, Dict String (List error) )
run formState (Parser parser) = run formState (Parser parser) =
parser Dict.empty formState parser Dict.empty formState
@ -864,6 +972,7 @@ run formState (Parser parser) =
-- |> parser Dict.empty -- |> parser Dict.empty
{-| -}
addError : String -> error -> Dict String (List error) -> Dict String (List error) addError : String -> error -> Dict String (List error) -> Dict String (List error)
addError name error allErrors = addError name error allErrors =
allErrors allErrors
@ -873,6 +982,7 @@ addError name error allErrors =
) )
{-| -}
addErrors : String -> List error -> Dict String (List error) -> Dict String (List error) addErrors : String -> List error -> Dict String (List error) -> Dict String (List error)
addErrors name newErrors allErrors = addErrors name newErrors allErrors =
allErrors allErrors

View File

@ -1,14 +1,13 @@
module Pages.Msg exposing module Pages.Msg exposing
( Msg(..) ( Msg(..)
, map, onSubmit, fetcherOnSubmit , map, onSubmit, fetcherOnSubmit, submitIfValid
, submitIfValid
) )
{-| {-|
@docs Msg @docs Msg
@docs map, onSubmit, fetcherOnSubmit @docs map, onSubmit, fetcherOnSubmit, submitIfValid
-} -}

View File

@ -1,6 +1,9 @@
module Server.Request exposing module Server.Request exposing
( Parser ( Parser
, succeed, fromResult, skip , succeed, fromResult, skip
, formParserResultNew, formParserResult
, formData
, expectForm, formParser
, method, rawBody, allCookies, rawHeaders, queryParams , method, rawBody, allCookies, rawHeaders, queryParams
, requestTime, optionalHeader, expectContentType, expectJsonBody , requestTime, optionalHeader, expectContentType, expectJsonBody
, acceptMethod, acceptContentTypes , acceptMethod, acceptContentTypes
@ -14,7 +17,6 @@ module Server.Request exposing
, map3, map4, map5, map6, map7, map8, map9 , map3, map4, map5, map6, map7, map8, map9
, Method(..), methodToString , Method(..), methodToString
, errorsToString, errorToString, getDecoder, ValidationError , errorsToString, errorToString, getDecoder, ValidationError
, expectForm, formData, formParser, formParserResult, formParserResultNew
) )
{-| {-|
@ -24,6 +26,18 @@ module Server.Request exposing
@docs succeed, fromResult, skip @docs succeed, fromResult, skip
## Forms
@docs formParserResultNew, formParserResult
@docs formData
### Deprecated?
@docs expectForm, formParser
## Direct Values ## Direct Values
@docs method, rawBody, allCookies, rawHeaders, queryParams @docs method, rawBody, allCookies, rawHeaders, queryParams