aboutsummaryrefslogtreecommitdiff
path: root/src/client/elm/View/LoggedIn/Account.elm
blob: 9459740bd7cd04dbd21fd1409b49233bedc0f699 (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
129
130
131
module View.LoggedIn.Account
  ( account
  ) where

import List
import Signal exposing (Address)

import Html exposing (..)
import Html as H exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (..)

import Model exposing (Model)
import Model.User exposing (getUserName)
import Model.Payer exposing (..)
import Model.Translations exposing (getParamMessage, getMessage)
import Model.Action exposing (..)
import Model.Action.LoggedInAction exposing (..)
import Model.Action.AccountAction exposing (..)
import Model.Communication as Communication

import Model.View.LoggedInView exposing (LoggedInView)
import Model.View.LoggedIn.Account exposing (..)

import View.Expand exposing (..)
import View.Price exposing (price)
import View.Events exposing (onSubmitPrevDefault)

import Utils.Either exposing (toMaybeError)

account : Address Action -> Model -> LoggedInView -> Html
account address model loggedInView =
  let account = loggedInView.account
  in  div
        [ classList
            [ ("account", True)
            , ("detail", account.visibleDetail)
            ]
        ]
        [ exceedingPayers address model loggedInView
        , if account.visibleDetail
            then income address model account
            else text ""
        ]

exceedingPayers : Address Action -> Model -> LoggedInView -> Html
exceedingPayers address model loggedInView =
  button
    [ class "header"
    , onClick address (UpdateLoggedIn << UpdateAccount <| ToggleDetail)
    ]
    (  (List.map (exceedingPayer model loggedInView) (getOrderedExceedingPayers model.currentTime loggedInView.account.payers))
    ++ [ expand ExpandDown loggedInView.account.visibleDetail ]
    )

exceedingPayer : Model -> LoggedInView -> ExceedingPayer -> Html
exceedingPayer model loggedInView payer =
  div
    [ class "exceedingPayer" ]
    [ span
        [ class "userName" ]
        [ payer.userId
            |> getUserName loggedInView.users
            |> Maybe.withDefault "−"
            |> text
        ]
    , span
        [ class "amount" ]
        [ text ("+ " ++ (price model payer.amount)) ]
    ]

income : Address Action -> Model -> Account -> Html
income address model account =
  case account.incomeEdition of
    Nothing ->
      incomeRead address model account
    Just edition ->
      incomeEdition address model account edition

incomeRead : Address Action -> Model -> Account -> Html
incomeRead address model account =
  div
    [ class "income" ]
    [ ( case getCurrentIncome account of
          Nothing ->
            text (getMessage "NoIncome" model.translations)
          Just income ->
            text (getParamMessage [price model income] "Income" model.translations)
      )
    , toggleIncomeEdition address "editIncomeEdition" (getMessage "Edit" model.translations)
    ]

incomeEdition : Address Action -> Model -> Account -> IncomeEdition -> Html
incomeEdition address model account edition =
  H.form
    [ case validateIncome edition.income model.translations of
        Ok validatedAmount ->
          onSubmitPrevDefault address (ServerCommunication <| Communication.SetIncome model.currentTime validatedAmount)
        Err error ->
          onSubmitPrevDefault address (UpdateLoggedIn << UpdateAccount << UpdateEditionError <| error)
    , class "income"
    ]
    [ label
        [ for "incomeInput" ]
        [ text (getMessage "NewIncome" model.translations) ]
    , input
        [ id "incomeInput"
        , value edition.income
        , on "input" targetValue (Signal.message address << UpdateLoggedIn << UpdateAccount << UpdateIncomeEdition)
        , maxlength 10
        ]
        []
    , button
        [ type' "submit"
        , class "validateIncomeEdition"
        ]
        [ text (getMessage "Validate" model.translations) ]
    , toggleIncomeEdition address "undoIncomeEdition" (getMessage "Undo" model.translations)
    , case edition.error of
        Just error -> div [ class "error" ] [ text error ]
        Nothing -> text ""
    ]

toggleIncomeEdition : Address Action -> String -> String -> Html
toggleIncomeEdition address className name =
  button
    [ type' "button"
    , class className
    , onClick address (UpdateLoggedIn << UpdateAccount <| ToggleIncomeEdition)
    ]
    [ text name ]