Skip to content

Instantly share code, notes, and snippets.

@ifyouseewendy
Created July 23, 2016 15:51
Show Gist options
  • Save ifyouseewendy/fc0d8e35e261538bc29997d41d6a5efc to your computer and use it in GitHub Desktop.
Save ifyouseewendy/fc0d8e35e261538bc29997d41d6a5efc to your computer and use it in GitHub Desktop.
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (..)
import Html.App as Html
-- model
type alias Model =
{
shelves : Int,
username : String
}
model : Model
model =
{ shelves = 2, username = "wendi" }
-- update
type Action =
Increment Int | Decrement
update : Action -> Model -> Model
update action model =
case action of
Increment quantity ->
{ model | shelves = model.shelves + quantity }
Decrement ->
{ model | shelves = Basics.max 0 (model.shelves - 1) }
-- _ ->
-- model
-- view
pluralize : String -> String -> Int -> String
pluralize singular plural quantity =
if quantity == 1 then
singular
else
plural
shelfButton caption action =
button [ onClick action ] [ text caption ]
view model =
let
isDisabled =
model.shelves <= 0
caption =
(toString model.shelves)
++ " "
++ (pluralize "shelf" "shelves" model.shelves)
in
div
[ class "content", id "main-body" ]
[
h1 [] [ text "Pluralizer"],
div [] [ shelfButton "Add a shelf" (Increment 10) ],
div [] [ shelfButton "Explode a shelf" Decrement ],
text caption
]
main =
Html.beginnerProgram
{ model = model, update = update, view = view }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment