diff options
-rw-r--r-- | front/src/Main.elm | 96 | ||||
-rw-r--r-- | src/lib.rs | 12 |
2 files changed, 77 insertions, 31 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 [] @@ -233,14 +233,14 @@ pub struct PeriodicUpdate { #[derive(Debug, Default, Serialize, Deserialize)] pub struct SimUpdates { - periodically: Vec<PeriodicUpdate>, + periodic: Vec<PeriodicUpdate>, by_month: HashMap<u32, SimUpdate>, } impl SimUpdates { fn get(&self, month: u32) -> Vec<SimUpdate> { let SimUpdates { - periodically, + periodic, by_month, } = self; @@ -250,7 +250,7 @@ impl SimUpdates { from, to, update, - } in periodically.iter() + } in periodic.iter() { let base = from.unwrap_or(0); if month % period == base && base <= month && to.unwrap_or(month + 1) > month { @@ -265,8 +265,8 @@ impl SimUpdates { } pub fn and(mut self, other: SimUpdates) -> Self { - for p in other.periodically.iter() { - self.periodically.push(p.clone()); + for p in other.periodic.iter() { + self.periodic.push(p.clone()); } for (k, v) in other.by_month { self.by_month.insert(k, v); @@ -298,7 +298,7 @@ impl fmt::Display for SimUpdate { impl SimUpdate { pub fn every(&self, months: u32) -> SimUpdates { let mut updates = SimUpdates::default(); - updates.periodically.push(PeriodicUpdate { + updates.periodic.push(PeriodicUpdate { period: months, from: None, to: None, |