From cfca18262c1ff48dcb683ddab7d03cf8e55573ff Mon Sep 17 00:00:00 2001 From: Joris Date: Fri, 24 Mar 2017 09:21:04 +0000 Subject: Features/categories --- src/client/elm/Update.elm | 134 ++++++++++++++++++++++++++-------------------- 1 file changed, 77 insertions(+), 57 deletions(-) (limited to 'src/client/elm/Update.elm') diff --git a/src/client/elm/Update.elm b/src/client/elm/Update.elm index e66414e..7006d5a 100644 --- a/src/client/elm/Update.elm +++ b/src/client/elm/Update.elm @@ -1,11 +1,10 @@ module Update exposing ( update - , urlUpdate ) import Task import Platform.Cmd exposing (Cmd) -import Navigation +import Navigation exposing (Location) import Page exposing (Page) @@ -32,7 +31,6 @@ 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 = @@ -41,12 +39,15 @@ update msg model = NoOp -> (model, Cmd.none) + UpdatePage page -> + ({ model | page = page }, Cmd.none) + SignIn email -> ( applySignIn model (SignInMsg.WaitingServer) - , Server.signIn email - |> Task.perform - (\error -> UpdateSignIn (SignInMsg.ErrorLogin (errorKey error))) - (\() -> UpdateSignIn SignInMsg.ValidLogin) + , Server.signIn email (\result -> case result of + Ok _ -> UpdateSignIn SignInMsg.ValidLogin + Err error -> UpdateSignIn (SignInMsg.ErrorLogin (errorKey error)) + ) ) GoLoggedInView init -> @@ -66,73 +67,100 @@ update msg model = UpdateLoggedIn loggedInMsg -> applyLoggedIn model loggedInMsg - CreatePayment name cost date frequency -> + SignOut -> + ( model + , Server.signOut (\result -> case result of + Ok _ -> GoSignInView + Err _ -> Error "SignOutError" + ) + ) + + Error error -> + ({ model | errors = model.errors ++ [ error ] }, Cmd.none) + + Dialog dialogMsg -> + Dialog.update DialogUpdate.update dialogMsg model.dialog.model model.dialog + |> Tuple.mapFirst (\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 + ) + + CreatePayment name cost date category frequency -> ( model - , Server.createPayment name cost date frequency - |> Task.perform - (always <| Error "CreatePaymentError") - (\paymentId -> UpdateLoggedIn <| LoggedInMsg.ValidateCreatePayment paymentId name cost date frequency) + , Server.createPayment name cost date category frequency (\result -> case result of + Ok paymentId -> UpdateLoggedIn <| LoggedInMsg.ValidateCreatePayment paymentId name cost date category frequency + Err _ -> Error "CreatePaymentError" + ) ) - EditPayment paymentId name cost date frequency -> + EditPayment paymentId name cost date category frequency -> ( model - , Server.editPayment paymentId name cost date frequency - |> Task.perform - (always <| Error "EditPaymentError") - (always <| UpdateLoggedIn <| LoggedInMsg.ValidateEditPayment paymentId name cost date frequency) + , Server.editPayment paymentId name cost date category frequency (\result -> case result of + Ok _ -> UpdateLoggedIn <| LoggedInMsg.ValidateEditPayment paymentId name cost date category frequency + Err _ -> Error "EditPaymentError" + ) ) DeletePayment paymentId -> ( model - , Server.deletePayment paymentId - |> Task.perform - (always <| Error "DeletePaymentError") - (always <| UpdateLoggedIn <| LoggedInMsg.ValidateDeletePayment paymentId) + , Server.deletePayment paymentId (\result -> case result of + Ok _ -> UpdateLoggedIn <| LoggedInMsg.ValidateDeletePayment paymentId + Err _ -> Error "DeletePaymentError" + ) ) CreateIncome amount date -> ( model - , Server.createIncome amount date - |> Task.perform - (always <| Error "CreateIncomeError") - (\incomeId -> UpdateLoggedIn <| LoggedInMsg.ValidateCreateIncome incomeId amount date) + , Server.createIncome amount date (\result -> case result of + Ok incomeId -> UpdateLoggedIn <| LoggedInMsg.ValidateCreateIncome incomeId amount date + Err _ -> Error "CreateIncomeError" + ) ) EditIncome incomeId amount date -> ( model - , Server.editIncome incomeId amount date - |> Task.perform - (always <| Error "EditIncomeError") - (always <| UpdateLoggedIn <| LoggedInMsg.ValidateEditIncome incomeId amount date) + , Server.editIncome incomeId amount date (\result -> case result of + Ok _ -> UpdateLoggedIn <| LoggedInMsg.ValidateEditIncome incomeId amount date + Err _ -> Error "EditIncomeError" + ) ) DeleteIncome incomeId -> ( model - , Server.deleteIncome incomeId - |> Task.perform - (always <| Error "DeleteIncomeError") - (always <| UpdateLoggedIn <| LoggedInMsg.ValidateDeleteIncome incomeId) + , Server.deleteIncome incomeId (\result -> case result of + Ok _ -> UpdateLoggedIn <| LoggedInMsg.ValidateDeleteIncome incomeId + Err _ -> Error "DeleteIncomeError" + ) ) - SignOut -> + CreateCategory name color -> ( model - , Server.signOut - |> Task.perform (always <| Error "SignOutError") (always GoSignInView) + , Server.createCategory name color (\result -> case result of + Ok categoryId -> UpdateLoggedIn <| LoggedInMsg.ValidateCreateCategory categoryId name color + Err _ -> Error "CreateCategoryError" + ) ) - Error error -> - ({ model | errors = model.errors ++ [ error ] }, Cmd.none) + EditCategory categoryId name color -> + ( model + , Server.editCategory categoryId name color (\result -> case result of + Ok _ -> UpdateLoggedIn <| LoggedInMsg.ValidateEditCategory categoryId name color + Err _ -> Error "EditCategoryError" + ) + ) - Dialog dialogMsg -> - Dialog.update DialogUpdate.update dialogMsg model.dialog.model model.dialog - |> Tuple.mapFst (\dialog -> { model | dialog = dialog }) - :> update (Tooltip Tooltip.HideMessage) + DeleteCategory categoryId -> + ( model + , Server.deleteCategory categoryId (\result -> case result of + Ok _ -> UpdateLoggedIn <| LoggedInMsg.ValidateDeleteCategory categoryId + Err _ -> Error "DeleteCategoryError" + ) + ) - 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 = @@ -146,17 +174,9 @@ 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 } + let (view, cmd) = LoggedInUpdate.update model loggedInMsg loggedInView + in ( { model | view = V.LoggedInView view } , 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) -- cgit v1.2.3