Skip to content

Commit

Permalink
bringing in likes to display
Browse files Browse the repository at this point in the history
  • Loading branch information
jessewoo committed Aug 17, 2024
1 parent 2ec8460 commit f9f7c64
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 28 deletions.
46 changes: 22 additions & 24 deletions core/components/com_forum/api/router.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ public function build(&$query)
unset($query['controller']);
}

if (!empty($query['id']))
{
$segments[] = $query['id'];
unset($query['id']);
}

if (!empty($query['task']))
{
$segments[] = $query['task'];
Expand All @@ -46,32 +52,24 @@ public function build(&$query)
* @return array The URL attributes to be used by the application.
*/
public function parse(&$segments)
{
$vars = array();
{
$vars = array();

$vars['controller'] = 'threads';
if (empty($segments))
{
return $vars;
}

if (isset($segments[0]))
{
if (is_numeric($segments[0]))
{
$vars['id'] = $segments[0];
if (\App::get('request')->method() == 'GET')
{
$vars['task'] = 'read';
}
}
else
{
$vars['task'] = $segments[0];
}
if (isset($segments[0]))
{
$vars['id'] = $segments[0];
}

if (isset($segments[1]))
{
$vars['task'] = $segments[1];
}
}
if (isset($segments[1]))
{
$vars['task'] = $segments[1];
}

return $vars;
}
return $vars;
}
}
69 changes: 66 additions & 3 deletions core/components/com_forum/site/assets/js/like.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,74 @@ window.addEventListener('DOMContentLoaded', (domEvent) => {
likeButton[i].onclick = (e) => {
e.preventDefault();

const dataId = likeButton[i].dataset.id
console.log("data id: " + dataId);
let hasHeart = likeButton[i].classList.contains("userLiked");

const threadId = likeButton[i].dataset.thread;
const postId = likeButton[i].dataset.post;
const userId = likeButton[i].dataset.user;

console.log(threadId, postId, userId);

if (hasHeart) {
removeLike(threadId, postId, userId).then((res) => {
if (res.ok) {
likeButton[i].classList.remove("userLiked");
console.warn(`Like removed for forum thread '${threadId}' of post '${postId}' for user ${userId}`);
}
})
} else {
addLike(threadId, postId, userId).then((res) => {
if (res.ok) {
likeButton[i].classList.add("userLiked");
console.log(`Like recorded for forum thread '${threadId}' of post '${postId}' for user ${userId}`);
}
})
}

return false;
};
}
}
});
});

const addLike = async (threadId, postId, userId) => {
const postUrl = "/api/forum/likes/addLikeToPost";
const data = {threadId, postId, userId};

try {
let response = await fetch(postUrl, {
method: "POST", headers: {"Content-Type": "application/x-www-form-urlencoded"},
body: new URLSearchParams(data) // urlencoded form body
});

if (!response.ok) {
window.confirm("Server Error with API");
console.error(`Error Code: ${response.status} / Error Message: ${response.statusText}`);
}

return response;
} catch (error) {
if (error instanceof SyntaxError) {
console.error('There was a SyntaxError', error);
} else {
console.error('There was an error', error);
}
}
};

const removeLike = async (threadId, postId, userId) => {
const deleteAssertionUrl = "/api/forum/likes/deleteLikeFromPost";
const data = {threadId, postId, userId};

const deleteAssertionResp = await fetch(deleteAssertionUrl, {
method: "DELETE", headers: {"Content-Type": "application/x-www-form-urlencoded"},
body: new URLSearchParams(data)
})

if (!deleteAssertionResp.ok) {
window.confirm("Server Error with API");
console.error(`Error Code: ${response.status} / Error Message: ${response.statusText}`);
}

return deleteAssertionResp;
}
11 changes: 11 additions & 0 deletions core/components/com_forum/site/controllers/threads.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,16 @@ public function displayTask()
// Set the pathway
$this->buildPathway($section, $category, $thread);

// Get all the likes of this thread
$db = \App::get('db');
$queryLikes = "SELECT LIKES.threadId as 'threadId', LIKES.postId as 'postId',
LIKES.userId as 'userId', USERS.name as 'userName', USERS.email as 'userEmail'
FROM jos_forum_posts_like as LIKES, jos_users AS USERS
WHERE LIKES.userId = USERS.id AND LIKES.threadId = " . $thread->get('id');

$db->setQuery($queryLikes);
$initialLikesList = $db->loadObjectList();

// Output the view
$this->view
->set('config', $this->config)
Expand All @@ -192,6 +202,7 @@ public function displayTask()
->set('category', $category)
->set('thread', $thread)
->set('filters', $filters)
->set('likes', $initialLikesList)
->setErrors($this->getErrors())
->display();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,10 @@
<p class="comment-options">
<!-- Like Comments -->
<!-- If user has liked it, should be red class="userLiked", if not gray -->
<a class="icon-heart like" data-id="c<?php echo $this->comment->get('id'); ?>" href="<?php echo Route::url($this->comment->link('like')); ?>">
<a class="icon-heart like" href="#"
data-thread="<?php echo $this->thread->get('id'); ?>"
data-post="<?php echo $this->comment->get('id'); ?>"
data-user="<?php echo User::get('id'); ?>" >
Like
</a>
<?php if ((!$this->comment->get('parent') && $this->config->get('access-delete-thread')) || ($this->comment->get('parent') && $this->config->get('access-delete-post'))) { ?>
Expand Down
2 changes: 2 additions & 0 deletions core/components/com_forum/site/views/threads/tmpl/display.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
$this->thread->set('section', $this->filters['section']);
$this->thread->set('category', $this->category->get('alias'));

print_r($this->likes);

$now = Date::of('now')->toSql();
?>
<header id="content-header">
Expand Down

0 comments on commit f9f7c64

Please sign in to comment.