aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGuillermo Ramos2025-03-16 20:13:49 +0100
committerGuillermo Ramos2025-03-23 15:02:36 +0100
commit0e1b22d5583064a1f62ea5c0cb90b0a1c8ffce74 (patch)
tree7de97c8889d51159e44d9b4384775c4096aa2a13
parent274e67b2021d23e036a76b9ae947e8dace0c2de1 (diff)
downloadhiccup-0e1b22d5583064a1f62ea5c0cb90b0a1c8ffce74.tar.gz
Periodic updates UI
-rw-r--r--front/src/Main.elm163
-rw-r--r--src/lib.rs4
2 files changed, 118 insertions, 49 deletions
diff --git a/front/src/Main.elm b/front/src/Main.elm
index dfdc55a..8b5dc16 100644
--- a/front/src/Main.elm
+++ b/front/src/Main.elm
@@ -135,6 +135,12 @@ make_t lang str =
"Interest:" ->
"Interés:"
+ "Period:" ->
+ "Periodo:"
+
+ "To:" ->
+ "Hasta:"
+
"Cancel" ->
"Cancelar"
@@ -375,9 +381,9 @@ periodicUpdateInMonth : Int -> PeriodicUpdate -> Bool
periodicUpdateInMonth month { period, from, to } =
let
base =
- Maybe.withDefault 0 from
+ Maybe.withDefault 1 from
in
- modBy period month == base && base <= month && Maybe.withDefault (month + 1) to > month
+ modBy period (month - base) == 0 && base <= month && Maybe.withDefault month to >= month
periodicUpdateEncode : PeriodicUpdate -> JE.Value
@@ -543,28 +549,6 @@ simUpdatesDecoder =
(JD.field "by_month" (JD.list monthUpdateDecoder))
-
--- (JD.list JD.string
--- |> JD.map
--- (\( m, u ) ->
--- ( Maybe.withDefault 0
--- (String.toInt
--- m
--- )
--- , simUpdateDecoder u
--- )
--- )
--- )
--- )
--- )
--- (simUpdateDecoder
--- >> JD.map
--- (List.map (\( k, v ) -> ( Maybe.withDefault 0 (String.toInt k), v )))
--- )
--- )
--- )
-
-
type alias SimSpecs =
{ principal : Float
, i1 : Float
@@ -724,7 +708,13 @@ settingsToQS { lang, currency } =
type Editing
- = AddUpdate { type_ : SimUpdate, txt : String, month : Int, error : String }
+ = AddUpdate
+ { type_ : SimUpdate
+ , txt : String
+ , month : Int
+ , error : String
+ , periodic : Maybe { period : String, to : String }
+ }
type alias Model =
@@ -1024,6 +1014,12 @@ update msg m =
CommitEditing ->
case m.editing of
Just (AddUpdate au) ->
+ let
+ err =
+ ( { m | editing = Just (AddUpdate { au | error = m.t "Invalid input" }) }
+ , Cmd.none
+ )
+ in
case String.toFloat au.txt of
Just f ->
let
@@ -1038,21 +1034,45 @@ update msg m =
updates =
m.rawSpecs.updates
- byMonth =
- ( au.month, u ) :: updates.byMonth
+ newUpdates =
+ case au.periodic of
+ Nothing ->
+ Just { updates | byMonth = ( au.month, u ) :: updates.byMonth }
+
+ Just { period, to } ->
+ let
+ pu p =
+ { period = p
+ , from = Just au.month
+ , to = String.toInt to
+ , upd = u
+ }
+ in
+ case String.toInt period of
+ Nothing ->
+ Nothing
+
+ Just p ->
+ Just { updates | periodic = pu p :: updates.periodic }
newM =
- { m | editing = Nothing }
- |> setModelUpdates { updates | byMonth = byMonth }
+ newUpdates
+ |> Maybe.map
+ (\upds ->
+ { m | editing = Nothing } |> setModelUpdates upds
+ )
in
- ( newM
- , Nav.pushUrl m.navKey (modelToUrl newM)
- )
+ case newM of
+ Nothing ->
+ err
+
+ Just newM2 ->
+ ( newM2
+ , Nav.pushUrl m.navKey (modelToUrl newM2)
+ )
Nothing ->
- ( { m | editing = Just (AddUpdate { au | error = m.t "Invalid input" }) }
- , Cmd.none
- )
+ err
_ ->
( m, Cmd.none )
@@ -1255,7 +1275,7 @@ specsView { t, settings, rawSpecs } =
simButAttrs =
case parseSimSpecs rawSpecs of
Nothing ->
- [ disabled True ]
+ [ disabled True, style "cursor" "default" ]
Just specs ->
[ onClick (RunSim specs) ]
@@ -1412,7 +1432,7 @@ quotaView m { updates } { month, payed, pending_principal } =
else
text ""
- newUpdateButton =
+ newUpdateBtn =
button
(secondaryButAttrs
++ [ onClick
@@ -1424,6 +1444,7 @@ quotaView m { updates } { month, payed, pending_principal } =
, txt = ""
, month = month
, error = ""
+ , periodic = Nothing
}
, title (m.t "Add update")
]
@@ -1452,6 +1473,50 @@ quotaView m { updates } { month, payed, pending_principal } =
}
, text "%"
)
+
+ periodBtnAttrs =
+ [ class "px-2", title (m.t "Periodic update") ]
+
+ ( periodBtn, periodicInputs ) =
+ case au.periodic of
+ Nothing ->
+ ( button
+ (secondaryButAttrs
+ ++ periodBtnAttrs
+ ++ [ onClick (setUpdate <| { au | periodic = Just { period = "", to = "" } })
+ ]
+ )
+ [ text "⟳" ]
+ , text ""
+ )
+
+ Just { period, to } ->
+ ( button
+ (primaryButAttrs
+ ++ periodBtnAttrs
+ ++ [ onClick (setUpdate <| { au | periodic = Nothing })
+ ]
+ )
+ [ text "⟳" ]
+ , div [ class "flex flex-col" ]
+ [ div []
+ [ text <| m.t "Period:"
+ , text " "
+ , txtInput
+ [ class "w-[40px] border" ]
+ (\newTxt -> setUpdate { au | periodic = Just { period = newTxt, to = to } })
+ period
+ ]
+ , div []
+ [ text <| m.t "To:"
+ , text " "
+ , txtInput
+ [ class "w-[40px] border" ]
+ (\newTxt -> setUpdate { au | periodic = Just { period = period, to = newTxt } })
+ to
+ ]
+ ]
+ )
in
div (commonAttrs ++ [ class "flex flex-col border border-1 rounded-md p-2" ])
[ div [ class "flex justify-center" ]
@@ -1470,13 +1535,17 @@ quotaView m { updates } { month, payed, pending_principal } =
)
[ text <| m.t "Interest:" ]
]
- , div [ class "py-1" ]
- [ txtInput
- [ class "w-[100px] border" ]
- (\newTxt -> setUpdate { au | txt = newTxt })
- au.txt
- , afterInput
+ , div [ class "py-1 flex justify-between" ]
+ [ div []
+ [ txtInput
+ [ class "w-[100px] border" ]
+ (\newTxt -> setUpdate { au | txt = newTxt })
+ au.txt
+ , afterInput
+ ]
+ , periodBtn
]
+ , periodicInputs
, errorTxt au.error
, div [ class "flex justify-end gap-1" ]
[ button (tertiaryButAttrs ++ [ onClick (SetEditing Nothing) ]) [ text <| m.t "Cancel" ]
@@ -1484,17 +1553,17 @@ quotaView m { updates } { month, payed, pending_principal } =
]
]
- newUpdateButtonOrInput =
+ newUpdateBtnOrInput =
case m.editing of
Just (AddUpdate au) ->
if au.month == month then
newUpdateInput au
else
- newUpdateButton
+ newUpdateBtn
_ ->
- newUpdateButton
+ newUpdateBtn
updatesField =
if modBy 12 (month - 1) == 0 && not monthInExpandedYear then
@@ -1504,7 +1573,7 @@ quotaView m { updates } { month, payed, pending_principal } =
div [ class "flex flex-wrap items-start gap-1" ]
(periodicUpdates
++ byMonUpdates
- ++ [ newUpdateButtonOrInput ]
+ ++ [ newUpdateBtnOrInput ]
)
in
if modBy 12 (month - 1) == 0 || monthInExpandedYear then
diff --git a/src/lib.rs b/src/lib.rs
index 39f225f..0f2c597 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -251,8 +251,8 @@ impl SimUpdates {
update,
} in periodic.iter()
{
- let base = from.unwrap_or(0);
- if month % period == base && base <= month && to.unwrap_or(month + 1) > month {
+ let base = from.unwrap_or(1);
+ if (month - base) % period == 0 && base <= month && to.unwrap_or(month) >= month {
ret.push(*update);
}
}