From 9bee6a718b0224ba0b129150a695139b6f07160c Mon Sep 17 00:00:00 2001 From: Jon Schoning Date: Mon, 15 Jun 2020 09:45:33 -0500 Subject: [PATCH] use named record constructors --- src/Handler/Add.hs | 32 ++++++++++++++++++++------------ src/Handler/Notes.hs | 5 ++++- src/Model.hs | 12 ++++++------ 3 files changed, 30 insertions(+), 19 deletions(-) diff --git a/src/Handler/Add.hs b/src/Handler/Add.hs index 8e72fcf..a2ac1a7 100644 --- a/src/Handler/Add.hs +++ b/src/Handler/Add.hs @@ -30,18 +30,26 @@ getAddViewR = do bookmarkFormUrl :: Handler BookmarkForm bookmarkFormUrl = do Entity _ user <- requireAuth - BookmarkForm - <$> (lookupGetParam "url" >>= pure . fromMaybe "") - <*> (lookupGetParam "title") - <*> (lookupGetParam "description" >>= pure . fmap Textarea) - <*> (lookupGetParam "tags") - <*> (lookupGetParam "private" >>= pure . fmap parseChk <&> (<|> Just (userPrivateDefault user))) - <*> (lookupGetParam "toread" >>= pure . fmap parseChk) - <*> pure Nothing - <*> pure Nothing - <*> pure Nothing - <*> pure Nothing - <*> pure Nothing + url <- lookupGetParam "url" >>= pure . fromMaybe "" + title <- lookupGetParam "title" + description <- lookupGetParam "description" >>= pure . fmap Textarea + tags <- lookupGetParam "tags" + private <- lookupGetParam "private" >>= pure . fmap parseChk <&> (<|> Just (userPrivateDefault user)) + toread <- lookupGetParam "toread" >>= pure . fmap parseChk + pure $ + BookmarkForm + { _url = url + , _title = title + , _description = description + , _tags = tags + , _private = private + , _toread = toread + , _bid = Nothing + , _slug = Nothing + , _selected = Nothing + , _time = Nothing + , _archiveUrl = Nothing + } where parseChk s = s == "yes" || s == "on" diff --git a/src/Handler/Notes.hs b/src/Handler/Notes.hs index c0666ec..20a9d2a 100644 --- a/src/Handler/Notes.hs +++ b/src/Handler/Notes.hs @@ -69,7 +69,7 @@ getAddNoteViewR :: UserNameP -> Handler Html getAddNoteViewR unamep@(UserNameP uname) = do userId <- requireAuthId let renderEl = "note" :: Text - note <- liftIO $ Entity (NoteKey 0) <$> _toNote userId (NoteForm Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing) + note <- liftIO $ Entity (NoteKey 0) <$> _toNote userId emptyNoteForm defaultLayout $ do $(widgetFile "note") toWidgetBody [julius| @@ -129,6 +129,9 @@ instance ToJSON NoteForm where toJSON = A.genericToJSON gNoteFormOptions gNoteFormOptions :: A.Options gNoteFormOptions = A.defaultOptions { A.fieldLabelModifier = drop 1 } +emptyNoteForm :: NoteForm +emptyNoteForm = NoteForm Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing + _toNote :: UserId -> NoteForm -> IO Note _toNote userId NoteForm {..} = do time <- liftIO getCurrentTime diff --git a/src/Model.hs b/src/Model.hs index 52f49b2..eede372 100644 --- a/src/Model.hs +++ b/src/Model.hs @@ -694,12 +694,12 @@ _toBookmark userId BookmarkForm {..} = do { bookmarkUserId = userId , bookmarkSlug = slug , bookmarkHref = _url - , bookmarkDescription = (fromMaybe "" _title) - , bookmarkExtended = (maybe "" unTextarea _description) - , bookmarkTime = (fromMaybe time (fmap unUTCTimeStr _time)) - , bookmarkShared = (maybe True not _private) - , bookmarkToRead = (fromMaybe False _toread) - , bookmarkSelected = (fromMaybe False _selected) + , bookmarkDescription = fromMaybe "" _title + , bookmarkExtended = maybe "" unTextarea _description + , bookmarkTime = fromMaybe time (fmap unUTCTimeStr _time) + , bookmarkShared = maybe True not _private + , bookmarkToRead = fromMaybe False _toread + , bookmarkSelected = fromMaybe False _selected , bookmarkArchiveHref = _archiveUrl }