mirror of
https://github.com/dillonkearns/elm-pages-v3-beta.git
synced 2024-12-28 14:23:04 +03:00
Migrate another page.
This commit is contained in:
parent
4496a72e35
commit
8680999754
@ -3,7 +3,6 @@ module Route.SmoothieId_.Edit exposing (ActionData, Data, Model, Msg, route)
|
||||
import Api.Scalar exposing (Uuid(..))
|
||||
import Data.Smoothies as Smoothies exposing (Smoothie)
|
||||
import DataSource exposing (DataSource)
|
||||
import Dict
|
||||
import Effect exposing (Effect)
|
||||
import ErrorPage exposing (ErrorPage)
|
||||
import Form
|
||||
@ -125,7 +124,7 @@ data routeParams =
|
||||
|
||||
action : RouteParams -> Request.Parser (DataSource (Response ActionData ErrorPage))
|
||||
action routeParams =
|
||||
Request.formDataWithoutServerValidation [ form, deleteForm ]
|
||||
Request.formDataWithoutServerValidation2 [ form, deleteForm ]
|
||||
|> MySession.expectSessionDataOrRedirect (Session.get "userId" >> Maybe.map Uuid)
|
||||
(\userId parsed session ->
|
||||
case parsed of
|
||||
@ -174,112 +173,111 @@ type alias EditInfo =
|
||||
{ name : String, description : String, price : Int, imageUrl : String }
|
||||
|
||||
|
||||
deleteForm : Form.HtmlForm String Action data Msg
|
||||
deleteForm : Form.HtmlFormNew String Action data Msg
|
||||
deleteForm =
|
||||
Form.init
|
||||
(Validation.succeed Delete)
|
||||
(\formState ->
|
||||
[ Html.button
|
||||
[ Attr.style "color" "red"
|
||||
Form.init2
|
||||
{ combine = Validation.succeed Delete
|
||||
, view =
|
||||
\formState ->
|
||||
[ Html.button
|
||||
[ Attr.style "color" "red"
|
||||
]
|
||||
[ Html.text "Delete" ]
|
||||
]
|
||||
[ Html.text "Delete" ]
|
||||
]
|
||||
)
|
||||
|> Form.hiddenKind ( "kind", "delete" ) "Required"
|
||||
}
|
||||
|> Form.hiddenKind2 ( "kind", "delete" ) "Required"
|
||||
|
||||
|
||||
form : Form.HtmlForm String Action Data Msg
|
||||
form : Form.HtmlFormNew String Action Data Msg
|
||||
form =
|
||||
Form.init
|
||||
Form.init2
|
||||
(\name description price imageUrl media ->
|
||||
Validation.succeed EditInfo
|
||||
|> Validation.andMap name
|
||||
|> Validation.andMap description
|
||||
|> Validation.andMap price
|
||||
|> Validation.andMap imageUrl
|
||||
|> Validation.map Edit
|
||||
)
|
||||
(\formState name description price imageUrl media ->
|
||||
let
|
||||
errors field =
|
||||
formState.errors
|
||||
|> Form.errorsForField field
|
||||
{ combine =
|
||||
Validation.succeed EditInfo
|
||||
|> Validation.andMap name
|
||||
|> Validation.andMap description
|
||||
|> Validation.andMap price
|
||||
|> Validation.andMap imageUrl
|
||||
|> Validation.map Edit
|
||||
, view =
|
||||
\formState ->
|
||||
let
|
||||
errorsView field =
|
||||
(if formState.submitAttempted || True then
|
||||
formState.errors
|
||||
|> Form.errorsForField2 field
|
||||
|> List.map (\error -> Html.li [] [ Html.text error ])
|
||||
|
||||
errorsView field =
|
||||
(if formState.submitAttempted || True then
|
||||
field
|
||||
|> errors
|
||||
|> List.map (\error -> Html.li [] [ Html.text error ])
|
||||
else
|
||||
[]
|
||||
)
|
||||
|> Html.ul [ Attr.style "color" "red" ]
|
||||
|
||||
else
|
||||
[]
|
||||
)
|
||||
|> Html.ul [ Attr.style "color" "red" ]
|
||||
fieldView label field =
|
||||
Html.div []
|
||||
[ Html.label []
|
||||
[ Html.text (label ++ " ")
|
||||
, field |> Form.FieldView.input2 []
|
||||
]
|
||||
, errorsView field
|
||||
]
|
||||
in
|
||||
[ fieldView "Name" name
|
||||
, fieldView "Description" description
|
||||
, fieldView "Price" price
|
||||
, fieldView "Image" imageUrl
|
||||
, Form.FieldView.radio2 []
|
||||
(\enum toRadio ->
|
||||
Html.label []
|
||||
[ toRadio []
|
||||
, Html.text
|
||||
(case enum of
|
||||
Article ->
|
||||
"📄 Article"
|
||||
|
||||
fieldView label field =
|
||||
Html.div []
|
||||
[ Html.label []
|
||||
[ Html.text (label ++ " ")
|
||||
, field |> Form.FieldView.input []
|
||||
]
|
||||
, errorsView field
|
||||
]
|
||||
in
|
||||
[ fieldView "Name" name
|
||||
, fieldView "Description" description
|
||||
, fieldView "Price" price
|
||||
, fieldView "Image" imageUrl
|
||||
, Form.FieldView.radio []
|
||||
(\enum toRadio ->
|
||||
Html.label []
|
||||
[ toRadio []
|
||||
, Html.text
|
||||
(case enum of
|
||||
Article ->
|
||||
"📄 Article"
|
||||
Book ->
|
||||
"📕 Book"
|
||||
|
||||
Book ->
|
||||
"📕 Book"
|
||||
Video ->
|
||||
"📺 Video"
|
||||
)
|
||||
]
|
||||
)
|
||||
media
|
||||
, Html.button []
|
||||
[ Html.text
|
||||
(if formState.isTransitioning then
|
||||
"Updating..."
|
||||
|
||||
Video ->
|
||||
"📺 Video"
|
||||
else
|
||||
"Update"
|
||||
)
|
||||
]
|
||||
)
|
||||
media
|
||||
, Html.button []
|
||||
[ Html.text
|
||||
(if formState.isTransitioning then
|
||||
"Updating..."
|
||||
|
||||
else
|
||||
"Update"
|
||||
)
|
||||
]
|
||||
]
|
||||
]
|
||||
}
|
||||
)
|
||||
|> Form.field "name"
|
||||
|> Form.field2 "name"
|
||||
(Field.text
|
||||
|> Field.required "Required"
|
||||
|> Field.withInitialValue (\{ smoothie } -> Form.Value.string smoothie.name)
|
||||
)
|
||||
|> Form.field "description"
|
||||
|> Form.field2 "description"
|
||||
(Field.text
|
||||
|> Field.required "Required"
|
||||
|> Field.withInitialValue (\{ smoothie } -> Form.Value.string smoothie.description)
|
||||
)
|
||||
|> Form.field "price"
|
||||
|> Form.field2 "price"
|
||||
(Field.int { invalid = \_ -> "Invalid int" }
|
||||
|> Field.required "Required"
|
||||
|> Field.withMin (Form.Value.int 1) "Price must be at least $1"
|
||||
|> Field.withInitialValue (\{ smoothie } -> Form.Value.int smoothie.price)
|
||||
)
|
||||
|> Form.field "imageUrl"
|
||||
|> Form.field2 "imageUrl"
|
||||
(Field.text
|
||||
|> Field.required "Required"
|
||||
|> Field.withInitialValue (\{ smoothie } -> Form.Value.string smoothie.unsplashImage)
|
||||
)
|
||||
|> Form.field "media"
|
||||
|> Form.field2 "media"
|
||||
(Field.select
|
||||
[ ( "article", Article )
|
||||
, ( "book", Book )
|
||||
@ -287,7 +285,7 @@ form =
|
||||
]
|
||||
(\option -> "Invalid option " ++ option)
|
||||
)
|
||||
|> Form.hiddenKind ( "kind", "edit" ) "Required"
|
||||
|> Form.hiddenKind2 ( "kind", "edit" ) "Required"
|
||||
|
||||
|
||||
type Media
|
||||
@ -317,7 +315,7 @@ view maybeUrl sharedModel model app =
|
||||
pendingCreation : Maybe NewItem
|
||||
pendingCreation =
|
||||
form
|
||||
|> Form.parse app app.data
|
||||
|> Form.parse2 app app.data
|
||||
|> parseIgnoreErrors
|
||||
|> Result.toMaybe
|
||||
|> Maybe.andThen
|
||||
@ -334,7 +332,7 @@ view maybeUrl sharedModel model app =
|
||||
, body =
|
||||
[ Html.h2 [] [ Html.text "Update item" ]
|
||||
, form
|
||||
|> Form.toDynamicTransition "form"
|
||||
|> Form.toDynamicTransitionNew "form"
|
||||
|> Form.renderHtml
|
||||
[ Attr.style "display" "flex"
|
||||
, Attr.style "flex-direction" "column"
|
||||
@ -348,7 +346,7 @@ view maybeUrl sharedModel model app =
|
||||
|> Maybe.map pendingView
|
||||
|> Maybe.withDefault (Html.div [] [])
|
||||
, deleteForm
|
||||
|> Form.toDynamicTransition "delete-form"
|
||||
|> Form.toDynamicTransitionNew "delete-form"
|
||||
|> Form.renderHtml []
|
||||
-- TODO
|
||||
Nothing
|
||||
|
Loading…
Reference in New Issue
Block a user