diff options
author | Gregor Kleen <gkleen@yggdrasil.li> | 2017-03-16 13:18:34 +0100 |
---|---|---|
committer | Gregor Kleen <gkleen@yggdrasil.li> | 2017-03-16 13:18:34 +0100 |
commit | 57d594818c14652681dce54d324b6b76941b2f4e (patch) | |
tree | f3e1e02e2fa41ff0d87eace034d6c883668841c5 /Handler | |
parent | 3d828feba67f21ae62d1e6eb598a22ffaebf1174 (diff) | |
parent | 3bfe0bdcb79b398a387e202c5150b5e6fd230d3a (diff) | |
download | bar-57d594818c14652681dce54d324b6b76941b2f4e.tar bar-57d594818c14652681dce54d324b6b76941b2f4e.tar.gz bar-57d594818c14652681dce54d324b6b76941b2f4e.tar.bz2 bar-57d594818c14652681dce54d324b6b76941b2f4e.tar.xz bar-57d594818c14652681dce54d324b6b76941b2f4e.zip |
Merge branch 'feat/openYes'
Diffstat (limited to 'Handler')
-rw-r--r-- | Handler/Common.hs | 102 | ||||
-rw-r--r-- | Handler/List.hs | 20 | ||||
-rw-r--r-- | Handler/OpenItem.hs | 2 |
3 files changed, 97 insertions, 27 deletions
diff --git a/Handler/Common.hs b/Handler/Common.hs index c2788e8..65e6ce1 100644 --- a/Handler/Common.hs +++ b/Handler/Common.hs | |||
@@ -34,6 +34,10 @@ import Database.Persist.Sql (fromSqlKey) | |||
34 | import qualified Web.Hashids as HID | 34 | import qualified Web.Hashids as HID |
35 | import Data.List ((\\)) | 35 | import Data.List ((\\)) |
36 | 36 | ||
37 | import Data.List.NonEmpty (NonEmpty) | ||
38 | import Data.Semigroup hiding (First(..)) | ||
39 | import Data.Monoid (First(..)) | ||
40 | |||
37 | humanId :: ItemId -> Text | 41 | humanId :: ItemId -> Text |
38 | humanId = Text.pack . CBS.unpack . HID.encode ctx . fromIntegral . fromSqlKey | 42 | humanId = Text.pack . CBS.unpack . HID.encode ctx . fromIntegral . fromSqlKey |
39 | where | 43 | where |
@@ -42,6 +46,12 @@ humanId = Text.pack . CBS.unpack . HID.encode ctx . fromIntegral . fromSqlKey | |||
42 | dayFormat :: Day -> String | 46 | dayFormat :: Day -> String |
43 | dayFormat = formatTime defaultTimeLocale "%e. %b %y" | 47 | dayFormat = formatTime defaultTimeLocale "%e. %b %y" |
44 | 48 | ||
49 | data DayFormConfig = DayFormConfig | ||
50 | { dfNever :: Bool | ||
51 | , dfUnknown :: Bool | ||
52 | , dfKnown :: Bool | ||
53 | } | ||
54 | |||
45 | itemForm :: Maybe Item -- ^ Update existing item or insert new? | 55 | itemForm :: Maybe Item -- ^ Update existing item or insert new? |
46 | -> Html -> MForm Handler (FormResult (WithType Item), Widget) | 56 | -> Html -> MForm Handler (FormResult (WithType Item), Widget) |
47 | itemForm proto identView = do | 57 | itemForm proto identView = do |
@@ -52,12 +62,12 @@ itemForm proto identView = do | |||
52 | let kt kWidget tWidget = | 62 | let kt kWidget tWidget = |
53 | [whamlet| | 63 | [whamlet| |
54 | <div .td>^{kWidget} | 64 | <div .td>^{kWidget} |
55 | <div .td>^{tWidget} | 65 | <div .td>^{tWidget} |
56 | |] | 66 | |] |
57 | ((kindRes, typeRes), typedKindWidget) <- typedKindField kt ((itemKind <$> proto), t) | 67 | ((kindRes, typeRes), typedKindWidget) <- typedKindField kt ((itemKind <$> proto), t) |
58 | (boughtRes, boughtWidget) <- dayForm (maybe (Just $ Just today) Just $ fmap itemBought proto) "Unknown" | 68 | (boughtRes, boughtWidget) <- dayForm (Just . fromMaybe (DateKnown today) $ itemBought <$> proto) $ DayFormConfig False True True |
59 | (expiresRes, expiresWidget) <- dayForm (fmap itemExpires proto) "Never" | 69 | (expiresRes, expiresWidget) <- dayForm (itemExpires <$> proto) $ DayFormConfig True False True |
60 | (openedRes, openedWidget) <- dayForm (fmap itemOpened proto) "Never" | 70 | (openedRes, openedWidget) <- dayForm (itemOpened <$> proto) $ DayFormConfig True True True |
61 | 71 | ||
62 | let itemRes = do | 72 | let itemRes = do |
63 | itemKind <- kindRes | 73 | itemKind <- kindRes |
@@ -77,33 +87,79 @@ itemForm proto identView = do | |||
77 | <div .td>^{openedWidget} | 87 | <div .td>^{openedWidget} |
78 | |] | 88 | |] |
79 | where | 89 | where |
80 | dayForm :: Maybe (Maybe Day) -> String -> MForm Handler (FormResult (Maybe Day), Widget) | 90 | dayForm :: Maybe ItemDate -> DayFormConfig -> MForm Handler (FormResult ItemDate, Widget) |
81 | dayForm proto label = do | 91 | dayForm proto DayFormConfig{..} = do |
82 | today <- utctDay <$> liftIO getCurrentTime | 92 | today <- utctDay <$> liftIO getCurrentTime |
83 | 93 | ||
84 | checkboxId <- ("check" <>) . show . hashUnique <$> liftIO newUnique | 94 | let mWhen = bool (Nothing <$) (fmap Just) |
85 | |||
86 | (fmap (fromMaybe False) -> isNothingRes, isNothingView) <- | ||
87 | mopt checkBoxField ("" { fsId = Just $ Text.pack checkboxId }) . Just . Just . fromMaybe True $ fmap isNothing proto | ||
88 | (dayRes, dayView) <- | ||
89 | mreq dayField "" . Just . fromMaybe today $ join proto | ||
90 | 95 | ||
91 | let res = (bool Just (const Nothing) <$> isNothingRes) <*> dayRes | 96 | neverBoxId <- ("check" <>) . show . hashUnique <$> liftIO newUnique |
97 | unknownBoxId <- ("check" <>) . show . hashUnique <$> liftIO newUnique | ||
98 | groupId <- ("dateGroup" <>) . show . hashUnique <$> liftIO newUnique | ||
99 | |||
100 | dNever <- mWhen dfNever $ | ||
101 | mopt checkBoxField ("" { fsId = Just $ Text.pack neverBoxId }) . Just . Just . fromMaybe True $ fmap isNever proto | ||
102 | dUnknown <- mWhen dfUnknown $ | ||
103 | mopt checkBoxField ("" { fsId = Just $ Text.pack unknownBoxId }) . Just . Just . fromMaybe False $ fmap isUnknown proto | ||
104 | dDay <- mWhen dfKnown $ | ||
105 | mopt dayField "" . Just . Just $ case proto of | ||
106 | Just (DateKnown d) -> d | ||
107 | _ -> today | ||
108 | |||
109 | let res = resFromMaybe . fromMaybe FormMissing . fmap (fmap getFirst) $ mconcat | ||
110 | [ fmap (fmap (First . bool Nothing (Just DateNever) . fromMaybe False) . fst) dNever | ||
111 | , fmap (fmap (First . bool Nothing (Just DateUnknown) . fromMaybe False) . fst) dUnknown | ||
112 | , fmap (fmap (First . fmap DateKnown) . fst) dDay | ||
113 | ] | ||
114 | resFromMaybe (FormSuccess Nothing) = FormFailure ["Missing required information"] | ||
115 | resFromMaybe (FormSuccess (Just x)) = FormSuccess x | ||
116 | resFromMaybe FormMissing = FormMissing | ||
117 | resFromMaybe (FormFailure es) = FormFailure es | ||
92 | return . (res, ) $ do | 118 | return . (res, ) $ do |
119 | toWidget $ | ||
120 | [julius| | ||
121 | $(function () { | ||
122 | var updateInput = function() { | ||
123 | $('##{rawJS groupId} :input').filter(':not(:checkbox)').prop("disabled", $('##{rawJS groupId} :checkbox').filter(':checked').length > 0); | ||
124 | }; | ||
125 | |||
126 | $('##{rawJS groupId} :checkbox').change(function() { | ||
127 | if (this.checked) { | ||
128 | $('##{rawJS groupId} :checkbox').not(this).prop('checked', false); | ||
129 | } | ||
130 | updateInput(); | ||
131 | }); | ||
132 | |||
133 | updateInput(); | ||
134 | }); | ||
135 | |] | ||
136 | let width = length $ (filter id [ isJust dNever, isJust dUnknown ] :: [Bool]) | ||
93 | [whamlet| | 137 | [whamlet| |
94 | $newline never | 138 | $newline never |
95 | <div .table> | 139 | <table ##{groupId} .dayField> |
96 | <div .tr> | 140 | <tr> |
97 | <label for=#{checkboxId} .checkbox .td> | 141 | $maybe (_, isNeverView) <- dNever |
98 | ^{fvInput isNothingView} | 142 | <td> |
99 | <span> | 143 | <label for=#{neverBoxId} .checkbox> |
100 | #{label} | 144 | ^{fvInput isNeverView} |
101 | <div .tr> | 145 | <span> |
102 | <div .td .dayInput>^{fvInput dayView} | 146 | Never |
147 | $maybe (_, isUnknownView) <- dUnknown | ||
148 | <td> | ||
149 | <label for=#{unknownBoxId} .checkbox> | ||
150 | ^{fvInput isUnknownView} | ||
151 | <span> | ||
152 | Unknown | ||
153 | $maybe (_, dayView) <- dDay | ||
154 | <tr> | ||
155 | <td .dayInput :width > 0:colspan=#{width}> | ||
156 | ^{fvInput dayView} | ||
103 | |] | 157 | |] |
104 | 158 | ||
105 | inventoryListing :: InventoryState -> Widget | 159 | inventoryListing :: InventoryState -> Widget |
106 | inventoryListing InventoryState{ invFormState = formState, ..} = $(widgetFile "inventoryListing") | 160 | inventoryListing InventoryState{ invFormState = formState, ..} = do |
161 | today <- liftIO $ utctDay <$> getCurrentTime | ||
162 | $(widgetFile "inventoryListing") | ||
107 | 163 | ||
108 | referenceForm :: Maybe Reference -- ^ Update existing item or insert new? | 164 | referenceForm :: Maybe Reference -- ^ Update existing item or insert new? |
109 | -> Html -> MForm Handler (FormResult (WithType Reference), Widget) | 165 | -> Html -> MForm Handler (FormResult (WithType Reference), Widget) |
@@ -199,7 +255,7 @@ kinds = do | |||
199 | stock <- runDB $ selectList [] [] | 255 | stock <- runDB $ selectList [] [] |
200 | reference <- runDB $ selectList [] [] | 256 | reference <- runDB $ selectList [] [] |
201 | 257 | ||
202 | return . Set.fromList $ concat | 258 | return . Set.fromList $ (concat :: [[a]] -> [a]) |
203 | [ [ itemKind | Entity _ Item{..} <- stock ] | 259 | [ [ itemKind | Entity _ Item{..} <- stock ] |
204 | , [ referenceKind | Entity _ Reference{..} <- reference ] | 260 | , [ referenceKind | Entity _ Reference{..} <- reference ] |
205 | ] | 261 | ] |
diff --git a/Handler/List.hs b/Handler/List.hs index 21b735b..7ab4ebe 100644 --- a/Handler/List.hs +++ b/Handler/List.hs | |||
@@ -19,9 +19,23 @@ import Database.Persist.Sql (Single(..), rawSql) | |||
19 | import Thermoprint.Client | 19 | import Thermoprint.Client |
20 | 20 | ||
21 | list :: Handler (Set (WithType Text)) | 21 | list :: Handler (Set (WithType Text)) |
22 | list = do | 22 | list = runDB $ do |
23 | (map (uncurry WithType . bimap unSingle unSingle) -> kinds) <- runDB $ rawSql "select reference.kind, kind.type from reference,kind where (not exists (select * from item where COALESCE(item.expires >= CURRENT_DATE, TRUE) and (item.norm_kind = reference.norm_kind))) and (reference.norm_kind = kind.norm_kind)" [] | 23 | today <- liftIO $ utctDay <$> getCurrentTime |
24 | return $ Set.fromList kinds | 24 | |
25 | items <- map entityVal <$> selectList [] [] | ||
26 | references <- Set.fromList <$> (withTypes . fmap entityVal =<< selectList [] []) | ||
27 | |||
28 | let | ||
29 | references' = Set.filter (isNothing . flip find items . matches) references | ||
30 | matches (Reference{..} `WithType` _) Item{..} | ||
31 | | today `isBefore` itemExpires = itemNormKind == referenceNormKind | ||
32 | | otherwise = False | ||
33 | |||
34 | isBefore _ DateNever = True | ||
35 | isBefore _ DateUnknown = False | ||
36 | isBefore d1 (DateKnown d2) = d1 < d2 | ||
37 | |||
38 | return $ Set.map (fmap referenceKind) references' | ||
25 | 39 | ||
26 | mkPrintout :: Set (WithType Text) -> Printout | 40 | mkPrintout :: Set (WithType Text) -> Printout |
27 | mkPrintout list = Printout ps | 41 | mkPrintout list = Printout ps |
diff --git a/Handler/OpenItem.hs b/Handler/OpenItem.hs index 468c6ec..3b1dfeb 100644 --- a/Handler/OpenItem.hs +++ b/Handler/OpenItem.hs | |||
@@ -5,7 +5,7 @@ import Import | |||
5 | postOpenItemR :: ItemId -> Handler TypedContent | 5 | postOpenItemR :: ItemId -> Handler TypedContent |
6 | postOpenItemR itemId = do | 6 | postOpenItemR itemId = do |
7 | today <- utctDay <$> liftIO getCurrentTime | 7 | today <- utctDay <$> liftIO getCurrentTime |
8 | result <- fmap (Entity itemId) . runDB $ updateGet itemId [ ItemOpened =. Just today | 8 | result <- fmap (Entity itemId) . runDB $ updateGet itemId [ ItemOpened =. DateKnown today |
9 | ] | 9 | ] |
10 | selectRep $ do | 10 | selectRep $ do |
11 | provideJson result | 11 | provideJson result |