aboutsummaryrefslogtreecommitdiff
path: root/src/client/elm/LoggedIn/User/View.elm
blob: 74e2ae2c0778a569acd5f6b7485b9fea47faf3f7 (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
module LoggedIn.User.View
  ( view
  ) where

import Dict
import Date

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

import LoggedData exposing (LoggedData)

import Model.Income exposing (IncomeId, Income)
import Model.Translations exposing (getMessage)
import LoggedIn.User.Model as UserModel

import Mailbox

import Action
import LoggedIn.Action as LoggedInAction
import LoggedIn.User.Action as UserAction

import LoggedIn.View.Date exposing (renderShortDate)
import LoggedIn.View.Price exposing (price)

import Utils.Maybe exposing (isJust)

view : LoggedData -> UserModel.Model -> Html
view loggedData userModel =
  div
    []
    [ h1 [] [ text <| getMessage "AddIncome" loggedData.translations ]
    , addIncomeView loggedData userModel.addIncome
    , h1 [] [ text <| getMessage "Incomes" loggedData.translations ]
    , incomesView loggedData
    ]

addIncomeView : LoggedData -> Form () UserModel.AddIncome -> Html
addIncomeView loggedData addIncome =
  let
    formAddress = Signal.forwardTo Mailbox.address (Action.UpdateLoggedIn << LoggedInAction.UserAction << UserAction.AddIncomeAction)
    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
  in
    div
      []
      [ label [] [ text "Creation" ]
      , Input.textInput creation formAddress []
      , errorFor "DateValidationError" creation

      , label [] [ text "amount" ]
      , Input.textInput amount formAddress []
      , errorFor "IncomeValidationError" amount

      , button
          [ case Form.getOutput addIncome of
              Just data ->
                onClick Mailbox.address (Action.UpdateLoggedIn <| LoggedInAction.AddIncome data.creation data.amount)
              Nothing ->
                onClick formAddress Form.Submit
          ]
          [ text (getMessage "Add" loggedData.translations) ]
      ]

incomesView : LoggedData -> Html
incomesView loggedData =
  ol
    []
    ( 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
incomeView loggedData (incomeId, income) =
  li
    []
    [ text <| renderShortDate (Date.fromTime income.creation) loggedData.translations
    , text "    −    "
    , text <| price loggedData.conf income.amount
    , text "    −    "
    , button
        [ onClick Mailbox.address (Action.UpdateLoggedIn <| LoggedInAction.DeleteIncome incomeId) ]
        [ text "x" ]
    ]