Skip to content

Instantly share code, notes, and snippets.

@zkessin
Created November 13, 2016 11:57
Show Gist options
  • Save zkessin/3cb6ef83cc1f55f24f9ea935d2801d7c to your computer and use it in GitHub Desktop.
Save zkessin/3cb6ef83cc1f55f24f9ea935d2801d7c to your computer and use it in GitHub Desktop.
module PivotApp exposing (..)
{-
********************************************************************************
Copyright 2016 Zachary Kessin
Released under the BSD3 licence
Test your Code for better code quiality
http://elm-test.com/?utm_source=gist&utm_content=template
For Elm 0.17
********************************************************************************
-}
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.App
import Html.Events exposing (..)
import Pivot exposing (Pivot)
type alias Special =
{ itemName : String
, link : String
, imageUrl : String
}
type alias InitData =
List Special
type alias Model =
{ specials : Pivot Special
}
type Msg
= Next
| Prev
init : List Special -> ( Model, Cmd Msg )
init specials =
case Pivot.fromList specials of
Just pivot ->
( Model pivot, Cmd.none )
Nothing ->
Debug.crash "Special List is Empty!"
update : Msg -> Model -> ( Model, Cmd Msg )
update event model =
case event of
Next ->
{ model | specials = Pivot.withRollback Pivot.goR model.specials } ! []
Prev ->
{ model | specials = Pivot.withRollback Pivot.goL model.specials } ! []
view : Model -> Html Msg
view m =
div []
[ if Pivot.hasL m.specials then
button [ onClick Prev ] [ text "Prev" ]
else
empty
, displaySpecial <| Pivot.getC m.specials
, if Pivot.hasR m.specials then
button [ onClick Next ] [ text "Next" ]
else
empty
]
empty : Html a
empty =
div [] [ text "" ]
displaySpecial : Special -> Html Msg
displaySpecial sp =
div
[]
[ a [ href sp.link ]
[ img [ src sp.imageUrl ] []
, div [] [ text sp.itemName ]
]
]
subscriptions : Model -> Sub Msg
subscriptions model =
Sub.batch []
main : Program InitData
main =
let
params =
{ init = init
, update = update
, view = view
, subscriptions = subscriptions
}
in
Html.App.programWithFlags params
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment