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
|
module View.Category.Table
( view
, In(..)
, Out(..)
) where
import qualified Data.Maybe as Maybe
import Data.Text (Text)
import qualified Data.Text as T
import Reflex.Dom (Dynamic, Event, MonadWidget)
import qualified Reflex.Dom as R
import Common.Model (Category (..), CategoryId, Currency,
User (..), UserId)
import qualified Common.Model as CM
import qualified Common.Msg as Msg
import qualified Common.View.Format as Format
import qualified Component.ConfirmDialog as ConfirmDialog
import qualified Component.Table as Table
import qualified Component.Tag as Tag
import qualified Util.Ajax as Ajax
import qualified Util.Either as EitherUtil
import qualified View.Category.Form as Form
data In t = In
{ _in_currentUser :: UserId
, _in_currency :: Currency
, _in_categories :: [Category]
, _in_usedCategories :: [CategoryId]
, _in_users :: [User]
}
data Out t = Out
{ _out_add :: Event t ()
, _out_edit :: Event t ()
, _out_delete :: Event t ()
}
view :: forall t m. MonadWidget t m => In t -> m (Out t)
view input = do
table <- Table.view $ Table.In
{ Table._in_headerLabel = headerLabel
, Table._in_rows = _in_categories input
, Table._in_cell = cell (_in_users input) (_in_currency input)
, Table._in_cloneModal = \category ->
Form.view $ Form.In
{ Form._in_operation = Form.Clone category
}
, Table._in_editModal = \category ->
Form.view $ Form.In
{ Form._in_operation = Form.Edit category
}
, Table._in_deleteModal = \category ->
ConfirmDialog.view $ ConfirmDialog.In
{ ConfirmDialog._in_header = Msg.get Msg.Category_DeleteConfirm
, ConfirmDialog._in_confirm = \e -> do
res <- Ajax.delete
(R.constDyn $ T.concat ["/api/category/", T.pack . show $ _category_id category])
e
return $ () <$ R.fmapMaybe EitherUtil.eitherToMaybe res
}
, Table._in_canEdit = const True
, Table._in_canDelete = not . flip elem (_in_usedCategories input) . _category_id
}
return $ Out
{ _out_add = Table._out_add table
, _out_edit = Table._out_edit table
, _out_delete = Table._out_delete table
}
data Header
= NameHeader
| ColorHeader
deriving (Eq, Show, Bounded, Enum)
headerLabel :: Header -> Text
headerLabel NameHeader = Msg.get Msg.Category_Name
headerLabel ColorHeader = Msg.get Msg.Category_Color
cell :: forall t m. MonadWidget t m => [User] -> Currency -> Header -> Category -> m ()
cell users currency header category =
case header of
NameHeader ->
R.text $ _category_name category
ColorHeader ->
Tag.view $ Tag.In
{ Tag._in_text = _category_name category
, Tag._in_color = _category_color category
}
|