Advertisement
Guest User

Untitled

a guest
Aug 14th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. import Html exposing (..)
  2. import Html.App as App
  3. import Html.Attributes exposing (..)
  4. import Html.Events exposing (onInput, onClick)
  5.  
  6.  
  7. main =
  8. App.beginnerProgram
  9. { model = model
  10. , view = view
  11. , update = update
  12. }
  13.  
  14.  
  15.  
  16. -- MODEL
  17. type alias User =
  18. {
  19. name : String,
  20. password : String
  21. }
  22.  
  23.  
  24. type alias Model =
  25. {
  26. form : User,
  27. users: List User
  28.  
  29. }
  30.  
  31. model: Model
  32. model =
  33. { form = {name = "", password = ""}, users = [] }
  34.  
  35.  
  36.  
  37. -- UPDATE
  38.  
  39.  
  40. type Msg
  41. = Name String
  42. | Password String
  43. | Submit
  44.  
  45.  
  46. update : Msg -> Model -> Model
  47. update msg model =
  48. case msg of
  49. Name name ->
  50. { model | form = updateForm msg model.form}
  51.  
  52. Password password ->
  53. { model | form = updateForm msg model.form}
  54.  
  55. Submit ->
  56. submitForm model
  57.  
  58.  
  59.  
  60. updateForm msg form =
  61. case msg of
  62. Name name ->
  63. { form | name = name }
  64.  
  65. Password password ->
  66. { form | password = password }
  67.  
  68. Submit ->
  69. form
  70.  
  71. clearName form =
  72. {form | name = ""}
  73.  
  74. clearPassword form =
  75. {form | password = ""}
  76.  
  77. clearForm form =
  78. clearPassword (clearName form)
  79.  
  80. addFormToList model =
  81. { model | users = List.append model.users [model.form] }
  82.  
  83. updateF model form1 =
  84. { model | form = form1}
  85.  
  86. submitForm model =
  87. updateF (addFormToList model) (clearForm model.form)
  88.  
  89. -- VIEW
  90.  
  91.  
  92. view : Model -> Html Msg
  93. view model =
  94. div []
  95. [ input [ type' "text", placeholder "Name", onInput Name ] []
  96. , input [ type' "password", placeholder "Password", onInput Password ] []
  97. , button [ onClick Submit ] [ text "Submit"]
  98. , ul []
  99. (List.map (\user -> li [] [text user.name]) model.users)
  100. ]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement