Last active
June 2, 2016 15:23
-
-
Save kristiannissen/fefe5e4158abfcbd472c6286bdbc1bb7 to your computer and use it in GitHub Desktop.
ELM based ABV calculator
This file contains hidden or 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.App as Html | |
import Html.Attributes exposing (..) | |
import Html.Events exposing (..) | |
import String | |
-- MAIN | |
main = | |
Html.beginnerProgram { model = model, view = view, update = update } | |
-- MODEL | |
type alias Model = | |
{og : Float | |
, fg : Float | |
, abv : Float | |
} | |
model : Model | |
model = | |
{ og = 1.055 | |
, fg = 1.015 | |
, abv = 0.00 | |
} | |
-- UPDATE | |
type Msg = Calculate | |
| ChangeOg String | |
| ChangeFg String | |
update : Msg -> Model -> Model | |
update msg model = | |
case msg of | |
Calculate -> | |
{ model | abv = (model.og - model.fg) * 131.25 } | |
ChangeOg val -> | |
{ model | og = Result.withDefault 0.000 (String.toFloat val) } | |
ChangeFg val -> | |
{ model | fg = Result.withDefault 0.000 (String.toFloat val) } | |
-- VIEW | |
view : Model -> Html Msg | |
view model = | |
div [] [ | |
div [] [ text (String.append (String.left 4 (toString model.abv)) "% ABV") ] | |
, input [ placeholder "Enter Original Gravety", onInput ChangeOg ] [] | |
, input [ placeholder "Enter Final Gravety", onInput ChangeFg ] [] | |
, button [ onClick Calculate ][ text "Calculate" ] | |
, viewValidation model | |
] | |
viewValidation : Model -> Html msg | |
viewValidation model = | |
let | |
(color, message) = | |
if model.fg <= 0.000 then | |
("red", "FG is wrong") | |
else if model.og <= 0.000 then | |
("red", "OG is wrong") | |
else | |
("", "") | |
in | |
div [ style [("color", color)] ] [ text message ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment