Skip to content

Instantly share code, notes, and snippets.

@nashamri
Created December 24, 2017 13:27
Show Gist options
  • Save nashamri/45b08a0e967162a89d411ed0116cf687 to your computer and use it in GitHub Desktop.
Save nashamri/45b08a0e967162a89d411ed0116cf687 to your computer and use it in GitHub Desktop.
module Main exposing (..)
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (onInput, onClick)
import Random
import Random.List exposing (choose)
import Tuple exposing (first, second)
type alias Model =
{ names : List String, current : String, picked : String }
type Msg
= InputUpdate String
| Submit
| SendPick
| Pick ( Maybe String, List String )
init : ( Model, Cmd Msg )
init =
( { names = [], current = "", picked = "" }, Cmd.none )
view : Model -> Html Msg
view model =
div []
[ ul [] (List.map viewList model.names)
, input [ onInput InputUpdate, placeholder "Enter a name" ] []
, button [ onClick Submit ] [ text "Submit" ]
, viewPick model.names
, div [] [ h1 [] [ text model.picked ] ]
]
viewPick : List String -> Html Msg
viewPick list =
if (List.length list) > 1 then
button [ onClick SendPick ] [ text "Pick" ]
else
span [] []
viewList : String -> Html Msg
viewList list =
li []
[ span [] [ text list ]
]
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
InputUpdate userInput ->
( { model | current = userInput }, Cmd.none )
Submit ->
( { model | names = (model.names ++ [ model.current ]) }, Cmd.none )
SendPick ->
( model, Random.generate Pick (choose model.names) )
Pick randElem ->
let
e =
first randElem
in
case e of
Just e ->
( { model | picked = e }, Cmd.none )
Nothing ->
( model, Cmd.none )
main : Program Never Model Msg
main =
Html.program
{ init = init
, view = view
, update = update
, subscriptions = subscriptions
}
subscriptions : Model -> Sub Msg
subscriptions model =
Sub.none
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment