From 64ff4707fdcd81c27c6be9903c3c82bc543ef016 Mon Sep 17 00:00:00 2001 From: Joris Date: Mon, 27 Mar 2017 10:18:40 +0200 Subject: Modelize punctual and monthly payment pages --- src/client/LoggedIn/Home/Header/View.elm | 3 ++- src/client/LoggedIn/Home/Model.elm | 16 ++++++++++------ src/client/LoggedIn/Home/Msg.elm | 1 + src/client/LoggedIn/Home/Update.elm | 31 ++++++++++++++++++++----------- src/client/LoggedIn/Home/View.elm | 11 ++++++++--- src/client/LoggedIn/Home/View/Table.elm | 7 ++++--- 6 files changed, 45 insertions(+), 24 deletions(-) (limited to 'src/client/LoggedIn/Home') diff --git a/src/client/LoggedIn/Home/Header/View.elm b/src/client/LoggedIn/Home/Header/View.elm index 3f8a320..14d90d7 100644 --- a/src/client/LoggedIn/Home/Header/View.elm +++ b/src/client/LoggedIn/Home/Header/View.elm @@ -21,7 +21,8 @@ import LoggedData exposing (LoggedData) import LoggedIn.Home.Model as Home import Model.Translations exposing (getParamMessage) import Model.Conf exposing (Conf) -import Model.Payment as Payment exposing (Payments, Frequency(..)) +import Model.Payment as Payment exposing (Payments) +import Model.Frequency exposing (Frequency(..)) import Model.Translations exposing (getMessage) import Dialog.AddPayment.Model as AddPayment diff --git a/src/client/LoggedIn/Home/Model.elm b/src/client/LoggedIn/Home/Model.elm index ace1593..e5381f6 100644 --- a/src/client/LoggedIn/Home/Model.elm +++ b/src/client/LoggedIn/Home/Model.elm @@ -7,15 +7,18 @@ module LoggedIn.Home.Model exposing ) import Form exposing (Form) -import Form.Validate as Validate exposing (Validation) import Form.Field as Field exposing (Field) +import Form.Validate as Validate exposing (Validation) -import Model.User exposing (Users, UserId) -import Model.Payment as Payment exposing (PaymentId, Payments, Frequency(..)) +import Model.Frequency as Frequency import Model.Payer exposing (Payers) +import Model.Payment as Payment exposing (PaymentId, Payments) +import Model.Frequency exposing (Frequency(..)) +import Model.User exposing (Users, UserId) type alias Model = - { currentPage : Int + { punctualPage : Int + , monthlyPage : Int , search : Form String Search } @@ -26,7 +29,8 @@ type alias Search = init : Model init = - { currentPage = 1 + { punctualPage = 1 + , monthlyPage = 1 , search = Form.initial (searchInitial Punctual) validation } @@ -37,4 +41,4 @@ validation : Validation String Search validation = Validate.map2 Search (Validate.field "name" (Validate.maybe Validate.string)) - (Validate.field "frequency" Payment.validateFrequency) + (Validate.field "frequency" Frequency.validate) diff --git a/src/client/LoggedIn/Home/Msg.elm b/src/client/LoggedIn/Home/Msg.elm index b5f2566..69f15ad 100644 --- a/src/client/LoggedIn/Home/Msg.elm +++ b/src/client/LoggedIn/Home/Msg.elm @@ -5,6 +5,7 @@ module LoggedIn.Home.Msg exposing import Form exposing (Form) import Model.Payment exposing (PaymentId) +import Model.Frequency exposing (Frequency) type Msg = NoOp diff --git a/src/client/LoggedIn/Home/Update.elm b/src/client/LoggedIn/Home/Update.elm index b0ce256..06c2c7e 100644 --- a/src/client/LoggedIn/Home/Update.elm +++ b/src/client/LoggedIn/Home/Update.elm @@ -5,9 +5,9 @@ module LoggedIn.Home.Update exposing import Form exposing (Form) import LoggedData exposing (LoggedData) - -import LoggedIn.Home.Msg as Home import LoggedIn.Home.Model as Home +import LoggedIn.Home.Msg as Home +import Model.Frequency as Frequency exposing (Frequency(..)) update : LoggedData -> Home.Msg -> Home.Model -> (Home.Model, Cmd Home.Msg) update loggedData msg model = @@ -19,17 +19,26 @@ update loggedData msg model = ) Home.UpdatePage page -> - ( { model | currentPage = page } + ( updatePage page model , Cmd.none ) Home.SearchMsg formMsg -> - ( { model - | search = Form.update Home.validation formMsg model.search - , currentPage = + let newModel = case formMsg of - Form.Input "name" _ _ -> 1 - _ -> model.currentPage - } - , Cmd.none - ) + Form.Input "name" _ _ -> updatePage 1 model + _ -> model + in ( { model | search = Form.update Home.validation formMsg model.search } + , Cmd.none + ) + +updatePage : Int -> Home.Model -> Home.Model +updatePage page model = + let frequency = + Form.getFieldAsString "frequency" model.search + |> .value + |> Maybe.andThen Frequency.fromString + in case frequency of + Just Punctual -> { model | punctualPage = page } + Just Monthly -> { model | monthlyPage = page } + Nothing -> model diff --git a/src/client/LoggedIn/Home/View.elm b/src/client/LoggedIn/Home/View.elm index 0b90e67..fba3f7c 100644 --- a/src/client/LoggedIn/Home/View.elm +++ b/src/client/LoggedIn/Home/View.elm @@ -16,7 +16,8 @@ import LoggedIn.Home.Msg as HomeMsg import LoggedIn.Home.View.Paging as Paging import LoggedIn.Home.View.Table as Table import LoggedIn.Msg as LoggedInMsg -import Model.Payment as Payment exposing (Frequency(..)) +import Model.Payment as Payment +import Model.Frequency exposing (Frequency(..)) import Msg exposing (Msg) view : LoggedData -> Home.Model -> Html Msg @@ -26,12 +27,16 @@ view loggedData home = Just data -> (Maybe.withDefault "" data.name, data.frequency) Nothing -> ("", Punctual) payments = Payment.search name frequency loggedData.payments + page = + case frequency of + Punctual -> home.punctualPage + Monthly -> home.monthlyPage in div [ class "home" ] [ Header.view loggedData home payments frequency - , Table.view loggedData home payments frequency + , Table.view loggedData home payments frequency page , Paging.view - home.currentPage + page (List.length payments) Msg.NoOp (Msg.UpdateLoggedIn << LoggedInMsg.HomeMsg << HomeMsg.UpdatePage) diff --git a/src/client/LoggedIn/Home/View/Table.elm b/src/client/LoggedIn/Home/View/Table.elm index 8828488..f94bb19 100644 --- a/src/client/LoggedIn/Home/View/Table.elm +++ b/src/client/LoggedIn/Home/View/Table.elm @@ -30,15 +30,16 @@ import LoggedIn.View.Format as Format import View.Date as Date import Model.Payment as Payment exposing (..) +import Model.Frequency exposing (Frequency(..)) import Model.PaymentCategory as PaymentCategory import Model.Translations exposing (getMessage) import Model.User exposing (getUserName) -view : LoggedData -> Home.Model -> Payments -> Frequency -> Html Msg -view loggedData homeModel payments frequency = +view : LoggedData -> Home.Model -> Payments -> Frequency -> Int -> Html Msg +view loggedData homeModel payments frequency page = let visiblePayments = payments - |> List.drop ((homeModel.currentPage - 1) * perPage) + |> List.drop ((page - 1) * perPage) |> List.take perPage in div [ class "table" ] -- cgit v1.2.3