Skip to content

Commit

Permalink
frontend: remove search on localstate, change to backend search use m…
Browse files Browse the repository at this point in the history
…ongo passing searchTerm
  • Loading branch information
fadingNA committed Nov 18, 2024
1 parent 13b9119 commit 0493352
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
3 changes: 2 additions & 1 deletion frontend/src/preferences/preferenceApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ export async function getDocsWithPagination(
order = 'desc',
pageNumber = 1,
rowsPerPage = 10,
searchTerm = '',
): Promise<GetDocsResponse | null> {
try {
const query = `sort=${sort}&order=${order}&page=${pageNumber}&rows=${rowsPerPage}`;
const query = `sort=${sort}&order=${order}&page=${pageNumber}&rows=${rowsPerPage}&search=${searchTerm}`;
const response = await userService.getDocsWithPagination(query);
const data = await response.json();
const docs: Doc[] = [];
Expand Down
29 changes: 16 additions & 13 deletions frontend/src/settings/Documents.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,25 +40,18 @@ const Documents: React.FC<DocumentsProps> = ({
const { t } = useTranslation();
const dispatch = useDispatch();
// State for search input
const [searchTerm, setSearchTerm] = useState('');
const [searchTerm, setSearchTerm] = useState<string>('');
// State for modal: active/inactive
const [modalState, setModalState] = useState<ActiveState>('INACTIVE'); // Initialize with inactive state
const [isOnboarding, setIsOnboarding] = useState(false); // State for onboarding flag
const [loading, setLoading] = useState(false);
const [isOnboarding, setIsOnboarding] = useState<boolean>(false); // State for onboarding flag
const [loading, setLoading] = useState<boolean>(false);
const [sortField, setSortField] = useState<'date' | 'tokens'>('date');
const [sortOrder, setSortOrder] = useState<'asc' | 'desc'>('desc');
// Pagination
const [currentPage, setCurrentPage] = useState<number>(1);
const [rowsPerPage, setRowsPerPage] = useState<number>(10);
const [totalPages, setTotalPages] = useState<number>(1);
// const [totalDocuments, setTotalDocuments] = useState<number>(0);
// Filter documents based on the search term
const filteredDocuments = paginatedDocuments?.filter((document) =>
document.name.toLowerCase().includes(searchTerm.toLowerCase()),
);
// State for documents
const currentDocuments = filteredDocuments ?? [];
console.log('currentDocuments', currentDocuments);
const currentDocuments = paginatedDocuments ?? [];
const syncOptions = [
{ label: 'Never', value: 'never' },
{ label: 'Daily', value: 'daily' },
Expand All @@ -84,7 +77,7 @@ const Documents: React.FC<DocumentsProps> = ({
setSortOrder('desc');
}
}
getDocsWithPagination(sortField, sortOrder, page, rowsPerPg)
getDocsWithPagination(sortField, sortOrder, page, rowsPerPg, searchTerm)
.then((data) => {
dispatch(setPaginatedDocuments(data ? data.docs : []));
setTotalPages(data ? data.totalPages : 0);
Expand Down Expand Up @@ -130,6 +123,10 @@ const Documents: React.FC<DocumentsProps> = ({
}
}, [modalState, sortField, currentPage, rowsPerPage]);

useEffect(() => {
refreshDocs(sortField, 1, rowsPerPage);
}, [searchTerm]);

return (
<div className="mt-8">
<div className="flex flex-col relative">
Expand All @@ -143,7 +140,13 @@ const Documents: React.FC<DocumentsProps> = ({
type="text"
id="document-search-input"
value={searchTerm}
onChange={(e) => setSearchTerm(e.target.value)} // Handle search input change
onChange={(e) => {
setSearchTerm(e.target.value);
setCurrentPage(1);
// refreshDocs(sortField, 1, rowsPerPage);
// do not call refreshDocs here the state is async
// so it will not have the updated value
}} // Handle search input change
/>
</div>
<button
Expand Down

0 comments on commit 0493352

Please sign in to comment.