aboutsummaryrefslogtreecommitdiff
path: root/src/client/elm/LoggedIn/Home/Header/View.elm
blob: e6b24445a2449de33c3b9e15fa1c8f1ef2bf972d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
module LoggedIn.Home.Header.View exposing
  ( view
  )

import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (..)
import Html.App as Html
import String
import Dict
import Date

import Form exposing (Form)
import View.Form as Form
import View.Events exposing (onSubmitPrevDefault)

import Msg exposing (Msg)
import LoggedIn.Msg as LoggedInMsg
import LoggedIn.Home.Msg as HomeMsg

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.Translations exposing (getMessage)

import Dialog.AddPayment.Model as AddPayment
import Dialog.AddPayment.View as AddPayment

import LoggedIn.Home.View.ExceedingPayers as ExceedingPayers
import LoggedIn.View.Format as Format
import View.Plural exposing (plural)

import Utils.Tuple as Tuple

view : LoggedData -> Home.Model -> Payments -> Frequency -> Html Msg
view loggedData { search } payments frequency =
  let currentDate = Date.fromTime loggedData.currentTime
  in  Html.div
        [ class "header" ]
        [ div
            [ class "payerAndAdd" ]
            [ ExceedingPayers.view loggedData
            , AddPayment.button
                loggedData
                (AddPayment.initialAdd loggedData.translations currentDate frequency)
                "AddPayment"
                (text (getMessage "AddPayment" loggedData.translations))
                Nothing
            ]
        , Html.div
            [ class "searchLine" ]
            [ searchForm loggedData search ]
        , infos loggedData payments
        ]

searchForm : LoggedData -> Form String Home.Search -> Html Msg
searchForm loggedData search =
  Html.map (Msg.UpdateLoggedIn << LoggedInMsg.HomeMsg << HomeMsg.SearchMsg) <|
    Html.form
      [ onSubmitPrevDefault Form.NoOp ]
      [ Form.textInput loggedData.translations search "search" "name"
      , if List.isEmpty (Payment.monthly loggedData.payments)
          then text ""
          else Form.radioInputs loggedData.translations search "search" "frequency" [ toString Punctual, toString Monthly ]
      ]

infos : LoggedData -> Payments -> Html Msg
infos loggedData payments =
  let paymentsCount = List.length payments
  in  if paymentsCount == 0
        then text ""
        else
          let count = plural loggedData.translations (List.length payments) "Payment" "Payments"
              sum = paymentsSum loggedData.conf payments
          in  div
                [ class "infos" ]
                [ span
                    [ class "total" ]
                    [ text <| getParamMessage [ count, sum ] "Worth" loggedData.translations ]
                , span
                    [ class "partition" ]
                    [ text <| paymentsPartition loggedData payments ]
                ]

paymentsPartition : LoggedData -> Payments -> String
paymentsPartition loggedData payments =
  String.join
    ", "
    ( loggedData.users
        |> Dict.toList
        |> List.map (Tuple.mapFst (\userId -> Payment.totalPayments (always True) userId payments))
        |> List.filter (\(sum, _) -> sum > 0)
        |> List.sortBy fst
        |> List.reverse
        |> List.map (\(sum, user) ->
             getParamMessage [ user.name, Format.price loggedData.conf sum ] "By" loggedData.translations
           )
    )

paymentsSum : Conf -> Payments -> String
paymentsSum conf payments =
  payments
    |> List.map .cost
    |> List.sum
    |> Format.price conf