commit
fad8d34776
2 changed files with 34 additions and 18 deletions
|
|
@ -6,30 +6,23 @@ import { ListQuestion, LoadingBar } from 'components';
|
||||||
import { PaginatedList } from 'controllers';
|
import { PaginatedList } from 'controllers';
|
||||||
import { searchQuestions } from 'services/questionsServices';
|
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() {
|
export default function Buffet() {
|
||||||
const [sort, setSort] = useState(0);
|
const [sort, setSort] = useState('');
|
||||||
const [loading, setLoading] = useState(true);
|
const [loading, setLoading] = useState(true);
|
||||||
const [, setSearchParams] = useSearchParams();
|
const [, setSearchParams] = useSearchParams();
|
||||||
|
|
||||||
const getData = async ({ question_id }) => {
|
const getData = async ({ question_id }) => {
|
||||||
const { questions } = await searchQuestions({
|
const { questions } = await searchQuestions({
|
||||||
...sortObjArr[sort],
|
sort,
|
||||||
after: question_id,
|
|
||||||
});
|
});
|
||||||
|
|
||||||
setLoading(() => false);
|
setLoading(() => false);
|
||||||
return questions;
|
return questions;
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleSortChange = (_, newSort) => {
|
const handleSortChange = (_, value) => {
|
||||||
setSearchParams(sortObjArr[newSort]);
|
setSearchParams({ sort: value });
|
||||||
setSort(newSort);
|
setSort(value);
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|
@ -60,10 +53,10 @@ export default function Buffet() {
|
||||||
onChange={handleSortChange}
|
onChange={handleSortChange}
|
||||||
style={{ display: 'block', marginTop: '1%' }}
|
style={{ display: 'block', marginTop: '1%' }}
|
||||||
>
|
>
|
||||||
<ToggleButton value={0}>Recent</ToggleButton>
|
<ToggleButton value=''>Recent</ToggleButton>
|
||||||
<ToggleButton value={1}>Best</ToggleButton>
|
<ToggleButton value='u'>Best</ToggleButton>
|
||||||
<ToggleButton value={2}>Interesting</ToggleButton>
|
<ToggleButton value='uvc'>Interesting</ToggleButton>
|
||||||
<ToggleButton value={3}>Hot</ToggleButton>
|
<ToggleButton value='uvac'>Hot</ToggleButton>
|
||||||
</ToggleButtonGroup>
|
</ToggleButtonGroup>
|
||||||
</Box>
|
</Box>
|
||||||
{loading && <LoadingBar />}
|
{loading && <LoadingBar />}
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@ const config = require('server/config.json');
|
||||||
const Question = require('server/db/models/Question');
|
const Question = require('server/db/models/Question');
|
||||||
|
|
||||||
async function Search(req, res) {
|
async function Search(req, res) {
|
||||||
const { creator, title, text, tags, createdAt } = req.query;
|
const { creator, title, text, tags, createdAt, sort } = req.query;
|
||||||
|
|
||||||
let searchQuery = {};
|
let searchQuery = {};
|
||||||
if (creator) {
|
if (creator) {
|
||||||
|
|
@ -28,7 +28,30 @@ async function Search(req, res) {
|
||||||
searchQuery['createdAt'] = createdAt;
|
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 });
|
return res.send({ questions });
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Reference in a new issue