diff --git a/rosstuck-elm/README.md b/rosstuck-elm/README.md new file mode 100644 index 0000000..cb7a00a --- /dev/null +++ b/rosstuck-elm/README.md @@ -0,0 +1 @@ +Install elm. Run "elm-reactor" and go to localhost:8000 diff --git a/rosstuck-elm/elm-package.json b/rosstuck-elm/elm-package.json new file mode 100644 index 0000000..872b990 --- /dev/null +++ b/rosstuck-elm/elm-package.json @@ -0,0 +1,16 @@ +{ + "version": "1.0.0", + "summary": "DomCode sample raffler", + "repository": "https://github.com/domcode/rafflers.git", + "license": "MIT", + "source-directories": [ + "." + ], + "exposed-modules": [], + "dependencies": { + "elm-community/random-extra": "2.0.0 <= v < 3.0.0", + "elm-lang/core": "5.1.1 <= v < 6.0.0", + "elm-lang/html": "2.0.0 <= v < 3.0.0" + }, + "elm-version": "0.18.0 <= v < 0.19.0" +} diff --git a/rosstuck-elm/test.elm b/rosstuck-elm/test.elm new file mode 100644 index 0000000..88b5cac --- /dev/null +++ b/rosstuck-elm/test.elm @@ -0,0 +1,60 @@ +import Html exposing (h2, textarea, br, button, ul, li, text, div, Html) +import Html.Events exposing (onClick, onInput) +import List exposing (map) +import String exposing (split, trim) +import Random +import Random.Extra exposing (sample) + +main = + Html.program + { init = init + , view = view + , update = update + , subscriptions = \_ -> Sub.none + } + +type alias Name = String +type alias Names = List Name +type alias Model = + { contestants : Names + , winner : Maybe Name + } + +init : (Model, Cmd msg) +init = (Model [] Nothing, Cmd.none) + +type Msg = GatherNames Names | StartRaffle | PickWinner (Maybe Name) + +update : Msg -> Model -> (Model, Cmd Msg) +update msg model = + case msg of + GatherNames allNames -> + ({model | contestants = allNames, winner = Nothing}, Cmd.none) + StartRaffle -> + (model, Random.generate PickWinner (sample model.contestants)) + PickWinner nameOfWinner -> + ({model | winner = nameOfWinner}, Cmd.none) + +view : Model -> Html Msg +view model = + div [] + [ h2 [] [text "DomCode Elm Raffler"] + , textarea [onInput splitNames] [] + , br [] [] + , button [onClick StartRaffle] [text "And the winner is..."] + , br [] [] + , showWinner model + ] + +splitNames : String -> Msg +splitNames input = + if (trim input == "") then + GatherNames [] + else + GatherNames (split "\n" (trim input)) + +showWinner : Model -> Html Msg +showWinner model = + case model.winner of + Just winner -> text winner + Nothing -> text "No Winner yet"