aboutsummaryrefslogtreecommitdiff
path: root/src/client/elm/LoggedIn/Income/View.elm
blob: 39f16f4b39ad88dae0ce7f8a4b8baa23393db4fc (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
module LoggedIn.Income.View exposing
  ( view
  )

import Dict
import Date
import Time exposing (Time)

import Html.App as Html
import Html exposing (..)
import Html.Events exposing (..)
import Html.Attributes exposing (..)
import Form exposing (Form)
import Form.Input as Input

import Msg exposing (Msg)

import LoggedData exposing (LoggedData)

import Model.Income exposing (IncomeId, Income, userCumulativeIncomeSince)
import Model.Translations exposing (getMessage, getParamMessage)
import Model.Payer exposing (useIncomesFrom)
import Model.User exposing (UserId, User)
import LoggedIn.Income.Model as IncomeModel

import LoggedIn.Msg as LoggedInMsg
import LoggedIn.Income.Msg as IncomeMsg

import LoggedIn.View.Date exposing (renderShortDate)
import LoggedIn.View.Format as Format

import Utils.Maybe exposing (isJust)

import LoggedIn.View.Date exposing (renderLongDate)
import View.Events exposing (onSubmitPrevDefault)

view : LoggedData -> IncomeModel.Model -> Html Msg
view loggedData incomeModel =
  div
    [ class "income" ]
    [ case useIncomesFrom loggedData.users loggedData.incomes loggedData.payments of
        Just since -> cumulativeIncomesView loggedData since
        Nothing -> text ""
    , h1 [] [ text <| getMessage "AddIncome" loggedData.translations ]
    , addIncomeView loggedData incomeModel.addIncome
    , h1 [] [ text <| getMessage "MonthlyNetIncomes" loggedData.translations ]
    , incomesView loggedData
    ]

cumulativeIncomesView : LoggedData -> Time -> Html Msg
cumulativeIncomesView loggedData since =
  let longDate = renderLongDate (Date.fromTime since) loggedData.translations
  in  div
        []
        [ h1 [] [ text <| getParamMessage [longDate] "CumulativeIncomesSince" loggedData.translations ]
        , ul
            []
            ( Dict.toList loggedData.users
                |> List.map (\(userId, user) ->
                     (user.name, userCumulativeIncomeSince loggedData.currentTime since loggedData.incomes userId)
                   )
                |> List.sortBy snd
                |> List.map (\(userName, cumulativeIncome) ->
                     li
                       []
                       [ text userName
                       , text " − "
                       , text <| Format.price loggedData.conf cumulativeIncome
                       ]
                   )
            )
        ]

addIncomeView : LoggedData -> Form () IncomeModel.AddIncome -> Html Msg
addIncomeView loggedData addIncome =
  let
    errorFor error field =
      if isJust field.liveError
        then div [ class "error" ] [ text (getMessage error loggedData.translations) ]
        else text ""
    creation = Form.getFieldAsString "creation" addIncome
    amount = Form.getFieldAsString "amount" addIncome
    htmlMap = Html.map (Msg.UpdateLoggedIn << LoggedInMsg.IncomeMsg << IncomeMsg.AddIncomeMsg)
  in
    Html.form
      [ onSubmitPrevDefault Msg.NoOp ]
      [ label [] [ text "Creation" ]
      , htmlMap <| Input.textInput creation []
      , errorFor "DateValidationError" creation

      , label [] [ text "amount" ]
      , htmlMap <| Input.textInput amount []
      , errorFor "IncomeValidationError" amount

      , button
          [ case Form.getOutput addIncome of
              Just data ->
                onClick (Msg.UpdateLoggedIn <| LoggedInMsg.AddIncome data.creation data.amount)
              Nothing ->
                onClick (Msg.UpdateLoggedIn <| LoggedInMsg.IncomeMsg <| IncomeMsg.AddIncomeMsg <| Form.Submit)
          ]
          [ text (getMessage "Add" loggedData.translations) ]
      ]

incomesView : LoggedData -> Html Msg
incomesView loggedData =
  ul
    []
    ( loggedData.incomes
        |> Dict.toList
        |> List.filter ((==) loggedData.me << .userId << snd)
        |> List.sortBy (.creation << snd)
        |> List.reverse
        |> List.map (incomeView loggedData)
    )

incomeView : LoggedData -> (IncomeId, Income) -> Html Msg
incomeView loggedData (incomeId, income) =
  li
    []
    [ text <| renderShortDate (Date.fromTime income.creation) loggedData.translations
    , text "    −    "
    , text <| Format.price loggedData.conf income.amount
    , text "    −    "
    , button
        [ onClick (Msg.UpdateLoggedIn <| LoggedInMsg.DeleteIncome incomeId) ]
        [ text "x" ]
    ]