Created
December 1, 2016 17:19
-
-
Save ryan-haskell/40a4a983ea96b655aa16577ce501ad27 to your computer and use it in GitHub Desktop.
Get Lunch Suggestions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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