-
Notifications
You must be signed in to change notification settings - Fork 18
/
SeatSaver.elm
81 lines (62 loc) · 1.63 KB
/
SeatSaver.elm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
module SeatSaver where
import Html exposing (..)
import Html.Attributes exposing (class)
import Html.Events exposing (onClick)
import StartApp.Simple
main : Signal Html
main =
StartApp.Simple.start
{ model = init
, update = update
, view = view
}
-- MODEL
type alias Seat =
{ seatNo : Int
, occupied : Bool
}
type alias Model =
List Seat
init : Model
init =
[ { seatNo = 1, occupied = False }
, { seatNo = 2, occupied = False }
, { seatNo = 3, occupied = False }
, { seatNo = 4, occupied = False }
, { seatNo = 5, occupied = False }
, { seatNo = 6, occupied = False }
, { seatNo = 7, occupied = False }
, { seatNo = 8, occupied = False }
, { seatNo = 9, occupied = False }
, { seatNo = 10, occupied = False }
, { seatNo = 11, occupied = False }
, { seatNo = 12, occupied = False }
]
-- UPDATE
type Action = Toggle Seat
update : Action -> Model -> Model
update action model =
case action of
Toggle seatToToggle ->
let
updateSeat seatFromModel =
if seatFromModel.seatNo == seatToToggle.seatNo then
{ seatFromModel | occupied = not seatFromModel.occupied }
else seatFromModel
in
List.map updateSeat model
-- VIEW
view : Signal.Address Action -> Model -> Html
view address model =
ul [ class "seats" ] (List.map (seatItem address) model)
seatItem : Signal.Address Action -> Seat -> Html
seatItem address seat =
let
occupiedClass =
if seat.occupied then "occupied" else "available"
in
li
[ class ("seat " ++ occupiedClass)
, onClick address (Toggle seat)
]
[ text (toString seat.seatNo) ]