mirror of
https://github.com/dillonkearns/elm-pages-v3-beta.git
synced 2024-11-24 15:12:01 +03:00
Remove obsolete form parser version.
This commit is contained in:
parent
4dc3921dfb
commit
5bbcc21998
@ -1,6 +1,5 @@
|
||||
module Route.Profile exposing (ActionData, Data, Model, Msg, route)
|
||||
|
||||
import Api.Scalar exposing (Uuid(..))
|
||||
import Data.User as User exposing (User)
|
||||
import DataSource exposing (DataSource)
|
||||
import Dict exposing (Dict)
|
||||
@ -12,7 +11,6 @@ import Html exposing (Html)
|
||||
import Html.Attributes as Attr
|
||||
import MySession
|
||||
import Pages.Form
|
||||
import Pages.FormParser as FormParser
|
||||
import Pages.Msg
|
||||
import Pages.PageUrl exposing (PageUrl)
|
||||
import Pages.Transition
|
||||
@ -120,50 +118,9 @@ type alias Action =
|
||||
}
|
||||
|
||||
|
||||
actionFormDecoder : FormParser.Parser String Action
|
||||
actionFormDecoder =
|
||||
FormParser.succeed Action
|
||||
|> andMap (FormParser.required "username" "First is required" |> FormParser.validate "username" validateUsername)
|
||||
|> andMap (FormParser.required "name" "First is required")
|
||||
|
||||
|
||||
validateUsername : String -> Result String String
|
||||
validateUsername rawUsername =
|
||||
if rawUsername |> String.contains "@" then
|
||||
Err "Username cannot include @"
|
||||
|
||||
else
|
||||
Ok rawUsername
|
||||
|
||||
|
||||
andMap : FormParser.Parser error a -> FormParser.Parser error (a -> b) -> FormParser.Parser error b
|
||||
andMap =
|
||||
FormParser.map2 (|>)
|
||||
|
||||
|
||||
action : RouteParams -> Request.Parser (DataSource (Response ActionData ErrorPage))
|
||||
action routeParams =
|
||||
Request.map2 Tuple.pair
|
||||
(Request.formParserResult actionFormDecoder)
|
||||
Request.requestTime
|
||||
|> MySession.expectSessionDataOrRedirect (Session.get "userId" >> Maybe.map Uuid)
|
||||
(\userId ( parsedAction, requestTime ) session ->
|
||||
case parsedAction |> Debug.log "parsedAction" of
|
||||
Ok { name } ->
|
||||
User.updateUser { userId = userId, name = name |> Debug.log "Updating name mutation" }
|
||||
|> Request.Hasura.mutationDataSource requestTime
|
||||
|> DataSource.map
|
||||
(\_ ->
|
||||
--Response.render parsedAction
|
||||
Route.redirectTo Route.Profile
|
||||
)
|
||||
|> DataSource.map (Tuple.pair session)
|
||||
|
||||
Err errors ->
|
||||
DataSource.succeed
|
||||
(Response.render parsedAction)
|
||||
|> DataSource.map (Tuple.pair session)
|
||||
)
|
||||
Request.skip "No action."
|
||||
|
||||
|
||||
head :
|
||||
|
@ -1,8 +1,11 @@
|
||||
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)
|
||||
module Pages.FormParser exposing
|
||||
( Context, FieldErrors, HtmlForm, ParsedField, RawField, addError, addErrors, andThenNew, field, hiddenField, hiddenKind, init, input, ok, render, renderHelper, renderHtml, runNew, runOneOfServerSide, runServerSide, toResult, withError
|
||||
, CombinedParser(..), CompleteParser(..), FieldDefinition(..), InputType(..), ParseResult(..), ParsingResult(..), TextType(..)
|
||||
)
|
||||
|
||||
{-|
|
||||
|
||||
@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
|
||||
@docs CombinedParser, CompleteParser, Context, FieldDefinition, FieldErrors, HtmlForm, InputType, ParseResult, ParsedField, ParsingResult, RawField, TextType, addError, addErrors, andThenNew, field, hiddenField, hiddenKind, init, input, ok, render, renderHelper, renderHtml, runNew, runOneOfServerSide, runServerSide, toResult, withError
|
||||
|
||||
-}
|
||||
|
||||
@ -28,20 +31,6 @@ type
|
||||
| DecodeFailure (Dict String (List error))
|
||||
|
||||
|
||||
{-| -}
|
||||
type Parser error decoded
|
||||
= Parser (Dict String (List error) -> Form.FormState -> ( Maybe decoded, Dict String (List error) ))
|
||||
|
||||
|
||||
{-| -}
|
||||
optional : String -> Parser error (Maybe String)
|
||||
optional name =
|
||||
(\errors form ->
|
||||
( Just (form.fields |> Dict.get name |> Maybe.map .value), errors )
|
||||
)
|
||||
|> Parser
|
||||
|
||||
|
||||
{-| -}
|
||||
init : Form.FormState
|
||||
init =
|
||||
@ -814,174 +803,6 @@ withError _ _ =
|
||||
()
|
||||
|
||||
|
||||
{-| -}
|
||||
required : String -> error -> Parser error String
|
||||
required name error =
|
||||
(\errors form ->
|
||||
case form.fields |> Dict.get name |> Maybe.map .value of
|
||||
Just "" ->
|
||||
( Just "", errors |> addError name error )
|
||||
|
||||
Just nonEmptyValue ->
|
||||
( Just nonEmptyValue, errors )
|
||||
|
||||
Nothing ->
|
||||
( Just "", errors |> addError name error )
|
||||
)
|
||||
|> Parser
|
||||
|
||||
|
||||
{-| -}
|
||||
int : String -> error -> Parser error Int
|
||||
int name error =
|
||||
(\errors form ->
|
||||
case form.fields |> Dict.get name |> Maybe.map .value of
|
||||
Just "" ->
|
||||
( Nothing, errors |> addError name error )
|
||||
|
||||
Just nonEmptyValue ->
|
||||
case nonEmptyValue |> String.toInt of
|
||||
Just parsedInt ->
|
||||
( Just parsedInt, errors )
|
||||
|
||||
Nothing ->
|
||||
( Nothing, errors |> addError name error )
|
||||
|
||||
Nothing ->
|
||||
( Nothing, errors |> addError name error )
|
||||
)
|
||||
|> Parser
|
||||
|
||||
|
||||
{-| -}
|
||||
map2 : (value1 -> value2 -> combined) -> Parser error value1 -> Parser error value2 -> Parser error combined
|
||||
map2 combineFn (Parser parser1) (Parser parser2) =
|
||||
(\errors form ->
|
||||
let
|
||||
( combined1, allErrors1 ) =
|
||||
parser1 errors form
|
||||
|
||||
( combined2, allErrors2 ) =
|
||||
parser2 errors form
|
||||
in
|
||||
( Maybe.map2 combineFn combined1 combined2
|
||||
, Dict.merge (\name errors1 dict -> ( name, errors1 ) :: dict)
|
||||
(\name errors1 errors2 dict -> ( name, errors1 ++ errors2 ) :: dict)
|
||||
(\name errors2 dict -> ( name, errors2 ) :: dict)
|
||||
allErrors1
|
||||
allErrors2
|
||||
[]
|
||||
|> Dict.fromList
|
||||
)
|
||||
)
|
||||
|> Parser
|
||||
|
||||
|
||||
{-| -}
|
||||
map : (original -> mapped) -> Parser error original -> Parser error mapped
|
||||
map mapFn (Parser parser) =
|
||||
(\errors form ->
|
||||
let
|
||||
( combined1, allErrors1 ) =
|
||||
parser errors form
|
||||
in
|
||||
( Maybe.map mapFn combined1
|
||||
, allErrors1
|
||||
)
|
||||
)
|
||||
|> Parser
|
||||
|
||||
|
||||
{-| -}
|
||||
validate : String -> (original -> Result error mapped) -> Parser error original -> Parser error mapped
|
||||
validate name mapFn (Parser parser) =
|
||||
(\errors form ->
|
||||
let
|
||||
( combined1, allErrors1 ) =
|
||||
parser errors form
|
||||
in
|
||||
case combined1 |> Maybe.map mapFn of
|
||||
Just (Ok okResult) ->
|
||||
( Just okResult
|
||||
, allErrors1
|
||||
)
|
||||
|
||||
Just (Err error) ->
|
||||
( Nothing
|
||||
, allErrors1 |> addError name error
|
||||
)
|
||||
|
||||
Nothing ->
|
||||
( Nothing
|
||||
, allErrors1
|
||||
)
|
||||
)
|
||||
|> Parser
|
||||
|
||||
|
||||
{-| -}
|
||||
succeed : value -> Parser error value
|
||||
succeed value_ =
|
||||
Parser (\errors form -> ( Just value_, Dict.empty ))
|
||||
|
||||
|
||||
{-| -}
|
||||
fail : error -> Parser error value
|
||||
fail error =
|
||||
Parser (\errors form -> ( Nothing, Dict.fromList [ ( "global", [ error ] ) ] ))
|
||||
|
||||
|
||||
{-| -}
|
||||
andThen : (value1 -> Parser error value2) -> Parser error value1 -> Parser error value2
|
||||
andThen andThenFn (Parser parser1) =
|
||||
(\errors form ->
|
||||
let
|
||||
( combined1, allErrors1 ) =
|
||||
parser1 errors form
|
||||
|
||||
foo : Maybe (Parser error value2)
|
||||
foo =
|
||||
Maybe.map andThenFn combined1
|
||||
in
|
||||
case foo of
|
||||
Just (Parser parser2) ->
|
||||
let
|
||||
( combined2, allErrors2 ) =
|
||||
parser2 errors form
|
||||
in
|
||||
( combined2
|
||||
, Dict.merge (\name errors1 dict -> ( name, errors1 ) :: dict)
|
||||
(\name errors1 errors2 dict -> ( name, errors1 ++ errors2 ) :: dict)
|
||||
(\name errors2 dict -> ( name, errors2 ) :: dict)
|
||||
allErrors1
|
||||
allErrors2
|
||||
[]
|
||||
|> Dict.fromList
|
||||
)
|
||||
|
||||
Nothing ->
|
||||
( Nothing, allErrors1 )
|
||||
)
|
||||
|> Parser
|
||||
|
||||
|
||||
{-| -}
|
||||
run : Form.FormState -> Parser error decoded -> ( Maybe decoded, Dict String (List error) )
|
||||
run formState (Parser parser) =
|
||||
parser Dict.empty formState
|
||||
|
||||
|
||||
|
||||
--runOnList : List ( String, String ) -> Parser error decoded -> ( Maybe decoded, Dict String (List error) )
|
||||
--runOnList rawFormData (Parser parser) =
|
||||
-- (rawFormData
|
||||
-- |> List.map
|
||||
-- (Tuple.mapSecond (\value_ -> { value = value_, status = Form.NotVisited }))
|
||||
-- |> Dict.fromList
|
||||
-- )
|
||||
-- |> parser Dict.empty
|
||||
|
||||
|
||||
{-| -}
|
||||
addError : String -> error -> Dict String (List error) -> Dict String (List error)
|
||||
addError name error allErrors =
|
||||
|
@ -1,9 +1,9 @@
|
||||
module Server.Request exposing
|
||||
( Parser
|
||||
, succeed, fromResult, skip
|
||||
, formParserResultNew, formParserResult
|
||||
, formParserResultNew
|
||||
, formData
|
||||
, expectForm, formParser
|
||||
, expectForm
|
||||
, method, rawBody, allCookies, rawHeaders, queryParams
|
||||
, requestTime, optionalHeader, expectContentType, expectJsonBody
|
||||
, acceptMethod, acceptContentTypes
|
||||
@ -28,14 +28,14 @@ module Server.Request exposing
|
||||
|
||||
## Forms
|
||||
|
||||
@docs formParserResultNew, formParserResult
|
||||
@docs formParserResultNew
|
||||
|
||||
@docs formData
|
||||
|
||||
|
||||
### Deprecated?
|
||||
|
||||
@docs expectForm, formParser
|
||||
@docs expectForm
|
||||
|
||||
|
||||
## Direct Values
|
||||
@ -102,7 +102,6 @@ import Internal.Request
|
||||
import Json.Decode
|
||||
import Json.Encode
|
||||
import List.NonEmpty
|
||||
import Pages.Form
|
||||
import Pages.FormParser
|
||||
import QueryParams
|
||||
import Time
|
||||
@ -915,75 +914,6 @@ fileField_ name =
|
||||
|> Internal.Request.Parser
|
||||
|
||||
|
||||
{-| -}
|
||||
formParser : Pages.FormParser.Parser String decoded -> Parser decoded
|
||||
formParser formParser_ =
|
||||
formData
|
||||
|> andThen
|
||||
(\rawFormData ->
|
||||
let
|
||||
--something : ( Maybe decoded, Dict String (List String) )
|
||||
( maybeDecoded, errors ) =
|
||||
Pages.FormParser.run
|
||||
{ fields =
|
||||
rawFormData
|
||||
|> List.map
|
||||
(Tuple.mapSecond (\value -> { value = value, status = Pages.Form.NotVisited }))
|
||||
|> Dict.fromList
|
||||
, submitAttempted = False
|
||||
}
|
||||
formParser_
|
||||
in
|
||||
case ( maybeDecoded, errors |> Dict.toList |> List.NonEmpty.fromList ) of
|
||||
( Just decoded, Nothing ) ->
|
||||
succeed decoded
|
||||
|
||||
( _, maybeErrors ) ->
|
||||
maybeErrors
|
||||
|> Maybe.map List.NonEmpty.toList
|
||||
|> Maybe.withDefault []
|
||||
-- TODO print out nicely formatted message
|
||||
|> Debug.toString
|
||||
|> skip
|
||||
)
|
||||
|
||||
|
||||
{-| -}
|
||||
formParserResult : Pages.FormParser.Parser error decoded -> Parser (Result { fields : List ( String, String ), errors : Dict String (List error) } decoded)
|
||||
formParserResult formParser_ =
|
||||
formData
|
||||
|> andThen
|
||||
(\rawFormData ->
|
||||
let
|
||||
--something : ( Maybe decoded, Dict String (List String) )
|
||||
( maybeDecoded, errors ) =
|
||||
Pages.FormParser.run
|
||||
{ fields =
|
||||
rawFormData
|
||||
|> List.map
|
||||
(Tuple.mapSecond (\value -> { value = value, status = Pages.Form.NotVisited }))
|
||||
|> Dict.fromList
|
||||
, submitAttempted = False
|
||||
}
|
||||
formParser_
|
||||
in
|
||||
case ( maybeDecoded, errors |> Dict.toList |> List.NonEmpty.fromList ) of
|
||||
( Just decoded, Nothing ) ->
|
||||
succeed (Ok decoded)
|
||||
|
||||
( _, maybeErrors ) ->
|
||||
Err
|
||||
{ fields = rawFormData
|
||||
, errors =
|
||||
maybeErrors
|
||||
|> Maybe.map List.NonEmpty.toList
|
||||
|> Maybe.withDefault []
|
||||
|> Dict.fromList
|
||||
}
|
||||
|> succeed
|
||||
)
|
||||
|
||||
|
||||
{-| -}
|
||||
formParserResultNew :
|
||||
List (Pages.FormParser.CombinedParser error combined data (Pages.FormParser.Context error -> viewFn))
|
||||
|
Loading…
Reference in New Issue
Block a user