module LoggedIn.Update exposing ( update ) import Dict import String import Task import Http exposing (Error(..)) import Date exposing (Date) import Platform.Cmd exposing (Cmd) import Form import Model exposing (Model) import Model.Payment as Payment exposing (Payment) import Model.Frequency exposing (Frequency(..)) import Model.Income as Income exposing (Income) import Model.Category exposing (Category) import Model.PaymentCategory as PaymentCategory import Server import LoggedData import LoggedIn.Msg as LoggedInMsg import LoggedIn.Model as LoggedInModel import LoggedIn.Home.Msg as Home import LoggedIn.Home.Update as Home import LoggedIn.Home.Model as Home import Utils.Cmd exposing ((:>)) update : Model -> LoggedInMsg.Msg -> LoggedInModel.Model -> (LoggedInModel.Model, Cmd LoggedInMsg.Msg) update model msg loggedIn = let loggedData = LoggedData.build model loggedIn in case msg of LoggedInMsg.NoOp -> ( loggedIn , Cmd.none ) LoggedInMsg.HomeMsg homeMsg -> case Home.update loggedData homeMsg loggedIn.home of (home, effects) -> ( { loggedIn | home = home } , Cmd.map LoggedInMsg.HomeMsg effects ) LoggedInMsg.ValidateCreatePayment paymentId name cost date category frequency -> update model (LoggedInMsg.HomeMsg <| Home.SearchMsg (Form.Reset (Home.searchInitial frequency))) loggedIn :> update model (LoggedInMsg.HomeMsg <| Home.UpdatePage 1) :> (\loggedIn -> let newPayment = Payment paymentId name cost date loggedIn.me frequency in ( { loggedIn | payments = newPayment :: loggedIn.payments , paymentCategories = PaymentCategory.save name category loggedIn.paymentCategories } , Cmd.none ) ) LoggedInMsg.ValidateEditPayment paymentId name cost date category frequency -> let updatedPayment = Payment paymentId name cost date loggedIn.me frequency mbOldPayment = Payment.find paymentId loggedIn.payments in ( { loggedIn | payments = Payment.edit updatedPayment loggedIn.payments , paymentCategories = case mbOldPayment of Just oldPayment -> PaymentCategory.save name category loggedIn.paymentCategories Nothing -> loggedData.paymentCategories } , Cmd.none ) LoggedInMsg.ValidateDeletePayment paymentId -> let payments = Payment.delete paymentId loggedIn.payments frequency = case Form.getOutput loggedIn.home.search of Just data -> data.frequency Nothing -> Punctual switchToPunctual = ( frequency == Monthly && List.isEmpty (Payment.monthly payments) ) in if switchToPunctual then update model (LoggedInMsg.HomeMsg <| Home.SearchMsg (Form.Reset (Home.searchInitial Punctual))) loggedIn :> (\loggedIn -> ( { loggedIn | payments = payments } , Cmd.none ) ) else ( { loggedIn | payments = payments } , Cmd.none ) LoggedInMsg.ValidateCreateIncome incomeId amount date -> let newIncome = { userId = loggedIn.me, amount = amount, time = Date.toTime date } in ( { loggedIn | incomes = Dict.insert incomeId newIncome loggedIn.incomes } , Cmd.none ) LoggedInMsg.ValidateEditIncome incomeId amount date -> let updateIncome _ = Just <| Income loggedIn.me (Date.toTime date) amount in ( { loggedIn | incomes = Dict.update incomeId updateIncome loggedIn.incomes } , Cmd.none ) LoggedInMsg.ValidateDeleteIncome incomeId -> ( { loggedIn | incomes = Dict.remove incomeId loggedIn.incomes } , Cmd.none ) LoggedInMsg.ValidateCreateCategory categoryId name color -> let newCategory = { name = name, color = color } in ( { loggedIn | categories = Dict.insert categoryId newCategory loggedIn.categories } , Cmd.none ) LoggedInMsg.ValidateEditCategory categoryId name color -> let updateCategory _ = Just <| Category name color in ( { loggedIn | categories = Dict.update categoryId updateCategory loggedIn.categories } , Cmd.none) LoggedInMsg.ValidateDeleteCategory categoryId -> ( { loggedIn | categories = Dict.remove categoryId loggedIn.categories } , Cmd.none )