aboutsummaryrefslogtreecommitdiff
path: root/src/client/View/Payments/Add.elm
blob: d1431b8f8668a59ac82ad4fbf9c84eebc11c237f (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
module View.Payments.Add
  ( addPayment
  ) where

import Html as H exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (..)
import Reads exposing (readInt)
import Result exposing (..)

import ServerCommunication as SC exposing (serverCommunications)

import Update exposing (..)
import Update.Payment exposing (..)
import Update.Payment.Add exposing (..)

import Model.View.Payment.Add exposing (..)

import View.Events exposing (onSubmitPrevDefault)
import View.Icon exposing (renderIcon)

import Utils.Maybe exposing (isJust)
import Utils.Either exposing (toMaybeError)

addPayment : AddPayment -> Html
addPayment addPayment =
  H.form
    [ class "add"
    , case (validateName addPayment.name, validateCost addPayment.cost) of
        (Ok name, Ok cost) ->
          onSubmitPrevDefault serverCommunications.address (SC.AddPayment name cost)
        (resName, resCost) ->
          onSubmitPrevDefault actions.address (UpdatePayment <| UpdateAdd <| AddError (toMaybeError resName) (toMaybeError resCost))
    ]
    [ div
        [ class "name" ]
        [ input
            [ id "nameInput"
            , class (if isJust addPayment.nameError then "error" else "")
            , value addPayment.name
            , on "input" targetValue (Signal.message actions.address << UpdatePayment << UpdateAdd << UpdateName)
            ]
            []
        , label
            [ for "nameInput" ]
            [ renderIcon "shopping-cart" ]
        , case addPayment.nameError of
            Just error ->
              div [ class "errorMessage" ] [ text error ]
            Nothing ->
              text ""
        ]
    , div
        [ class "cost" ]
        [ input
            [ id "costInput"
            , class (if isJust addPayment.costError then "error" else "")
            , value addPayment.cost
            , on "input" targetValue (Signal.message actions.address << UpdatePayment << UpdateAdd << UpdateCost)
            ]
            []
        , label
            [ for "costInput" ]
            [ renderIcon "euro" ]
        , case addPayment.costError of
            Just error ->
              div [ class "errorMessage" ] [ text error ]
            Nothing ->
              text ""
        , button
            [ type' "submit" ]
            [ text "Add" ]
        ]
    ]