aboutsummaryrefslogtreecommitdiff
path: root/src/client/Dialog/AddCategory/Model.elm
blob: 3b70482e1ad0a1c8b751d92459de6e783a285c7b (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
module Dialog.AddCategory.Model exposing
  ( Model
  , init
  , initialAdd
  , initialClone
  , initialEdit
  , validation
  )

import Date exposing (Date)
import Dict

import Form exposing (Form)
import Form.Field as Field exposing (Field)
import Form.Validate as Validate exposing (Validation)

import Model.Category exposing (Categories, Category, CategoryId)
import Model.Translations exposing (Translations)
import Validation
import View.Date as Date

type alias Model =
  { id : Maybe CategoryId
  , name : String
  , color : String
  }

init : Form String Model
init = Form.initial [] validation

initialAdd : Translations -> List (String, Field)
initialAdd translations =
  [ ("color", Field.string "#000000")
  ]

initialClone : Translations -> Category -> List (String, Field)
initialClone translations category =
  [ ("name", Field.string category.name)
  , ("color", Field.string category.color)
  ]

initialEdit : Translations -> CategoryId -> Category -> List (String, Field)
initialEdit translations categoryId category =
  [ ("id", Field.string (toString categoryId))
  , ("name", Field.string category.name)
  , ("color", Field.string category.color)
  ]

validation : Validation String Model
validation =
  Validate.map3 Model
    (Validate.field "id" (Validate.maybe Validate.int))
    (Validate.field "name" (Validate.string |> Validate.andThen Validate.nonEmpty))
    (Validate.field "color" Validation.color)