Skip to content

Instantly share code, notes, and snippets.

@amencarini
Last active August 6, 2016 10:50
Show Gist options
  • Save amencarini/0ce67494f52097fbe84a6b6ad59a4650 to your computer and use it in GitHub Desktop.
Save amencarini/0ce67494f52097fbe84a6b6ad59a4650 to your computer and use it in GitHub Desktop.
SeatSaver
module Seat exposing (Model, Msg, view, init, update)
import Html exposing (Html, text, li)
import Html.Attributes exposing (class)
import Html.Events exposing (onClick)
-- MODEL
type alias Model =
{ seatNo: Int
, occupied: Bool
}
init : Int -> Model
init seatNo =
{ seatNo = seatNo, occupied = False }
-- VIEW
view : Model -> Html Msg
view seat =
let
occupiedClass =
if seat.occupied then "occupied" else "available"
in
li
[ class ("seat " ++ occupiedClass)
, onClick Toggle
]
[ text (toString seat.seatNo) ]
-- UPDATE
type Msg
= Toggle
update : Msg -> Model -> Model
update msg seat =
case msg of
Toggle ->
{ seat | occupied = not seat.occupied }
module SeatSaver exposing (..)
import Html exposing (Html, ul)
import Html.App
import Html.Attributes exposing (class)
import Seat
-- MODEL
type alias Model =
List Seat.Model
init : Model
init =
List.map Seat.init [1..16]
-- VIEW
view : Model -> Html Msg
view model =
ul [ class "seats" ] (List.map viewSeat model)
viewSeat : Seat.Model -> Html Msg
viewSeat seat =
Html.App.map (UpdateSeat seat) (Seat.view seat)
-- UPDATE
type Msg =
UpdateSeat Seat.Model Seat.Msg
update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
case msg of
UpdateSeat seat msg ->
((List.map (updateHelp seat msg) model), Cmd.none)
updateHelp : Seat.Model -> Seat.Msg -> Seat.Model -> Seat.Model
updateHelp targetSeat msg seat =
if targetSeat.seatNo == seat.seatNo then Seat.update msg seat else seat
-- SUBSCRIPTIONS
subscriptions : Model -> Sub Msg
subscriptions model =
Sub.none
-- MAIN
main : Program Never
main =
Html.App.program
{ init = (init, Cmd.none)
, view = view
, update = update
, subscriptions = subscriptions
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment