diff --git a/client/src/containers/Buffet.jsx b/client/src/containers/Buffet.jsx
index e6e5a4b..4eeebb1 100644
--- a/client/src/containers/Buffet.jsx
+++ b/client/src/containers/Buffet.jsx
@@ -6,30 +6,23 @@ import { ListQuestion, LoadingBar } from 'components';
import { PaginatedList } from 'controllers';
import { searchQuestions } from 'services/questionsServices';
-const recent = {};
-const best = { sort: 'u' };
-const interesting = { match: JSON.stringify({ answers: 0 }), sort: 'uvc' };
-const hot = { match: JSON.stringify({ hasAcceptedAnswer: false }), sort: 'uvac' };
-const sortObjArr = [recent, best, interesting, hot];
-
export default function Buffet() {
- const [sort, setSort] = useState(0);
+ const [sort, setSort] = useState('');
const [loading, setLoading] = useState(true);
const [, setSearchParams] = useSearchParams();
const getData = async ({ question_id }) => {
const { questions } = await searchQuestions({
- ...sortObjArr[sort],
- after: question_id,
+ sort,
});
setLoading(() => false);
return questions;
};
- const handleSortChange = (_, newSort) => {
- setSearchParams(sortObjArr[newSort]);
- setSort(newSort);
+ const handleSortChange = (_, value) => {
+ setSearchParams({ sort: value });
+ setSort(value);
};
return (
@@ -60,10 +53,10 @@ export default function Buffet() {
onChange={handleSortChange}
style={{ display: 'block', marginTop: '1%' }}
>
- Recent
- Best
- Interesting
- Hot
+ Recent
+ Best
+ Interesting
+ Hot
{loading && }
diff --git a/server/routes/question/Search.js b/server/routes/question/Search.js
index 058f3df..eb5311a 100644
--- a/server/routes/question/Search.js
+++ b/server/routes/question/Search.js
@@ -3,7 +3,7 @@ const config = require('server/config.json');
const Question = require('server/db/models/Question');
async function Search(req, res) {
- const { creator, title, text, tags, createdAt } = req.query;
+ const { creator, title, text, tags, createdAt, sort } = req.query;
let searchQuery = {};
if (creator) {
@@ -28,7 +28,30 @@ async function Search(req, res) {
searchQuery['createdAt'] = createdAt;
}
- const questions = await Question.find(searchQuery).sort({ createdAt: 'asc' });
+ let sortQuery = [[createdAt, 'desc']];
+ switch (sort) {
+ case 'u':
+ sortQuery = [['upvotes', 'desc']];
+ break;
+ case 'uvc':
+ sortQuery = [
+ ['upvotes', 'desc'],
+ ['view', 'desc'],
+ ['comments', 'desc'],
+ ];
+ break;
+ case 'uvac':
+ sortQuery = [
+ ['upvotes', 'desc'],
+ ['view', 'desc'],
+ ['answers', 'desc'],
+ ['comments', 'desc'],
+ ];
+ searchQuery['hasAcceptedAnswer'] = false;
+ break;
+ }
+
+ const questions = await Question.find(searchQuery).sort(sortQuery);
return res.send({ questions });
}