145 lines
3.3 KiB
Elm
145 lines
3.3 KiB
Elm
module Wealth exposing (Model, Msg(..), editValue, init, update, view)
|
|
|
|
import Api.Player exposing (Wealth)
|
|
import Bulma as B
|
|
import Html exposing (..)
|
|
import Html.Attributes exposing (..)
|
|
import Html.Events exposing (..)
|
|
|
|
|
|
type Model
|
|
= View
|
|
| Edit String
|
|
|
|
|
|
init =
|
|
View
|
|
|
|
|
|
view : Wealth -> Model -> List (Html Msg)
|
|
view wealth model =
|
|
div [ class "level-item" ]
|
|
[ span [ class "icon is-large" ] [ i [ class "fas fa-2x fa-piggy-bank" ] [] ] ]
|
|
:: (case model of
|
|
View ->
|
|
viewWealth wealth
|
|
++ [ div [ class "level-item button is-small is-dark" ]
|
|
[ span
|
|
[ class "icon is-small"
|
|
, onClick StartEdit
|
|
]
|
|
[ i [ class "fas fa-tools" ] [] ]
|
|
]
|
|
]
|
|
|
|
Edit amount ->
|
|
viewUpdateWealth amount
|
|
)
|
|
|
|
|
|
inBarButton =
|
|
class "button is-small is-outlined "
|
|
|
|
|
|
viewUpdateWealth amount =
|
|
[ input
|
|
[ class "level-item"
|
|
, class "input has-text-right has-background-dark has-text-white"
|
|
, classList
|
|
[ ( "is-danger", (not << isValid) amount )
|
|
, ( "is-success", isValid amount )
|
|
]
|
|
, value amount
|
|
, style "width" "8em"
|
|
, onInput AmountChanged
|
|
]
|
|
[]
|
|
, button
|
|
[ inBarButton
|
|
, class "level-item is-primary"
|
|
, onClick ConfirmEdit
|
|
]
|
|
[ B.icon
|
|
{ icon =
|
|
if isValid amount then
|
|
"fas fa-check"
|
|
|
|
else
|
|
"fas fa-times"
|
|
, ratio = Nothing
|
|
, size = Nothing
|
|
}
|
|
]
|
|
]
|
|
|
|
|
|
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 "", 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
|