Created
April 23, 2017 01:29
-
-
Save arecvlohe/378bddb288b284d5e4ec77ebf3357df3 to your computer and use it in GitHub Desktop.
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
module Main exposing (..) | |
import Html exposing (Html, text, div, button) | |
import Html.Events exposing (onClick) | |
import Random | |
import Maybe exposing (withDefault) | |
import List.Extra exposing (getAt) | |
-- MODEL | |
type alias Model = | |
{ author : String | |
, quote : String | |
} | |
init : ( Model, Cmd Msg ) | |
init = | |
( { author = "Adam", quote = "Elm is a nice language to use!" }, Cmd.none ) | |
quotes : List Model | |
quotes = | |
[ { author = "Victoria Justice", quote = "It's nice to just embrace the natural beauty within you." } | |
, { author = "Moises Arias", quote = "It is nice finding that place where you can just go and relax." } | |
, { author = "Yogi Berra", quote = "It ain't over till it's over" } | |
, { author = "Albert Einstein", quote = "Look deep into nature, and then you will understand everything better." } | |
] | |
defaultQuote: Model | |
defaultQuote = | |
{ author = "Adam", quote = "Elm is fun!" } | |
-- VIEW | |
view : Model -> Html Msg | |
view model = | |
div [] | |
[ div [] [ text model.author ] | |
, div [] [ text model.quote ] | |
, button [ onClick FetchQuote ] [ text "Fetch new quote" ] | |
] | |
-- UPDATE | |
type Msg | |
= NoOp | |
| FetchQuote | |
| NewQuote Int | |
update : Msg -> Model -> ( Model, Cmd Msg ) | |
update msg model = | |
case msg of | |
FetchQuote -> | |
( model, Random.generate NewQuote (Random.int 0 3) ) | |
NewQuote index -> | |
( withDefault defaultQuote (getAt index quotes), Cmd.none ) | |
NoOp -> | |
( model, Cmd.none ) | |
subscriptions : Model -> Sub Msg | |
subscriptions model = | |
Sub.none | |
-- MAIN | |
main : Program Never Model Msg | |
main = | |
Html.program | |
{ init = init | |
, update = update | |
, subscriptions = subscriptions | |
, view = view | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment