Skip to content

Instantly share code, notes, and snippets.

@dadhi
Last active September 16, 2016 09:38
Show Gist options
  • Save dadhi/d50687bf08a4b00211d9219688688be9 to your computer and use it in GitHub Desktop.
Save dadhi/d50687bf08a4b00211d9219688688be9 to your computer and use it in GitHub Desktop.
Small elm app for civilian / in-army people tracking
import Html exposing (..)
import Html.App as Html
import Html.Attributes exposing (..)
import Html.Events exposing (..)
import String as String
main =
Html.beginnerProgram
{ model = model
, update = update
, view = view
}
type PersonStatus = Civilian | Army
type alias Person =
{ status : PersonStatus
, name : String
, id : Int
}
newPerson {personName, personId} =
Person Civilian personName personId
model =
{ personName = ""
, personId = 0
, people = []
}
type Action
= NewName String
| AddNewCivilian
| CivilianToArmy Int
--| ArmyToCivilian
--| RemoveDead
update action model =
case action of
NewName name ->
{ model | personName = name}
AddNewCivilian ->
{ model
| personId = model.personId + 1
, people = (newPerson model)::model.people}
CivilianToArmy id ->
let updatePerson id person =
if id == person.id then
{ person | status = Army }
else
person
in
{ model | people = List.map (updatePerson id) model.people}
view model =
div []
[ h2 [] [ text "People" ]
, div [] (List.map viewPerson model.people)
, input [ placeholder "Name", onInput NewName] []
, button [
onClick AddNewCivilian,
disabled (String.length model.personName == 0) ]
[ text "add civilian" ]
]
viewPerson {status, name, id} =
div []
[ text ((toString status) ++ " #" ++ (toString id) ++ " " ++ name)
, viewPersonControls status id
]
viewPersonControls status id =
case status of
Civilian ->
button [ onClick (CivilianToArmy id) ]
[ text "to army" ]
Army ->
span [] []
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment