Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attempt to fix existing bugs in endorsement feature #34

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
47 changes: 31 additions & 16 deletions public/src/client/topic/postTools.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';



define('forum/topic/postTools', [
'share',
'navigator',
Expand All @@ -19,15 +20,6 @@ define('forum/topic/postTools', [
PostTools.init = function (tid) {
staleReplyAnyway = false;

// Load Session Storage when the page loads
const postEndorseText = getTextFromLocalStorage('postEndorseText');
if (postEndorseText) {
$('[component="post/endorse"]').text(postEndorseText);
}
const topicEndorseText = getTextFromLocalStorage('topicEndorseText');
if (topicEndorseText) {
$('[component="topic/endorse"]').text(topicEndorseText);
}

renderMenu();

Expand All @@ -38,6 +30,16 @@ define('forum/topic/postTools', [
votes.addVoteHandler();

PostTools.updatePostCount(ajaxify.data.postcount);
// Load Session Storage when the page loads
const postEndorseText = getTextFromLocalStorage('postEndorseText');
if (postEndorseText) {
$('[component="post/endorse"]').text(postEndorseText);
}
// Load Endorsement text as soon as menu loads
const topicEndorseText = getTextFromLocalStorage('topicEndorseText');
if (topicEndorseText) {
$('[component="topic/endorse"]').text(topicEndorseText);
}
};
// Function to set the endorse messageText in local storage
function setTextInLocalStorage(key, text) {
Expand All @@ -47,21 +49,24 @@ define('forum/topic/postTools', [
function getTextFromLocalStorage(key) {
return sessionStorage.getItem(key);
}

function renderMenu() {
$('[component="topic"]').on('show.bs.dropdown', '.moderator-tools', function () {
const $this = $(this);
const dropdownMenu = $this.find('.dropdown-menu');
if (dropdownMenu.html()) {
return;
}

// Get endorsement message from session storage
const postEndorseText = getTextFromLocalStorage('postEndorseText');
if (postEndorseText) {
$('[component="post/endorse"]').text(postEndorseText);
}
// Load Endorsement text as soon as menu loads
const topicEndorseText = getTextFromLocalStorage('topicEndorseText');
// If message is not empty
if (topicEndorseText) {
// Append it to the viewport object
$('[component="topic/endorse"]').text(topicEndorseText);
}

const postEl = $this.parents('[data-pid]');
const pid = postEl.attr('data-pid');
const index = parseInt(postEl.attr('data-index'), 10);
Expand Down Expand Up @@ -122,6 +127,7 @@ define('forum/topic/postTools', [
var message = 'Someone thinks this is a good response(0)';
$(this).text(message);
const storageKey = 'postEndorseText';
// localStorage.setItem(storageKey, text);
setTextInLocalStorage(storageKey, message);
});

Expand All @@ -141,9 +147,6 @@ define('forum/topic/postTools', [
$('.topic').on('click', '[component="topic/endorse"]', function (e) {
e.preventDefault();
onEndorseClicked($(this), tid);
var message = 'Someone thinks this is a good response(0)';
const storageKey = 'topicEndorseText';
setTextInLocalStorage(storageKey, message);
});

$('.topic').on('click', '[component="topic/reply-as-topic"]', function () {
Expand All @@ -157,6 +160,18 @@ define('forum/topic/postTools', [

// Ansync Function to help us with the Click of Endorsed
async function onEndorseClicked(button, tid) {
// Fetch request to the designated route in the backend
fetch(window.location.href + '/isEndorsed', {
mathod: 'GET',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
}).then(res => res.json())
.then(data => console.log(data))
.catch(err => console.log(err));
// Throws an error if there is any
// Second part of the action of dispatching the associated hook
const selectedNode = await getSelectedNode();
showStaleWarning(async function () {
let username = await getUserSlug(button);
Expand Down
20 changes: 20 additions & 0 deletions src/controllers/topics.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
const nconf = require('nconf');
const qs = require('querystring');

// Import the DB object to allow us to use it
const db = require('../database');
const user = require('../user');
const meta = require('../meta');
const topics = require('../topics');
Expand Down Expand Up @@ -123,6 +125,24 @@ topicsController.get = async function getTopic(req, res, next) {
res.render('topic', topicData);
};

// Set up a whole new controller to update the DB when called upon
topicsController.topicIsEndorsed = async function (req, res) {
// Use a try-catch block to adhere to the existing async models
try {
// Get the topic ID
const topic_id = Number(req.params.topic_id);
// Asynchronously try to update the field in the DB
await db.setObjectField(`topic: ${topic_id}`, 'endorsed', true);
// Use the get route to receive data related to post
const ret = await topicsController.get(req, req.params);
// Dispatch the appropriate API Response
helpers.formatApiResponse(200, res, ret);
} catch (err) {
// Log to the console any errors we get and dispatch the
// relevant API response
helpers.formatApiResponse(500, res, { err: 'Unable to update DB field' });
}
};
function generateQueryString(query) {
const qString = qs.stringify(query);
return qString.length ? `?${qString}` : '';
Expand Down
2 changes: 2 additions & 0 deletions src/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ _mounts.globalMod = (app, middleware, controllers) => {
_mounts.topic = (app, name, middleware, controllers) => {
setupPageRoute(app, `/${name}/:topic_id/:slug/:post_index?`, [], controllers.topics.get);
setupPageRoute(app, `/${name}/:topic_id/:slug?`, [], controllers.topics.get);
// Set up a route to handle endorse requests
setupPageRoute(app, `/${name}/:topic_id/:slug/isEndorsed`, [], controllers.topics.topicIsEndorsed);
};

_mounts.post = (app, name, middleware, controllers) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,6 @@

<div class="content" component="post/content" itemprop="text">
{posts.content}
{{{ if posts.endorsed_by_Instructor }}}
<p style="text-align: right; color: blue;">This response has been Endorsed !</p>
{{{ end }}}
</div>

<div class="post-footer">
Expand All @@ -78,7 +75,7 @@
<i class="fa fa-fw fa-spin fa-spinner hidden" component="post/replies/loading"></i>
</a>
{{{ end }}}

<small class="pull-right">
<!-- IMPORT partials/topic/reactions.tpl -->
<span class="post-tools">
Expand Down