Skip to content

Instantly share code, notes, and snippets.

@ryannhg
Created Dec 1, 2016
Embed
What would you like to do?
Get Lunch Suggestions
import Html exposing (..)
import Html.Events exposing (..)
import Html.Attributes exposing (..)
import Random
main =
Html.program
{ init = init
, view = view
, update = update
, subscriptions = subscriptions
}
-- MODEL
type alias Location =
String
type alias Model =
{ location : Maybe Location
, locations : List Location
}
init : (Model, Cmd Msg)
init =
(Model Nothing initialLocations, Cmd.none)
initialLocations : List Location
initialLocations =
[ "Jimmy Johns"
, "Boars Head"
, "Chipotle"
, "Naf Naf Grill"
, "Roti"
, "Cosi"
, "Asada"
, "M Burger"
, "Halsted"
, "Blackwood BBQ"
, "Jason's Deli"
, "Epic Burger"
, "Cafecito"
, "Shamrock Club!!! LEzZZZGOOOO"
, "Monks"
, "Erik Carron's House"
]
-- UPDATE
type Msg
= GetNewLocation
| SetNewLocation Int
update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
case msg of
GetNewLocation ->
(model, Random.generate SetNewLocation (
Random.int
0
( (List.length model.locations) - 1)
)
)
SetNewLocation index ->
( { model | location = (getLocationFromIndex model.locations index) }
, Cmd.none
)
getLocationFromIndex : List Location -> Int -> Maybe Location
getLocationFromIndex locations index =
List.head ( List.drop index locations )
-- SUBSCRIPTIONS
subscriptions : Model -> Sub Msg
subscriptions model =
Sub.none
-- VIEW
view : Model -> Html Msg
view model =
div [ style
[ ("margin", "20px")
]
]
[ h1 [ style [("margin","0") ] ] [text "RAYN"]
, h2 [ style [("margin","0"), ("fontStyle","italic") ] ] [text "Ryan Assigns Your Nutrition"]
, viewLocation model.location
, button [ onClick GetNewLocation ] [ text "Get Restaurant" ]
]
viewLocation : Maybe Location -> Html Msg
viewLocation maybeLocation =
case maybeLocation of
Nothing ->
h3 [] [ text "Click the thing" ]
Just location ->
h3 [ style [("color","#960")] ] [ text location ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment