this is a lot of work...
This commit is contained in:
116
src/Wealth.elm
Normal file
116
src/Wealth.elm
Normal file
@@ -0,0 +1,116 @@
|
||||
module Wealth exposing (Model, Msg(..), editValue, init, update, view)
|
||||
|
||||
import Api.Player exposing (Wealth)
|
||||
import Html exposing (..)
|
||||
import Html.Attributes exposing (..)
|
||||
import Html.Events exposing (..)
|
||||
|
||||
|
||||
type Model
|
||||
= View
|
||||
| Edit String
|
||||
|
||||
|
||||
init =
|
||||
View
|
||||
|
||||
|
||||
view wealth model =
|
||||
div [ class "level-item" ]
|
||||
[ span [ class "icon is-large" ] [ i [ class "fas fa-2x fa-piggy-bank" ] [] ]
|
||||
, span [ class "icon", onClick StartEdit ] [ i [ class "fas fa-tools" ] [] ]
|
||||
]
|
||||
:: (case model of
|
||||
View ->
|
||||
viewWealth wealth
|
||||
|
||||
Edit amount ->
|
||||
viewUpdateWealth amount
|
||||
)
|
||||
|
||||
|
||||
viewUpdateWealth amount =
|
||||
[ input
|
||||
[ class "level-item"
|
||||
, class "input"
|
||||
, classList
|
||||
[ ( "is-danger", (not << isValid) amount )
|
||||
, ( "is-success", isValid amount )
|
||||
]
|
||||
, value amount
|
||||
, onInput AmountChanged
|
||||
]
|
||||
[]
|
||||
, button [ class "level-item button", onClick ConfirmEdit ] [ text "Ok" ]
|
||||
]
|
||||
|
||||
|
||||
viewWealth : Wealth -> List (Html Msg)
|
||||
viewWealth wealth =
|
||||
[ showWealthField "pp" <| String.fromInt wealth.pp
|
||||
, showWealthField "gp" <| String.padLeft 2 '0' <| String.fromInt wealth.gp
|
||||
, showWealthField "sp" <| String.fromInt wealth.sp
|
||||
, showWealthField "cp" <| String.fromInt wealth.cp
|
||||
]
|
||||
|
||||
|
||||
showWealthField : String -> String -> Html Msg
|
||||
showWealthField name value =
|
||||
div [ class "level-item" ]
|
||||
[ p [ class "has-text-right" ]
|
||||
[ strong [ class "heading is-marginless has-text-white" ] [ text name ]
|
||||
, span [ class <| "is-size-4" ] [ text value ]
|
||||
]
|
||||
]
|
||||
|
||||
|
||||
type Msg
|
||||
= StartEdit
|
||||
| QuitEdit
|
||||
| AmountChanged String
|
||||
| ConfirmEdit
|
||||
|
||||
|
||||
update : Msg -> Model -> ( Model, Maybe Float )
|
||||
update msg model =
|
||||
case msg of
|
||||
StartEdit ->
|
||||
( Edit "0.0", Nothing )
|
||||
|
||||
QuitEdit ->
|
||||
( View, Nothing )
|
||||
|
||||
AmountChanged newAmount ->
|
||||
( Edit <| String.replace "," "." newAmount
|
||||
, Nothing
|
||||
)
|
||||
|
||||
ConfirmEdit ->
|
||||
( View, editValue model )
|
||||
|
||||
|
||||
|
||||
-- Checks that the amount is a valid float
|
||||
|
||||
|
||||
isValid amount =
|
||||
case String.toFloat amount of
|
||||
Just _ ->
|
||||
True
|
||||
|
||||
Nothing ->
|
||||
False
|
||||
|
||||
|
||||
|
||||
-- Returns the edited value as a Float, if it exists
|
||||
|
||||
|
||||
editValue : Model -> Maybe Float
|
||||
editValue model =
|
||||
case model of
|
||||
View ->
|
||||
Nothing
|
||||
|
||||
Edit value ->
|
||||
String.toFloat value
|
||||
Reference in New Issue
Block a user