summaryrefslogtreecommitdiff
path: root/Handler/Common.hs
blob: 1cf63dea1c5cd50d67650fcbe4ce71a3ff0b1197 (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
{-# LANGUAGE ApplicativeDo #-}

module Handler.Common
  ( inventoryListing
  , itemForm
  , referenceListing
  , referenceForm
  , kinds
  , InventoryState(..)
  , ReferenceState(..)
  , FormState(..)
  , HasFormState(..)
  ) where

import Import

import Data.Unique

import qualified Data.Text as Text (pack)

import Data.Set (Set)
import qualified Data.Set as Set

import Control.Lens

import Handler.Common.Types

dayFormat :: Day -> String
dayFormat = formatTime defaultTimeLocale "%e. %b %y"

itemForm :: Maybe Item -- ^ Update existing item or insert new?
         -> Html -> MForm Handler (FormResult (WithType Item), Widget)
itemForm proto identView = do
  today <- utctDay <$> liftIO getCurrentTime

  t <- lift . runDB $ maybe (return Nothing) (fmap (Just . kindType) . getType) proto
  
  (kindRes, kindWidget) <- kindField $ itemKind <$> proto
  (typeRes, typeWidget) <- typeField $ t
  (boughtRes, boughtWidget) <- dayForm (maybe (Just $ Just today) Just $ fmap itemBought proto) "Unknown"
  (expiresRes, expiresWidget) <- dayForm (fmap itemExpires proto) "Never"
  (openedRes, openedWidget) <- dayForm (fmap itemOpened proto) "Never"

  let itemRes = do
        itemKind <- kindRes
        itemBought <- boughtRes
        itemExpires <- expiresRes
        itemOpened <- openedRes
        t <- typeRes
        return $ Item{ itemNormKind = normalizeKind itemKind, ..} `WithType` t

  return . (itemRes, ) $
    [whamlet|
            $newline never
            #{identView}
            <div .td>^{kindWidget}
            <div .td>^{typeWidget}
            <div .td>^{boughtWidget}
            <div .td>^{expiresWidget}
            <div .td>^{openedWidget}
            |]
  where
    dayForm :: Maybe (Maybe Day) -> String -> MForm Handler (FormResult (Maybe Day), Widget)
    dayForm proto label = do
      today <- utctDay <$> liftIO getCurrentTime

      checkboxId <- ("check" <>) . show . hashUnique <$> liftIO newUnique
      
      (fmap (fromMaybe False) -> isNothingRes, isNothingView) <-
        mopt checkBoxField ("" { fsId = Just $ Text.pack checkboxId }) . Just . Just . fromMaybe True $ fmap isNothing proto
      (dayRes, dayView) <-
        mreq dayField "" . Just . fromMaybe today $ join proto

      let res = (bool Just (const Nothing) <$> isNothingRes) <*> dayRes
      return . (res, ) $ do
        [whamlet|
                $newline never
                <div .table>
                  <div .tr>
                    <label for=#{checkboxId} .checkbox .td>
                      ^{fvInput isNothingView}
                      <span>
                        #{label}
                  <div .tr>
                    <div .td .dayInput>^{fvInput dayView}
                |]

inventoryListing :: InventoryState -> Widget
inventoryListing InventoryState{ invFormState = formState, ..} = $(widgetFile "inventoryListing")

referenceForm :: Maybe Reference -- ^ Update existing item or insert new?
              -> Html -> MForm Handler (FormResult (WithType Reference), Widget)
referenceForm proto identView = do
  t <- lift . runDB $ maybe (return Nothing) (fmap (Just . kindType) . getType) proto

  (kindRes, kindWidget) <- kindField $ referenceKind <$> proto
  (typeRes, typeWidget) <- typeField $ t

  let referenceRes = do
        referenceKind <- kindRes
        t <- typeRes
        return $ Reference{ referenceNormKind = normalizeKind referenceKind, .. } `WithType` t

  return . (referenceRes, ) $
    [whamlet|
            $newline never
            #{identView}
            <div .td>^{kindWidget}
            <div .td>^{typeWidget}
            |]

referenceListing :: ReferenceState -> Widget
referenceListing ReferenceState{ refFormState = formState, ..} = $(widgetFile "referenceListing")

kindField :: Maybe Text -> MForm Handler (FormResult Text, Widget)
kindField proto = do
  optionId <- ("options" <>) . tshow . hashUnique <$> liftIO newUnique

  let
    attrs = [ ("list", optionId)
            , ("autocomplete", "off")
            ]
  
  (kindRes, kindView) <- mreq textField ("" { fsAttrs = attrs }) proto

  options <- lift kinds

  return . (kindRes, ) $
    [whamlet|
            $newline never
            ^{fvInput kindView}
            <datalist ##{optionId}>
              $forall opt <- options
                <option value=#{opt}>
            |]

typeField :: Maybe Text -> MForm Handler (FormResult Text, Widget)
typeField proto = do
  optionId <- ("options" <>) . tshow . hashUnique <$> liftIO newUnique

  let
    attrs = [ ("list", optionId)
            , ("autocomplete", "off")
            ]

  (typeRes, typeView) <- mreq textField ("" { fsAttrs = attrs }) proto

  (Set.fromList . map (kindType . entityVal) -> options) <- lift . runDB $ selectList [] []

  return . (typeRes, ) $
    [whamlet|
            $newline never
            ^{fvInput typeView}
            <datalist ##{optionId}>
              $forall opt <- Set.toList options
                <option value=#{opt}>
            |]

kinds :: Handler [Text]
kinds = do
  stock <- runDB $ selectList [] []
  reference <- runDB $ selectList [] []

  return $ concat
    [ [ itemKind | Entity _ Item{..} <- stock ]
    , [ referenceKind | Entity _ Reference{..} <- reference ]
    ]