Added like button functionality (#15)

This commit is contained in:
Dan Burton 2014-11-11 11:31:40 -08:00 committed by Chris Done
parent 3d8852dfcd
commit 9c229d2d4e
6 changed files with 62 additions and 4 deletions

View File

@ -10,6 +10,7 @@ import qualified Data.Text.Encoding as T
import Data.Time (addUTCTime)
import Database.Esqueleto ((^.), (&&.), Value (Value))
import qualified Database.Esqueleto as E
import qualified Database.Persist as P
import Formatting
import Import
import Text.Email.Validate
@ -22,7 +23,8 @@ getPackageR pn = do
asInt = id
haddocksLink ident version =
HaddockR ident [concat [toPathPiece pn, "-", toPathPiece version]]
(packages, downloads, recentDownloads, Entity _ metadata) <- runDB $ do
muid <- maybeAuthId
(packages, downloads, recentDownloads, nLikes, liked, Entity _ metadata) <- runDB $ do
packages <- fmap (map reformat) $ E.select $ E.from $ \(p, s) -> do
E.where_ $ (p ^. PackageStackage E.==. s ^. StackageId)
&&. (p ^. PackageName' E.==. E.val pn)
@ -31,12 +33,19 @@ getPackageR pn = do
E.limit maxSnaps
--selectList [PackageName' ==. pn] [LimitTo 10, Desc PackageStackage]
return (p ^. PackageVersion, s ^. StackageTitle, s ^. StackageIdent, s ^. StackageHasHaddocks)
nLikes <- count [LikePackage ==. pn]
let getLiked uid = (>0) <$> count [LikePackage ==. pn, LikeVoter ==. uid]
liked <- maybe (return False) getLiked muid
downloads <- count [DownloadPackage ==. pn]
now <- liftIO getCurrentTime
let nowMinus30 = addUTCTime (-30 * 24 * 60 * 60) now
recentDownloads <- count [DownloadPackage ==. pn, DownloadTimestamp >=. nowMinus30]
metadata <- getBy404 (UniqueMetadata pn)
return (packages, downloads, recentDownloads, metadata)
return (packages, downloads, recentDownloads, nLikes, liked, metadata)
let likedClass = if liked then "fa-thumbs-up" else "fa-thumbs-o-up" :: Text
let deps = enumerate (metadataDeps metadata)
authors = enumerate (parseIdentitiesLiberally (metadataAuthor metadata))
maintainers = let ms = enumerate (parseIdentitiesLiberally (metadataMaintainer metadata))
@ -151,3 +160,19 @@ renderEmail = T.decodeUtf8 . toByteString
-- | Format a number with commas nicely.
formatNum :: Int -> Text
formatNum = sformat commas
postPackageLikeR :: PackageName -> Handler ()
postPackageLikeR packageName = maybeAuthId >>= \muid -> case muid of
Nothing -> return ()
Just uid -> do
runDB $ P.insert $ Like packageName uid
return ()
postPackageUnlikeR :: PackageName -> Handler ()
postPackageUnlikeR name = maybeAuthId >>= \muid -> case muid of
Nothing -> return ()
Just uid -> do
runDB $ E.delete $ E.from $ \like ->
E.where_ $ like ^. LikePackage E.==. E.val name
&&. like ^. LikeVoter E.==. E.val uid
return ()

View File

@ -47,6 +47,10 @@ Tag
tag Slug
voter UserId
Like
package PackageName
voter UserId
Download
ident PackageSetIdent Maybe
view HackageView Maybe

View File

@ -27,3 +27,5 @@
/package/#PackageName PackageR GET
/package PackageListR GET
/compressor-status CompressorStatusR GET
/package/#PackageName/like PackageLikeR POST
/package/#PackageName/unlike PackageUnlikeR POST

View File

@ -32,9 +32,11 @@ $newline never
<button .btn>Add
<div .social>
<span #likes>
#{formatNum 234}
#{nLikes}
\ likes #
<i class="fa fa-thumbs-o-up" onclick="this.className = 'fa fa-thumbs-up'; document.getElementById('likes').innerText = '235'"></i>
$if isJust muid
<i .fa.#{likedClass}
onclick="toggleLike(this)"></i>
$if downloads /= 0
<span .downloads>
$if downloads /= recentDownloads

View File

@ -9,3 +9,25 @@ $(function(){
}
});
});
function toggleLike(el) {
var $this = $(el);
var action = 'like';
if ($this.hasClass('fa-thumbs-up')) {
action = 'unlike';
}
$this.toggleClass('fa-thumbs-up');
$this.toggleClass('fa-thumbs-o-up');
$likes = $('#likes');
nLikes = parseInt($likes.text(), 10);
if (action == 'like') {
$likes.text(nLikes + 1);
$.post("@{PackageLikeR pn}");
} else {
$likes.text(nLikes - 1);
$.post("@{PackageUnlikeR pn}");
}
}

View File

@ -49,6 +49,9 @@ h3 {
.fa-thumbs-o-up:hover {
color: #3f4549
}
.fa-thumbs-up {
cursor: pointer;
}
}
.dependencies {
margin-top: 0.5em;