import React, { useState, useEffect } from 'react'; import { useLocation, useNavigate } from 'react-router-dom'; import styles from '../styles/Results.module.css'; // Make sure this path is correct import { getAuthHeaders } from '../utils/getAuthHeaders'; import { File } from '../types/types'; const Results: React.FC = () => { const [files, setFiles] = useState([]); const [isLoading, setLoading] = useState(false); const location = useLocation(); const navigate = useNavigate(); useEffect(() => { const fetchFiles = async () => { setLoading(true); try { // Need to double check this const queryString = location.search; // Includes the '?' prefix console.log(queryString) const response = await fetch(`http://localhost:8000/api/files/filter${queryString}`, {headers: getAuthHeaders()}); if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); } const data = await response.json(); setFiles(data); } catch (e) { console.error("Could not fetch files", e); } finally { setLoading(false); } }; fetchFiles(); }, [location]); const handleSearchAgain = () => { navigate('/search'); }; if (isLoading) return
Loading...
; return (
PeerNotes Logo
{isLoading ? (

Loading...

) : files.length ? ( files.map((file) => (

{file.filename}

Course: {file.course.name} {file.course.number}

Professor: {file.professor.name}

Semester: {file.semester.name}

Upvotes: {file.upvotes.length}

Downvotes: {file.downvotes.length}

Download

)) ) : (

No results found.

)}
); }; export default Results;