This repository has been archived on 2025-11-04. You can view files and clone it, but you cannot make any changes to its state, such as pushing and creating new issues, pull requests or comments.
qoverflow/client/src/controllers/SearchResultsController.jsx
2022-08-18 16:27:00 -04:00

43 lines
1.3 KiB
JavaScript

import { useState } from 'react';
import { useSearchParams } from 'react-router-dom';
import { ListQuestion, LoadingBar } from 'components';
import { PaginatedList } from 'controllers';
import { searchQuestions } from 'services/questionsServices';
export default function SearchResultsController() {
const [searchParams] = useSearchParams();
const [loading, setLoading] = useState(true);
const getData = async () => {
const creator = searchParams.get('creator');
const title = searchParams.get('title');
const text = searchParams.get('text');
let tags = searchParams.get('tags');
let time = searchParams.get('createdAt');
let createdAt = {};
if (time) {
time = new Date(time);
createdAt['$gte'] = parseInt(time.getTime());
time.setDate(time.getDate() + 1);
createdAt['$lte'] = time.getTime();
}
if (tags) {
tags = tags.split(' ');
}
setLoading(() => true);
const { questions } = await searchQuestions({ creator, title, text, tags, createdAt });
setLoading(() => false);
return questions;
};
return (
<>
{loading && <LoadingBar />}
<PaginatedList {...{ concat: true, Component: ListQuestion, getData }} />
</>
);
}