Skip to content

Instantly share code, notes, and snippets.

@freakingawesome
Created October 20, 2016 15:07
Show Gist options
  • Save freakingawesome/5b6cc5065a7bba9a3297d5f9ede50a52 to your computer and use it in GitHub Desktop.
Save freakingawesome/5b6cc5065a7bba9a3297d5f9ede50a52 to your computer and use it in GitHub Desktop.
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (..)
import Html.App exposing (program)
import Http
main =
program { init = init, view = view, update = update, subscriptions = \_ -> Sub.none }
type Msg
= NoOp
| FetchSucceed (List User)
| FetchError Http.Error
| UpdateTitle String
| SaveUser
init = { users = [], isLoading = False, newUserTitle = Nothing } ! []
type alias Model =
{ users : List User
, isLoading : Bool
, newUserTitle : Maybe String
}
type alias User =
{ title : String
, username : String
, email : String
}
update msg model =
case msg of
NoOp ->
( model, Cmd.none )
FetchSucceed newModel ->
( { model | users = newModel, isLoading = False }, Cmd.none )
FetchError _ ->
( { model | isLoading = False }, Cmd.none )
UpdateTitle newTitle ->
( { model | newUserTitle = Just newTitle }, Cmd.none )
SaveUser ->
case model.newUserTitle of
Nothing -> (model, Cmd.none)
Just title ->
( { model
| newUserTitle = Nothing
, users = model.users ++ [{ title = title, username = "", email = "" }]
}, Cmd.none)
view model =
div []
[ div [] (List.map displayRow model.users)
, formCreateUser model
]
formCreateUser model =
div []
[ input [ onInput UpdateTitle, placeholder "Title", value (Maybe.withDefault "" model.newUserTitle) ] []
, button [ onClick SaveUser ] [ text "Save" ]
]
displayRow {title} = div [] [ text title ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment