Skip to content

Commit

Permalink
Fix voting
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesread committed Jan 6, 2025
1 parent 463107d commit 9a7f338
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 25 deletions.
1 change: 1 addition & 0 deletions database/migrations/0.base.sql
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ CREATE TABLE `votes` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`delta` int(11) NOT NULL,
`quote` int(11) NOT NULL,
`user` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;
Expand Down
4 changes: 2 additions & 2 deletions src/includes/templates/quote.tpl
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<section id = "quote{$quote->id}" class = "quote">
{if $isVotingEnabled}
<div class = "voteContainer">
<a class = "voteButton" onclick = "voteUp({$quote->id})" class = "up">&#9650;</a>
<button class = "voteButton" onclick = "voteUp({$quote->id});">&#9650;</button>
<span class = "voteCount">{$quote->voteCount}</span>
<a class = "voteButton" onclick = "voteDown({$quote->id})" class = "up">&#9660;</a>
<button class = "voteButton" onclick = "voteDown({$quote->id});">&#9660;</button>
</div>
{/if}

Expand Down
21 changes: 14 additions & 7 deletions src/resources/javascript/main.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
function vote(id, dir)
{
function voteUp (id) {
return vote(id, 'up')
}

function voteDown (id) {
return vote(id, 'down')
}

function vote (id, dir) {
window.fetch('vote.php', {
method: 'POST',
headers: {
Expand All @@ -13,17 +20,18 @@ function vote(id, dir)
.then(response => response.json())
.then(onVoteReply)
.catch(onError)

return false // prevent default
}

function onError(res)
{
function onError (res) {
console.log('err', res)

document.querySelectorAll('p.error').forEach(function (el) {
el.remove()
})

if (typeof res.message != "undefined") {
if (typeof res.message !== 'undefined') {
const p = document.createElement('p')
p.classList.add('error')
p.textContent = 'Error: ' + res.message
Expand All @@ -35,8 +43,7 @@ function onError(res)
}
}

function onVoteReply(json)
{
function onVoteReply (json) {
if (json.type === 'error') {
if (json.cause === 'needsLogin') {
window.location = 'login.php'
Expand Down
14 changes: 11 additions & 3 deletions src/resources/stylesheets/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ section *:last-child {
margin-bottom: 0;
}

.quoteContainer {
flex-grow: 1;
}

.quoteContainer ul {
padding: 0;
margin: 0;
Expand Down Expand Up @@ -121,6 +125,7 @@ div.container {

section.quote {
margin-bottom: 2.5em;
display: flex;
}

.quoteContainer {
Expand All @@ -136,16 +141,19 @@ div.voteContainer {
min-width: 2em;
}

div.voteContainer a {
div.voteContainer button {
display: block;
color: lightgray;
cursor: pointer;
border: 0;
font-size: 1.5em;
text-decoration: none;
background-color: transparent;
}

div.voteContainer a:hover {
color: gray;
div.voteContainer button:hover {
color: black;
background-color: transparent;
}

div.quoteHeader {
Expand Down
26 changes: 13 additions & 13 deletions src/vote.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use libAllure\Session;
use libAllure\DatabaseFactory;

if (!$cfg->getBool('VOTING_ENABLED')) {
if ($cfg->getBool('VOTING_ENABLED')) {
outputJson(
array(
"type" => "error",
Expand All @@ -18,8 +18,11 @@
$cause = "";

try {
$dir = libAllure\Shortcuts::san()->filterString('dir');
$id = libAllure\Shortcuts::san()->filterUint('id');
$jsonData = file_get_contents('php://input');
$data = json_decode($jsonData, true);

$dir = $data['direction'];
$id = $data['id'];

switch ($dir) {
case 'up':
Expand All @@ -29,7 +32,7 @@
$delta = -1;
break;
default:
throw new Exception('What direction is that?!');
throw new Exception('What direction is that?! ');
}

if (!Session::isLoggedIn()) {
Expand All @@ -43,16 +46,13 @@
$stmt->execute();

if ($stmt->numRows() > 0) {
$currentVote = $stmt->fetchRow();
$currentVote['delta'] = intval($currentVote['delta']);

$delta = $currentVote['delta'] + $delta;
$currentRow = $stmt->fetchRow();

if ($delta > 1) {
$delta = 1;
} elseif ($delta < -1) {
$delta = -1;
}
$sql = 'DELETE FROM votes WHERE quote = :quote AND user = :user';
$stmt = DatabaseFactory::getInstance()->prepare($sql);
$stmt->bindValue('quote', $id);
$stmt->bindValue('user', Session::getUser()->getId());
$stmt->execute();
}

$sql = 'INSERT INTO votes (quote, user, delta) VALUES (:quote, :user, :delta1) ON DUPLICATE KEY UPDATE delta = :delta2';
Expand Down

0 comments on commit 9a7f338

Please # to comment.