aboutsummaryrefslogtreecommitdiff
path: root/front
diff options
context:
space:
mode:
authorGuillermo Ramos2025-03-16 11:20:15 +0100
committerGuillermo Ramos2025-03-16 13:18:14 +0100
commitb1709107d2a94eff5b05037d52e7b53aec9c7598 (patch)
tree2f6efa9ea876c4faaf22a9123f25183d191c1f84 /front
parent88cca41114a04f7cb6e8bda9ce72b836dbbddf6e (diff)
downloadhiccup-b1709107d2a94eff5b05037d52e7b53aec9c7598.tar.gz
Delete updates
Diffstat (limited to 'front')
-rw-r--r--front/src/Main.elm96
1 files changed, 71 insertions, 25 deletions
diff --git a/front/src/Main.elm b/front/src/Main.elm
index 4b7692f..bc0b050 100644
--- a/front/src/Main.elm
+++ b/front/src/Main.elm
@@ -376,13 +376,13 @@ periodicUpdateDecoder =
type alias SimUpdates =
- { periodically : List PeriodicUpdate
+ { periodic : List PeriodicUpdate
, byMonth : List ( Int, SimUpdate )
}
defaultSimUpdates =
- { periodically = [], byMonth = [] }
+ { periodic = [], byMonth = [] }
simUpdatesParser : UQ.Parser SimUpdates
@@ -450,7 +450,7 @@ simUpdatesParser =
simUpdatesToQS : SimUpdates -> List UB.QueryParameter
-simUpdatesToQS { periodically, byMonth } =
+simUpdatesToQS { periodic, byMonth } =
let
renderUpdate u =
case u of
@@ -473,26 +473,26 @@ simUpdatesToQS { periodically, byMonth } =
renderMonth ( m, u ) =
String.join ":" [ String.fromInt m, renderUpdate u ]
in
- List.map (UB.string "u_period" << renderPeriod) periodically
+ List.map (UB.string "u_period" << renderPeriod) periodic
++ List.map (UB.string "u_bymon" << renderMonth) byMonth
updatesInMonth : SimUpdates -> Int -> SimUpdates
-updatesInMonth { periodically, byMonth } month =
+updatesInMonth { periodic, byMonth } month =
let
newPeriodically =
- List.filter (periodicUpdateInMonth month) periodically
+ List.filter (periodicUpdateInMonth month) periodic
newByMonth =
List.filter (\( m, updates ) -> m == month) byMonth
in
- { periodically = newPeriodically, byMonth = newByMonth }
+ { periodic = newPeriodically, byMonth = newByMonth }
simUpdatesEncode : SimUpdates -> JE.Value
-simUpdatesEncode { periodically, byMonth } =
+simUpdatesEncode { periodic, byMonth } =
JE.object
- [ ( "periodically", JE.list periodicUpdateEncode periodically )
+ [ ( "periodic", JE.list periodicUpdateEncode periodic )
, ( "by_month"
, JE.object <|
List.map (\( m, us ) -> ( String.fromInt m, simUpdateEncode us )) byMonth
@@ -503,7 +503,7 @@ simUpdatesEncode { periodically, byMonth } =
simUpdatesDecoder : JD.Decoder SimUpdates
simUpdatesDecoder =
JD.map2 SimUpdates
- (JD.field "periodically" (JD.list periodicUpdateDecoder))
+ (JD.field "periodic" (JD.list periodicUpdateDecoder))
(JD.field "by_month"
(JD.keyValuePairs simUpdateDecoder
|> JD.map
@@ -683,6 +683,15 @@ type alias Model =
}
+setModelUpdates : SimUpdates -> Model -> Model
+setModelUpdates newUpdates m =
+ let
+ rawSpecs =
+ m.rawSpecs
+ in
+ { m | rawSpecs = { rawSpecs | updates = newUpdates } }
+
+
type Route
= NotFound
| Root ( Settings, RawSpecs )
@@ -801,6 +810,8 @@ type Msg
| RunSim SimSpecs
| GotSim Model (Result Http.Error MortgageSim)
| SetExpandedYears (Set Int)
+ | RmPeriodicUpdate PeriodicUpdate
+ | RmUpdate ( Int, SimUpdate )
update : Msg -> Model -> ( Model, Cmd Msg )
@@ -845,6 +856,32 @@ update msg m =
, Cmd.none
)
+ RmPeriodicUpdate pu ->
+ let
+ updates =
+ m.rawSpecs.updates
+
+ periodicUpdates =
+ updates.periodic
+
+ newM =
+ setModelUpdates { updates | periodic = List.filter ((/=) pu) periodicUpdates } m
+ in
+ ( newM, Nav.pushUrl m.navKey (modelToUrl newM) )
+
+ RmUpdate mu ->
+ let
+ updates =
+ m.rawSpecs.updates
+
+ byMonUpdates =
+ updates.byMonth
+
+ newM =
+ setModelUpdates { updates | byMonth = List.filter ((/=) mu) byMonUpdates } m
+ in
+ ( newM, Nav.pushUrl m.navKey (modelToUrl newM) )
+
UpdateSpecs field val ->
let
rawSpecs =
@@ -1162,14 +1199,24 @@ monthToYear month =
((month - 1) // 12) + 1
-simUpdateView : List (Attribute Msg) -> Model -> SimUpdate -> Html Msg
-simUpdateView attrs m upd =
- case upd of
- Amortize f ->
- p attrs [ text "+", amountView [] m.settings.currency f ]
+simUpdateView : List (Attribute Msg) -> Model -> SimUpdate -> Msg -> Html Msg
+simUpdateView attrs m upd onClick =
+ let
+ els =
+ case upd of
+ Amortize f ->
+ [ text "+", amountView [] m.settings.currency f ]
- SetI1 f ->
- p attrs [ text <| String.fromFloat (f * 100), text "%" ]
+ SetI1 f ->
+ [ text <| String.fromFloat (f * 100), text "%" ]
+ in
+ p (attrs ++ [ class "bg-lime-200 m-1" ])
+ (els
+ ++ [ text " "
+ , span (clickableAttrs onClick ++ [ class "text-red-600" ])
+ [ text "×" ]
+ ]
+ )
quotaView : Model -> MortgageSim -> Quota -> Html Msg
@@ -1191,6 +1238,12 @@ quotaView m { updates } { month, payed, pending_principal } =
else
( "+ ", Set.insert year m.expandedYears )
+ periodicUpdates =
+ List.map (\pu -> simUpdateView [] m pu.upd (RmPeriodicUpdate pu)) monthUpdates.periodic
+
+ byMonUpdates =
+ List.map (\mu -> simUpdateView [] m (Tuple.second mu) (RmUpdate mu)) monthUpdates.byMonth
+
( yearField, updatesField ) =
if modBy 12 (month - 1) == 0 then
( div []
@@ -1201,14 +1254,7 @@ quotaView m { updates } { month, payed, pending_principal } =
)
else
- ( text ""
- , div []
- (List.map (simUpdateView [ class "bg-lime-200" ] m << .upd) monthUpdates.periodically
- ++ (List.map (simUpdateView [ class "bg-lime-200" ] m) <|
- List.map Tuple.second monthUpdates.byMonth
- )
- )
- )
+ ( text "", div [] (periodicUpdates ++ byMonUpdates) )
in
if modBy 12 (month - 1) == 0 || monthInExpandedYear then
tr []