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 --- README.md | 1 - src/client/Dialog/AddPayment/Model.elm | 8 ++++--- src/client/Dialog/AddPayment/View.elm | 2 +- 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 ++++--- src/client/LoggedIn/Msg.elm | 3 ++- src/client/LoggedIn/Update.elm | 3 ++- src/client/Model/Frequency.elm | 36 ++++++++++++++++++++++++++++++++ src/client/Model/Payment.elm | 29 ++----------------------- src/client/Msg.elm | 3 ++- src/client/Server.elm | 1 + src/client/Utils/Form.elm | 2 -- 16 files changed, 96 insertions(+), 61 deletions(-) create mode 100644 src/client/Model/Frequency.elm diff --git a/README.md b/README.md index 281fff7..9f9045c 100644 --- a/README.md +++ b/README.md @@ -46,7 +46,6 @@ See [application.conf](application.conf). TODO ---- -- Modelize current page for both punctual and monthly payments - search by payment category - Use `sqlite-simple` instead of `persistent` - show charts with payments and categories diff --git a/src/client/Dialog/AddPayment/Model.elm b/src/client/Dialog/AddPayment/Model.elm index 11d59b1..8a94bc7 100644 --- a/src/client/Dialog/AddPayment/Model.elm +++ b/src/client/Dialog/AddPayment/Model.elm @@ -15,9 +15,11 @@ import Form.Field as Field exposing (Field) import Form.Validate as Validate exposing (Validation) import Validation -import Model.Payment as Payment exposing (Payment, Frequency, PaymentId) -import Model.Translations exposing (Translations) import Model.Category as Category exposing (Categories, CategoryId) +import Model.Frequency as Frequency +import Model.Payment as Payment exposing (Payment, PaymentId) +import Model.Frequency exposing (Frequency) +import Model.Translations exposing (Translations) import Utils.Maybe as Maybe @@ -67,4 +69,4 @@ validation categories = (Validate.field "cost" Validation.cost) (Validate.field "date" Validation.date) (Validate.field "category" (Validation.category categories)) - (Validate.field "frequency" Payment.validateFrequency) + (Validate.field "frequency" Frequency.validate) diff --git a/src/client/Dialog/AddPayment/View.elm b/src/client/Dialog/AddPayment/View.elm index 66c9f00..584adcd 100644 --- a/src/client/Dialog/AddPayment/View.elm +++ b/src/client/Dialog/AddPayment/View.elm @@ -26,7 +26,7 @@ import LoggedIn.Msg as LoggedInMsg import Msg exposing (Msg) import Model.Category exposing (Categories) -import Model.Payment as Payment exposing (Frequency(..)) +import Model.Frequency exposing (Frequency(..)) import Model.PaymentCategory exposing (PaymentCategories) import Model.Translations exposing (getMessage) import Model.View exposing (View(LoggedInView)) 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" ] diff --git a/src/client/LoggedIn/Msg.elm b/src/client/LoggedIn/Msg.elm index a128cff..d00e2cb 100644 --- a/src/client/LoggedIn/Msg.elm +++ b/src/client/LoggedIn/Msg.elm @@ -4,7 +4,8 @@ module LoggedIn.Msg exposing import Date exposing (Date) -import Model.Payment exposing (PaymentId, Frequency) +import Model.Payment exposing (PaymentId) +import Model.Frequency exposing (Frequency) import Model.Income exposing (IncomeId) import Model.Category exposing (CategoryId) diff --git a/src/client/LoggedIn/Update.elm b/src/client/LoggedIn/Update.elm index 1359e1a..753b1d3 100644 --- a/src/client/LoggedIn/Update.elm +++ b/src/client/LoggedIn/Update.elm @@ -13,7 +13,8 @@ import Platform.Cmd exposing (Cmd) import Form import Model exposing (Model) -import Model.Payment as Payment exposing (Payment, Frequency(..)) +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 diff --git a/src/client/Model/Frequency.elm b/src/client/Model/Frequency.elm new file mode 100644 index 0000000..40f9893 --- /dev/null +++ b/src/client/Model/Frequency.elm @@ -0,0 +1,36 @@ +module Model.Frequency exposing + ( Frequency(..) + , decoder + , validate + , fromString + ) + +import Json.Decode as Decode exposing (Decoder) +import Json.Decode.Extra as Decode + +import Form.Validate as Validate exposing (Validation) + +type Frequency = Punctual | Monthly + +decoder : Decoder Frequency +decoder = + let frequencyResult input = + fromString input + |> Result.fromMaybe ("Could not deduce Punctual nor Monthly from " ++ input) + in Decode.string |> Decode.andThen (Decode.fromResult << frequencyResult) + +validate : Validation String Frequency +validate = + Validate.customValidation Validate.string (\str -> + fromString str + |> Result.fromMaybe (Validate.customError "InvalidFrequency") + ) + +fromString : String -> Maybe Frequency +fromString str = + if str == toString Punctual then + Just Punctual + else if str == toString Monthly then + Just Monthly + else + Nothing diff --git a/src/client/Model/Payment.elm b/src/client/Model/Payment.elm index f61ded8..2412ab9 100644 --- a/src/client/Model/Payment.elm +++ b/src/client/Model/Payment.elm @@ -3,7 +3,6 @@ module Model.Payment exposing , Payments , Payment , PaymentId - , Frequency(..) , paymentsDecoder , paymentIdDecoder , find @@ -14,7 +13,6 @@ module Model.Payment exposing , monthly , groupAndSortByMonth , search - , validateFrequency ) import Date exposing (..) @@ -24,9 +22,9 @@ import Json.Decode.Extra as Decode import List import Form.Validate as Validate exposing (Validation) +import Model.Frequency as Frequency exposing (Frequency(..)) import Model.Date exposing (dateDecoder) import Model.User exposing (UserId, userIdDecoder) - import Utils.List as List import Utils.Search as Search @@ -46,8 +44,6 @@ type alias Payment = type alias PaymentId = Int -type Frequency = Punctual | Monthly - paymentsDecoder : Decoder Payments paymentsDecoder = Decode.list paymentDecoder @@ -59,20 +55,11 @@ paymentDecoder = (Decode.field "cost" Decode.int) (Decode.field "date" dateDecoder) (Decode.field "userId" userIdDecoder) - (Decode.field "frequency" frequencyDecoder) + (Decode.field "frequency" Frequency.decoder) paymentIdDecoder : Decoder PaymentId paymentIdDecoder = Decode.int -frequencyDecoder : Decoder Frequency -frequencyDecoder = - let frequencyResult input = - case input of - "Punctual" -> Ok Punctual - "Monthly" -> Ok Monthly - _ -> Err ("Could not deduce Punctual nor Monthly from " ++ input) - in Decode.string |> Decode.andThen (Decode.fromResult << frequencyResult) - find : PaymentId -> Payments -> Maybe Payment find paymentId payments = payments @@ -129,15 +116,3 @@ searchSuccess search { name, cost } = || String.contains word (toString cost) ) in List.all searchSuccessWord (String.words search) - -validateFrequency : Validation String Frequency -validateFrequency = - Validate.customValidation Validate.string (\str -> - if str == toString Punctual - then - Ok Punctual - else - if str == toString Monthly - then Ok Monthly - else Err (Validate.customError "InvalidFrequency") - ) diff --git a/src/client/Msg.elm b/src/client/Msg.elm index cf592aa..5970747 100644 --- a/src/client/Msg.elm +++ b/src/client/Msg.elm @@ -8,7 +8,8 @@ import Time exposing (Time) import Page exposing (Page) import Model.Init exposing (Init) -import Model.Payment exposing (PaymentId, Frequency) +import Model.Payment exposing (PaymentId) +import Model.Frequency exposing (Frequency) import Model.Income exposing (IncomeId) import Model.Category exposing (CategoryId) diff --git a/src/client/Server.elm b/src/client/Server.elm index 7f25876..c44b777 100644 --- a/src/client/Server.elm +++ b/src/client/Server.elm @@ -24,6 +24,7 @@ import Date.Extra.Format as DateFormat import Utils.Http as HttpUtils import Model.Payment exposing (..) +import Model.Frequency exposing (Frequency) import Model.Income exposing (incomeIdDecoder, IncomeId) import Model.Category exposing (categoryIdDecoder, CategoryId) import Model.User exposing (Users, usersDecoder, UserId, userIdDecoder) diff --git a/src/client/Utils/Form.elm b/src/client/Utils/Form.elm index 8d75a32..6793222 100644 --- a/src/client/Utils/Form.elm +++ b/src/client/Utils/Form.elm @@ -4,8 +4,6 @@ module Utils.Form exposing import Form exposing (Form) -import Model.Payment exposing (Frequency(..)) - fieldAsText : Form a b -> String -> String fieldAsText form field = Form.getFieldAsString field form -- cgit v1.2.3