Skip to content

Instantly share code, notes, and snippets.

@adeonhy
Created Jun 19, 2019
Embed
What would you like to do?
ねた
module Main exposing (Model, Msg(..), init, main, update, view)
import Browser
import Html exposing (Html, button, div, input, p, text)
import Html.Attributes exposing (class, href, placeholder, src, style, type_)
import Html.Events exposing (onClick, onInput)
import Time exposing (Posix, Zone, utc)
-- DATA and TYPE
main =
Browser.element
{ init = init
, update = update
, subscriptions = subscriptions
, view = view
}
-- MODEL
type alias Model =
{ dailySalary : Maybe Int
, elaspedSec : Int
, salaryInputValue : String
}
initialModel =
{ dailySalary = Nothing
, elaspedSec = 0
, salaryInputValue = ""
}
init : () -> ( Model, Cmd Msg )
init _ =
( initialModel, Cmd.none )
-- UPDATE
type Msg
= Tick Time.Posix
| InputSalary String
| SubmitSalary
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
Tick _ ->
( { model | elaspedSec = model.elaspedSec + 1 }, Cmd.none )
SubmitSalary ->
( { model | elaspedSec = 0, dailySalary = String.toInt model.salaryInputValue }, Cmd.none )
InputSalary salary ->
( { model | salaryInputValue = salary }, Cmd.none )
-- SUBSCRIPTIONS
subscriptions : Model -> Sub Msg
subscriptions model =
Sub.batch
[ Time.every 1000 Tick ]
-- VIEW
view : Model -> Html Msg
view model =
div [ style "text-align" "center" ]
[ case model.dailySalary of
Nothing ->
viewForm
Just salary ->
viewOchingin model
]
viewLoading =
p [] [ text "Loading..." ]
viewForm : Html Msg
viewForm =
div []
[ p [] [ text "日給を入力してね" ]
, input [ type_ "number", placeholder "10000", onInput InputSalary ] []
, button [ onClick SubmitSalary ] [ text "Submit" ]
]
viewOchingin : Model -> Html Msg
viewOchingin model =
div []
[ p [] [ text "現在のおちんぎんは" ]
, p [] [ text <| String.fromFloat <| calcOchingin model ]
]
calcOchingin : Model -> Float
calcOchingin model =
let
dailyTotalSec =
toFloat <| 60 * 60 * 8
floatDailySalary =
toFloat <| Maybe.withDefault 0 model.dailySalary
ochinginPerSec =
floatDailySalary / dailyTotalSec
in
ochinginPerSec * toFloat model.elaspedSec
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment