Skip to content

Commit

Permalink
Merge pull request #29 from CMU-313/search-combine
Browse files Browse the repository at this point in the history
Added filtering on search query for search bar component
  • Loading branch information
heyanuja authored Oct 9, 2024
2 parents e33ee67 + d4a2d61 commit a205c22
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 5 deletions.
39 changes: 35 additions & 4 deletions public/src/client/category.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

define('forum/category', [
'forum/infinitescroll',
'benchpress',
'share',
'navigator',
'topicList',
Expand All @@ -10,7 +11,7 @@ define('forum/category', [
'hooks',
'alerts',
'api',
], function (infinitescroll, share, navigator, topicList, sort, categorySelector, hooks, alerts, api) {
], function (infinitescroll, Benchpress, share, navigator, topicList, sort, categorySelector, hooks, alerts, api) {
const Category = {};

$(window).on('action:ajaxify.start', function (ev, data) {
Expand Down Expand Up @@ -53,8 +54,8 @@ define('forum/category', [
hooks.fire('action:topics.loaded', { topics: ajaxify.data.topics });
hooks.fire('action:category.loaded', { cid: ajaxify.data.cid });

$('#category-search-text').on('keyup', () => console.log('typing?'));
$('#category-search-button').on('click', () => console.log('clicking?'));
// Triggers searching
$('#category-search-text').on('keyup', Category.search);
};

function handleScrollToTopicIndex() {
Expand Down Expand Up @@ -124,8 +125,38 @@ define('forum/category', [
navigator.scrollBottom(count - 1);
};

// Triggered when key pressed on searchbar
Category.search = function () {
// HTML elements
const topicsEl = $('.topics-list');
const queryEl = $('#category-search-text');

// Get all topics
socket.emit('topics.getTopicsByCid', {
cid: ajaxify.data.cid,
}, function (err, topics) {
if (err) {
return alerts.error(err);
}

// Only keep topics that include search query
topics = topics.filter(topic => topic.title.toLowerCase().includes(queryEl.val().toLowerCase()));

// Shove topics back into HTML and render it
Benchpress.render('partials/topics_list', {
set: 'topics',
query: queryEl.val(),
topics: topics || [],
}).then(function (html) {
topicsEl.empty().append(html);
});
});

return false;
};

function loadTopicsAfter(after, direction, callback) {
callback = callback || function () {};
callback = callback || function () { };

hooks.fire('action:topics.loading');
const params = utils.params();
Expand Down
1 change: 0 additions & 1 deletion src/categories/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ module.exports = function (Categories) {
const colours = Categories.assignColours();

let category = createCategory(data, cid, colours, slug, parentCid, order);
console.log('Anuja - createCategory');

if (data.backgroundImage) {
category.backgroundImage = data.backgroundImage;
Expand Down
17 changes: 17 additions & 0 deletions src/socket.io/topics/tags.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,23 @@ module.exports = function (SocketTopics) {
return await method(data);
}

// Triggered in Category.search
SocketTopics.getTopicsByCid = async function (socket, data) {
if (!data || !data.cid || !utils.isNumber(data.cid)) {
throw new Error('[[error:invalid-data]]');
}

const canRead = await privileges.categories.can('topics:read', data.cid, socket.uid);
if (!canRead) {
throw new Error('[[error:no-privileges]]');
}

// Get all topics within the category
const topicsData = await topics.getTopicsFromSet(`cid:${data.cid}:tids`, socket.uid, 0, -1);

return topicsData.topics;
};

// used by tag filter search
SocketTopics.tagFilterSearch = async function (socket, data) {
let cids = [];
Expand Down

0 comments on commit a205c22

Please sign in to comment.