aboutsummaryrefslogtreecommitdiff
path: root/front/src
diff options
context:
space:
mode:
authorGuillermo Ramos2025-02-21 17:50:00 +0100
committerGuillermo Ramos2025-02-21 18:53:50 +0100
commitbaf346bbbdd04e08c686252242ea0a87a1a07d66 (patch)
tree6e09e5e7579cbb0f5ec22e19f58bbb1fb456aba3 /front/src
parent0ce76284445bb2eace7a399140a7d0e2a0bc01af (diff)
downloadhiccup-baf346bbbdd04e08c686252242ea0a87a1a07d66.tar.gz
Simulation in table
Diffstat (limited to 'front/src')
-rw-r--r--front/src/Main.elm88
1 files changed, 56 insertions, 32 deletions
diff --git a/front/src/Main.elm b/front/src/Main.elm
index 7f6522b..18e13b6 100644
--- a/front/src/Main.elm
+++ b/front/src/Main.elm
@@ -1,13 +1,13 @@
module Main exposing (main)
-import Round
import Browser
-import Html exposing (Html, button, div, input, text, hr)
-import Html.Attributes exposing (max, min, step, type_, value, disabled)
+import Html exposing (Html, button, div, hr, input, span, table, tbody, td, text, th, thead, tr)
+import Html.Attributes exposing (class, disabled, max, min, step, style, title, type_, value)
import Html.Events exposing (onClick, onInput)
import Http
import Json.Decode exposing (Decoder, field, float, int, list, map, map2, map3, map4)
import Json.Encode as Encode exposing (Value, object)
+import Round
@@ -35,16 +35,6 @@ capitalDecoder =
(field "interest" float)
-capitalSumStr : Capital -> String
-capitalSumStr { principal, interest } =
- Round.round 2 (principal + interest)
-
-
-capitalStr : Capital -> String
-capitalStr { principal, interest } =
- String.concat [ "{principal=", Round.round 2 principal, ", interest=", Round.round 2 interest, "}" ]
-
-
type alias Quota =
{ period : Int
, payed : Capital
@@ -83,12 +73,16 @@ type alias SimSpecsTxt =
, yearsTxt : String
}
+
simSpecsParse : SimSpecsTxt -> Maybe SimSpecs
simSpecsParse { principalTxt, i1Txt, yearsTxt } =
- case (String.toFloat principalTxt, String.toFloat i1Txt, String.toInt yearsTxt) of
- (Just principal, Just i1, Just years) ->
+ case ( String.toFloat principalTxt, String.toFloat i1Txt, String.toInt yearsTxt ) of
+ ( Just principal, Just i1, Just years ) ->
Just { principal = principal, i1 = i1, years = years }
- _ -> Nothing
+
+ _ ->
+ Nothing
+
type alias SimSpecs =
{ principal : Float
@@ -175,7 +169,7 @@ update msg model =
( { model | error = errorToString err }, Cmd.none )
RunSimulation specs ->
- (model, runSimulation specs)
+ ( model, runSimulation specs )
UpdateSimSpecs u val ->
let
@@ -228,12 +222,19 @@ errorToString error =
specsView : SimSpecsTxt -> Html Msg
specsView simSpecsTxt =
let
- { principalTxt, i1Txt, yearsTxt } = simSpecsTxt
- simSpecs = simSpecsParse simSpecsTxt
- simButtonAttrs = case simSpecs of
- Nothing -> [ disabled True ]
- Just specs -> [ onClick (RunSimulation specs) ]
+ { principalTxt, i1Txt, yearsTxt } =
+ simSpecsTxt
+
+ simSpecs =
+ simSpecsParse simSpecsTxt
+
+ simButtonAttrs =
+ case simSpecs of
+ Nothing ->
+ [ disabled True ]
+ Just specs ->
+ [ onClick (RunSimulation specs) ]
in
div []
[ div []
@@ -276,26 +277,49 @@ specsView simSpecsTxt =
]
+historyView : List Quota -> Html Msg
+historyView quotas =
+ let
+ titles =
+ [ "Month", "Quota", "Pending" ]
+
+ head =
+ thead [] [ tr [] (List.map (\t -> th [ class "px-3 py-1 border border-gray-300" ] [ text t ]) titles) ]
+ in
+ table [ class "border border-collapse border-gray-400" ] [ head, tbody [] (List.map quotaView quotas) ]
+
+
+capitalSumView : Capital -> Html Msg
+capitalSumView { principal, interest } =
+ let
+ partsTitle =
+ String.concat [ "Principal: ", Round.round 2 principal, "\nInterest: ", Round.round 2 interest ]
+ in
+ span [ class "underline", title partsTitle ] [ text (Round.round 2 (principal + interest)) ]
+
+
quotaView : Quota -> Html Msg
quotaView { period, payed, pending_principal } =
if modBy 12 period == 0 then
- div [] [ text (String.join "\t" [ String.fromInt period, capitalSumStr payed, capitalStr payed, Round.round 2 pending_principal ]) ]
+ tr []
+ (List.map (\t -> td [ class "px-3 py-1 border border-gray-300" ] [ t ])
+ [ text
+ (String.fromInt period)
+ , capitalSumView payed
+ , text (Round.round 2 pending_principal)
+ ]
+ )
+
else
text ""
-historyView : List Quota -> Html Msg
-historyView quotas =
- div [] (List.map quotaView quotas)
-
simView : Simulation -> Html Msg
simView { history, topay, payed } =
div []
- [ hr [] []
- , historyView history
- , hr [] []
- , div [] [ text (String.concat [ "to pay: ", capitalSumStr topay, " (", capitalStr topay, ")" ]) ]
- , div [] [ text (String.concat [ "payed: ", capitalSumStr payed, " (", capitalStr payed, ")" ]) ]
+ [ historyView history
+ , div [] [ text "to pay: ", capitalSumView topay ]
+ , div [] [ text "payed: ", capitalSumView payed ]
]