Skip to content

Commit

Permalink
Some cleanup plus ability to change color.
Browse files Browse the repository at this point in the history
  • Loading branch information
Bill Peregoy committed Mar 26, 2017
1 parent dc40f47 commit 14d97da
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions src/Main.elm
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ main =
type alias FlashElement =
{ id : Int
, text : String
, color : String
, expirationTime : Time
}

Expand All @@ -32,13 +33,14 @@ type alias Model =
, nextId : Int
, newText : String
, newDuration : Time
, newColor : String
, currentTime : Time
}


init : ( Model, Cmd Msg )
init =
Model [] 4 "" 0 0
Model [] 4 "" 0 "" 0
! []


Expand All @@ -47,21 +49,18 @@ init =


type Msg
= NoOp
| UpdateCurrentTime Time
= UpdateCurrentTime Time
| TimeoutFlashElements Time
| UpdateFormText String
| UpdateFormDuration String
| UpdateFormColor String
| CreateFlashElement
| DeleteFlashElement Time Int


update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
NoOp ->
model ! []

UpdateCurrentTime time ->
{ model | currentTime = time } ! []

Expand All @@ -85,14 +84,21 @@ update msg model =
in
{ model | newDuration = duration } ! []

UpdateFormColor color ->
{ model | newColor = color } ! []

CreateFlashElement ->
let
expirationTime =
model.currentTime
+ (model.newDuration * Time.second)

newFlashElement =
FlashElement model.nextId model.newText expirationTime
{ id = model.nextId
, text = model.newText
, color = model.newColor
, expirationTime = expirationTime
}

newList =
newFlashElement :: model.flashElements
Expand All @@ -102,6 +108,7 @@ update msg model =
, nextId = model.nextId + 1
, newDuration = 0
, newText = ""
, newColor = ""
}
! []

Expand Down Expand Up @@ -132,6 +139,8 @@ form model =
, input [ class "form-control", value model.newText, onInput UpdateFormText ] []
, label [] [ text "Timeout (seconds)" ]
, input [ class "form-control", value (durationString model.newDuration), onInput UpdateFormDuration ] []
, label [] [ text "Type (sucess, info, warning, danger)" ]
, input [ class "form-control", value model.newColor, onInput UpdateFormColor ] []
, button [ type_ "submit", class "btn btn-default", onClick CreateFlashElement ] [ text "Create" ]
]

Expand All @@ -144,7 +153,13 @@ flashView elements =

flashViewElements : List FlashElement -> List (Html Msg)
flashViewElements elements =
List.map (\elem -> div [ class "alert alert-warning" ] [ text elem.text ]) elements
List.map
(\elem ->
div
[ class ("alert alert-" ++ elem.color) ]
[ text elem.text ]
)
elements


view : Model -> Html Msg
Expand Down

0 comments on commit 14d97da

Please sign in to comment.