From 16c4656bf67e1c43552be6ed77ac89fa758c7160 Mon Sep 17 00:00:00 2001 From: afazio1 Date: Mon, 22 Apr 2024 12:52:59 -0400 Subject: [PATCH] add filter inputs --- frontend/src/screens/RegisterFile.tsx | 157 +++++++++++++++++++++++++- frontend/src/styles/RegisterFile.css | 6 +- 2 files changed, 154 insertions(+), 9 deletions(-) diff --git a/frontend/src/screens/RegisterFile.tsx b/frontend/src/screens/RegisterFile.tsx index be04836..5f94b23 100644 --- a/frontend/src/screens/RegisterFile.tsx +++ b/frontend/src/screens/RegisterFile.tsx @@ -1,7 +1,8 @@ import "../styles/RegisterFile.css"; -import { RegisteredFile, Status } from "../types/types"; +import { RegisteredFile, Status, Professor, Course, Topic, Semester } from "../types/types"; import { useState, useEffect } from "react"; import { getAuthHeaders } from "../utils/getAuthHeaders"; +import { getSessionCookie } from "../contexts/session"; export default function RegisterFile() { const [serverFiles, setServerFiles] = useState([]); @@ -52,16 +53,17 @@ export default function RegisterFile() { // @ts-expect-error - fileInput is an HTMLInputElement const filename: string = fileInput.files[0].name.replace(/\s/g, "_"); const formData = new FormData(event.target as HTMLFormElement); + // register file with central server fetch(`http://localhost:8000/api/register/`, { method: 'POST', headers: getAuthHeaders(), body: JSON.stringify({ filename: filename, - topic: 1, - semester: 1, - professor: 1, - course: 1, + topic: formData.get('topic'), + semester: formData.get('semester'), + professor: formData.get('professor'), + course: formData.get('course'), }) }) .then(response => response.json()) @@ -80,6 +82,113 @@ export default function RegisterFile() { }) .catch(error => console.error(error)); } + + // Filters Logic + interface Filters extends Record { + professor: string; + course: string; + topic: string; +} + const [filters, setFilters] = useState({ + professor: '', + course: '', + topic: '', + }); + const [professors, setProfessors] = useState([]); + const [courses, setCourses] = useState([]); + const [topics, setTopics] = useState([]); + const [semesters, setSemesters] = useState([]); + + useEffect(() => { + if (!getSessionCookie()) { + return; + } + // Fetch professors + fetchProfessors(); + // Fetch courses + fetchCourses(); + // Fetch topics + fetchTopics(); + // Fetch semesters + fetchSemesters(); + }, []); + + + const fetchProfessors = async () => { + try { + const response = await fetch('http://localhost:8000/api/professors', { + method: 'GET', + headers: getAuthHeaders(), + }); + const data = await response.json(); + + if (Array.isArray(data)) { + setProfessors(data); + } else { + console.error('Data fetched is not an array:', data); + } + } catch (error) { + console.error('Error fetching professors:', error); + } + }; + + const fetchCourses = async () => { + try { + const response = await fetch('http://localhost:8000/api/courses', { + method: 'GET', + headers: getAuthHeaders(), + }); + const data = await response.json(); + if (Array.isArray(data)) { + setCourses(data); + } else { + console.error('Data fetched is not an array:', data); + } + } catch (error) { + console.error('Error fetching courses:', error); + } + }; + + const fetchTopics = async () => { + try { + const response = await fetch('http://localhost:8000/api/topics', { + method: 'GET', + headers: getAuthHeaders(), + }); + const data = await response.json(); + if (Array.isArray(data)) { + setTopics(data); + } else { + console.error('Data fetched is not an array:', data); + } + } catch (error) { + console.error('Error fetching topics:', error); + } + }; + const fetchSemesters = async () => { + try { + const response = await fetch('http://localhost:8000/api/semesters', { + method: 'GET', + headers: getAuthHeaders(), + }); + const data = await response.json(); + if (Array.isArray(data)) { + setSemesters(data); + } else { + console.error('Data fetched is not an array:', data); + } + } catch (error) { + console.error('Error fetching courses:', error); + } + }; + + const handleChange = (event: React.ChangeEvent) => { + const { name, value } = event.target; + setFilters((prevFilters: Filters) => ({ + ...prevFilters, + [name]: value, + })); + }; return (
@@ -87,7 +196,43 @@ export default function RegisterFile() {

Host a local file for the GT community!

- + +
+ + +
+
+ + +
+
+ + +
+
+ + +
diff --git a/frontend/src/styles/RegisterFile.css b/frontend/src/styles/RegisterFile.css index d1be3a4..b76a396 100644 --- a/frontend/src/styles/RegisterFile.css +++ b/frontend/src/styles/RegisterFile.css @@ -3,8 +3,8 @@ color: white; } .container { display: flex; - gap: 10rem; - justify-content: space-between; + gap: 5rem; + justify-content: center; } .file-upload-card { @@ -45,4 +45,4 @@ color: white; border: 1px solid #FFFFFF; background-color: #B1D4E0; color: black; - } + } \ No newline at end of file