dckesler
12/11/2015 - 3:15 AM

NaughtyNice.elm

module Main where

import Html exposing (div, text, input, span, Html)
import Html.Attributes exposing (placeholder, value)
import Html.Events exposing (on, targetValue, onKeyUp)
import StartApp.Simple as StartApp
import Signal exposing (Address)

-- # Main

main =
  StartApp.start { model = model, view = view, update = update }

-- # Model

type alias Model = 
  { niceItem : String
  , niceList: List String
  , badItem: String
  , badList: List String
  }
model : Model
model =
  { niceItem = ""
  , niceList = []
  , badItem = ""
  , badList = []
  }

-- # Actions

update : Action -> Model -> Model
update action model =
  case action of
    NoOp -> model
    UpdateString listName s -> if listName == Bad
      then { model | badItem = s }
      else { model | niceItem = s }
    EnterAction listName -> if listName == Bad
      then { model | badList = model.badItem::model.badList, badItem = "" }
      else { model | niceList = model.niceItem::model.niceList, niceItem = "" }

-- # View
type InputListName = Bad | Nice
type Action = NoOp | UpdateString InputListName String | EnterAction InputListName

view : Address Action -> Model -> Html
view address model =
  div []
  [
    input 
      [ placeholder "Nice List..."
      , value model.niceItem
      , on "input" targetValue ((Signal.message address) << UpdateString Nice)
      , onKeyUp address ( \code -> if code == 13 then EnterAction Nice else NoOp)
      ]
      []
  , div []
      (List.map (\n -> div [] [text n]) model.niceList)
  , input 
      [ placeholder "Bad List..."
      , value model.badItem
      , on "input" targetValue ((Signal.message address) << UpdateString Bad)
      , onKeyUp address ( \code -> if code == 13 then EnterAction Bad else NoOp)
      ]
      []
  , div []
      (List.map (\n -> div [] [text n]) model.badList)
  ]