Skip to content

Instantly share code, notes, and snippets.

@adeonhy
Created June 19, 2019 07:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adeonhy/54a7a076bd704c246d8532baf5bbb147 to your computer and use it in GitHub Desktop.
Save adeonhy/54a7a076bd704c246d8532baf5bbb147 to your computer and use it in GitHub Desktop.
ねた
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