diff options
author | Guillermo Ramos | 2025-02-27 00:15:01 +0100 |
---|---|---|
committer | Guillermo Ramos | 2025-03-05 23:51:31 +0100 |
commit | 53ea8f5348c7717decbb559b1a41ad120146a6ed (patch) | |
tree | 0fecbedec742444a9de280b86d4960dc64a2069a /front/src | |
parent | 96926a82e592374971f5410bc1051a85f72506f7 (diff) | |
download | hiccup-53ea8f5348c7717decbb559b1a41ad120146a6ed.tar.gz |
Fix persistent simulation data
Diffstat (limited to 'front/src')
-rw-r--r-- | front/src/Main.elm | 59 |
1 files changed, 37 insertions, 22 deletions
diff --git a/front/src/Main.elm b/front/src/Main.elm index 1e54654..7da9ffa 100644 --- a/front/src/Main.elm +++ b/front/src/Main.elm @@ -63,7 +63,7 @@ quotaDecoder = (field "pending_principal" float) -type alias Simulation = +type alias FinanceSim = { history : List Quota , topay : Capital , payed : Capital @@ -71,15 +71,21 @@ type alias Simulation = } -simDecoder : Decoder Simulation +simDecoder : Decoder FinanceSim simDecoder = - map4 Simulation + map4 FinanceSim (field "history" (list quotaDecoder)) (field "topay" capitalDecoder) (field "payed" capitalDecoder) (field "payed_amortized" float) +type alias Simulation = + { financed : FinanceSim + , initial : Float + } + + type alias SimSpecsRaw = { titleTxt : String , totalValueTxt : String @@ -143,11 +149,11 @@ simSpecsToURL { principal, i1, years } = ] -runSimulation : SimSpecs -> Cmd Msg -runSimulation simSpecs = +runSimulation : Model -> SimSpecs -> Cmd Msg +runSimulation m simSpecs = Http.get { url = simSpecsToURL simSpecs - , expect = Http.expectJson GotSimulation simDecoder + , expect = Http.expectJson (GotSimulation m) simDecoder } @@ -200,7 +206,7 @@ convertInitialRate convert val totalValueTxt = type Msg - = GotSimulation (Result Http.Error Simulation) + = GotSimulation Model (Result Http.Error FinanceSim) | UpdateSimSpecs SimSpecUpdate String | RunSimulation SimSpecs | SetExpandedYears (Set Int) @@ -213,14 +219,22 @@ update msg model = Debug.log "UPDATE!" msg in case msg of - GotSimulation (Ok simulation) -> - ( { model | simulation = Just simulation }, Cmd.none ) + GotSimulation m (Ok financed) -> + ( { model + | simulation = + Just + { initial = Maybe.withDefault 0 <| String.toFloat m.simSpecsRaw.initialTxt + , financed = financed + } + } + , Cmd.none + ) - GotSimulation (Err err) -> + GotSimulation _ (Err err) -> ( { model | error = errorToString err }, Cmd.none ) RunSimulation specs -> - ( model, runSimulation specs ) + ( model, runSimulation model specs ) UpdateSimSpecs u val -> let @@ -233,7 +247,12 @@ update msg model = { simSpecsRaw | titleTxt = val } Principal -> - { simSpecsRaw | totalValueTxt = val } + { simSpecsRaw + | totalValueTxt = val + , initialTxt = + Maybe.withDefault simSpecsRaw.initialTxt <| + convertInitialRate rateToInitial simSpecsRaw.financedRateTxt val + } Rate -> { simSpecsRaw @@ -504,25 +523,21 @@ quotaView m { period, payed, pending_principal } = simView : Model -> Simulation -> Html Msg -simView m { history, topay, payed } = +simView m { initial, financed } = let - { totalValueTxt, initialTxt } = - m.simSpecsRaw + { history, topay, payed } = + financed total = - Round.round 2 <| - topay.principal - + topay.interest - + Maybe.withDefault 0 - (String.toFloat initialTxt) + topay.principal + topay.interest + initial in div [] [ hr [ class "my-5" ] [] , div [] [ text "To pay: " - , text total + , text <| Round.round 2 total , text " (" - , text initialTxt + , text <| Round.round 2 initial , text " initial + " , capitalSumView topay , text " financed)" |