module Update exposing ( update , urlUpdate ) import Task import Platform.Cmd exposing (Cmd) import Navigation import Page exposing (Page) import Server import Msg exposing (..) import Model exposing (Model) import Model.Translations exposing (getMessage) import Model.View as V import LoggedIn.Model as LoggedInModel import LoggedIn.Msg as LoggedInMsg import LoggedIn.Update as LoggedInUpdate import SignIn.Model as SignInModel import SignIn.Msg as SignInMsg import SignIn.Update as SignInUpdate import Dialog import Dialog.Update as DialogUpdate import Tooltip import Utils.Http exposing (errorKey) import Utils.Cmd exposing ((:>)) import Utils.Tuple as Tuple update : Msg -> Model -> (Model, Cmd Msg) update msg model = case msg of NoOp -> (model, Cmd.none) SignIn email -> ( applySignIn model (SignInMsg.WaitingServer) , Server.signIn email |> Task.perform (\error -> UpdateSignIn (SignInMsg.ErrorLogin (errorKey error))) (\() -> UpdateSignIn SignInMsg.ValidLogin) ) GoLoggedInView init -> ( { model | view = V.LoggedInView (LoggedInModel.init init) } , Cmd.none ) UpdateTime time -> ({ model | currentTime = time }, Cmd.none) GoSignInView -> ({ model | view = V.SignInView (SignInModel.init Nothing) }, Cmd.none) UpdateSignIn signInMsg -> (applySignIn model signInMsg, Cmd.none) UpdateLoggedIn loggedInMsg -> applyLoggedIn model loggedInMsg CreatePayment name cost date frequency -> ( model , Server.createPayment name cost date frequency |> Task.perform (always <| Error "CreatePaymentError") (\paymentId -> UpdateLoggedIn <| LoggedInMsg.ValidateCreatePayment paymentId name cost date frequency) ) EditPayment paymentId name cost date frequency -> ( model , Server.editPayment paymentId name cost date frequency |> Task.perform (always <| Error "EditPaymentError") (always <| UpdateLoggedIn <| LoggedInMsg.ValidateEditPayment paymentId name cost date frequency) ) DeletePayment paymentId -> ( model , Server.deletePayment paymentId |> Task.perform (always <| Error "DeletePaymentError") (always <| UpdateLoggedIn <| LoggedInMsg.ValidateDeletePayment paymentId) ) CreateIncome amount date -> ( model , Server.createIncome amount date |> Task.perform (always <| Error "CreateIncomeError") (\incomeId -> UpdateLoggedIn <| LoggedInMsg.ValidateCreateIncome incomeId amount date) ) EditIncome incomeId amount date -> ( model , Server.editIncome incomeId amount date |> Task.perform (always <| Error "EditIncomeError") (always <| UpdateLoggedIn <| LoggedInMsg.ValidateEditIncome incomeId amount date) ) DeleteIncome incomeId -> ( model , Server.deleteIncome incomeId |> Task.perform (always <| Error "DeleteIncomeError") (always <| UpdateLoggedIn <| LoggedInMsg.ValidateDeleteIncome incomeId) ) SignOut -> ( model , Server.signOut |> Task.perform (always <| Error "SignOutError") (always GoSignInView) ) Error error -> ({ model | errors = model.errors ++ [ error ] }, Cmd.none) Dialog dialogMsg -> Dialog.update DialogUpdate.update dialogMsg model.dialog.model model.dialog |> Tuple.mapFst (\dialog -> { model | dialog = dialog }) :> update (Tooltip Tooltip.HideMessage) Tooltip tooltipMsg -> let (newTooltip, command) = Tooltip.update tooltipMsg model.tooltip in ( { model | tooltip = newTooltip } , Cmd.map Tooltip command ) applySignIn : Model -> SignInMsg.Msg -> Model applySignIn model signInMsg = case model.view of V.SignInView signInView -> { model | view = V.SignInView (SignInUpdate.update model.translations signInMsg signInView) } _ -> model applyLoggedIn : Model -> LoggedInMsg.Msg -> (Model, Cmd Msg) applyLoggedIn model loggedInMsg = case model.view of V.LoggedInView loggedInView -> let (loggedInView, cmd) = LoggedInUpdate.update model loggedInMsg loggedInView in ( { model | view = V.LoggedInView loggedInView } , Cmd.map UpdateLoggedIn cmd ) _ -> (model, Cmd.none) urlUpdate : Result String Page -> Model -> (Model, Cmd Msg) urlUpdate result model = case result of Err _ -> (model, Navigation.modifyUrl (Page.toHash model.page)) Ok page -> ({ model | page = page }, Cmd.none)