From ed9eb04f1e769c12bd46c77fc777fc69fb57f131 Mon Sep 17 00:00:00 2001 From: Puneet Bajaj Date: Thu, 11 Apr 2024 21:40:11 -0400 Subject: [PATCH] initial connection attempt to backend --- app/login/student_login.tsx | 95 ++++++++++++++++--- app/student/profile/student_profile.tsx | 18 +++- app/utils/drag_and_drop.tsx | 120 ++++++++++++++++-------- components/ui/skeleton.tsx | 15 +++ lib/models.ts | 13 +++ 5 files changed, 210 insertions(+), 51 deletions(-) create mode 100644 components/ui/skeleton.tsx create mode 100644 lib/models.ts diff --git a/app/login/student_login.tsx b/app/login/student_login.tsx index 9349949..c89ba7f 100644 --- a/app/login/student_login.tsx +++ b/app/login/student_login.tsx @@ -23,28 +23,92 @@ import { useRouter } from 'next/navigation'; const StudentLoginPage: React.FC = () => { const [username, setUsername] = useState(''); const [password, setPassword] = useState(''); + const [cpassword, setCPassword] = useState(''); + const [name, setName] = useState(''); + const [isMismatched, setIsMismatched] = useState(false); const router = useRouter(); - const handleLoginClick = () => { - router.push('/student'); + const handleLoginClick = async () => { + try { + const response = await fetch('${process.env.NEXT_PUBLIC_BACKEND_URL}/api/signin/', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + email: username, + Password: password, + }), + credentials: 'include', + }); + + if (response.ok) { + const data = await response.json(); + console.log(data); + router.push('/student'); + } else { + alert('Login failed. Please check your credentials and try again.'); + } + } catch (error) { + console.error('There was an error logging in:', error); + alert('An error occurred. Please try again later.'); + } }; - const handleSignupClick = () => { - router.push('/student/signup'); + const handleSignupClick = async () => { + try { + const response = await fetch('${process.env.NEXT_PUBLIC_BACKEND_URL}/api/signup/', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + name: name, + email: username, + Password: password, + }), + credentials: 'include', + }); + + if (response.ok) { + const data = await response.json(); + console.log(data); + router.push('/student/signup'); + } else { + alert('Signup failed. Please try again.'); + } + + } catch (error) { + console.error('There was an error signing up:', error); + alert('An error occurred. Please try again later.'); + } }; const handleUsernameChange = (event: React.ChangeEvent) => { setUsername(event.target.value); }; + const handleNameChange = (event: React.ChangeEvent) => { + setName(event.target.value); + } + + const handleCPasswordChange = (event: React.ChangeEvent) => { + setCPassword(event.target.value); + if (password !== event.target.value) { + setIsMismatched(true); + } else { + setIsMismatched(false); + } + } + const handlePasswordChange = (event: React.ChangeEvent) => { setPassword(event.target.value); }; const handleSubmit = (event: React.FormEvent) => { event.preventDefault(); - // Perform login logic here + handleLoginClick(); }; return ( @@ -65,11 +129,11 @@ const StudentLoginPage: React.FC = () => {
- +
- +
@@ -86,21 +150,30 @@ const StudentLoginPage: React.FC = () => { +
+ + +
- +
- +
- +
+ {isMismatched && ( +
+ Passwords do not match. +
+ )}
- + diff --git a/app/student/profile/student_profile.tsx b/app/student/profile/student_profile.tsx index b111238..4097143 100644 --- a/app/student/profile/student_profile.tsx +++ b/app/student/profile/student_profile.tsx @@ -12,8 +12,21 @@ import Image from 'next/image'; export interface StudentProfileProps { } -export default class StudentProfile extends React.Component { - public render() { +export default function StudentProfile() { + + const [profileData, setProfileData] = React.useState(null); + + React.useEffect(() => { + fetch('${process.env.NEXT_PUBLIC_BACKEND_URL}/api/student_profile/', {credentials: 'include'}) + .then (response => response.json()) + .then (data => { + setProfileData(data); + }) + .catch (error => { + console.error('There was an error fetching the profile data:', error); + }); + }, []); + return (
@@ -59,5 +72,4 @@ export default class StudentProfile extends React.Component
); - } } diff --git a/app/utils/drag_and_drop.tsx b/app/utils/drag_and_drop.tsx index e535a6f..1bddd26 100644 --- a/app/utils/drag_and_drop.tsx +++ b/app/utils/drag_and_drop.tsx @@ -1,11 +1,16 @@ "use client"; -import { useRef, useState } from "react"; +import { Button } from "@/components/ui/button"; +import { FormEvent, useRef, useState } from "react"; +import { useRouter } from 'next/navigation'; +import { Skeleton } from "@/components/ui/skeleton"; export default function DragAndDrop() { const [dragActive, setDragActive] = useState(false); const inputRef = useRef(null); - const [files, setFiles] = useState([]); + const [file, setFile] = useState(); + const [isLoading, setIsLoading] = useState(false); + const router = useRouter(); const handleDragEnter = (e: any) => { e.preventDefault(); @@ -25,54 +30,84 @@ export default function DragAndDrop() { setDragActive(true); }; - const handleDrop = (e: any) => { - e.preventDefault(); - e.stopPropagation(); + const handleDrop = (event: any) => { + event.preventDefault(); setDragActive(false); - if(e.dataTransfer.files && e.dataTransfer.files[0]){ - for(let i = 0; i < e.dataTransfer.files["length"]; i++){ - setFiles((prevState: any) => [...prevState, e.dataTransfer.files[i]]); - } + + if (event.dataTransfer.files && event.dataTransfer.files[0]) { + const newFile = event.dataTransfer.files[0]; + setFile(newFile); } }; + const openFileExplorer = () => { inputRef.current.value = ""; inputRef.current.click(); }; - const handleFileChange = (e: any) => { - e.preventDefault(); - console.log("File has been added"); - if(e.target.files && e.target.files[0]){ - for(let i = 0; i < e.target.files["length"]; i++){ - setFiles((prevState: any) => [...prevState, e.target.files[i]]); - } + const handleFileChange = (event: React.ChangeEvent) => { + if (event.target.files && event.target.files[0]) { + const newFile = event.target.files[0]; + setFile(newFile); } }; + - function handleSubmitFile(e: any) { - if(files.length === 0){ - // Show error message + const handleSubmit = async (event: FormEvent) => { + event.preventDefault(); + if (!file) { + alert('Please select a file to upload.'); + return; } - else{ - // Perform file upload logic here - } - }; - const removeFile = (name: any, index: any) => { - let newFiles = files.filter((file: any) => file.name !== name); - setFiles(newFiles); + setIsLoading(true); + const formData = new FormData(); + formData.append('file', file); // 'file' should match the key expected by your Django backend + + try { + const response = await fetch('${process.env.NEXT_PUBLIC_BACKEND_URL}/api/upload/', { // Update URL to your actual endpoint + method: 'POST', + body: formData, + credentials: 'include', // to include cookies (e.g., sessionid) + }); + + setIsLoading(false); + if (response.ok) { + const data = await response.json(); + router.push('/student'); + } else { + alert('Upload failed. Please try again.'); + } + } catch (error) { + console.error('Error uploading file:', error); + alert('An error occurred. Please try again.'); + } + }; + const removeFile = () => { + setFile(null); }; return (
-
+
+ + +
+
+ + +
+
+ ) : + ( e.preventDefault()} + onSubmit={handleSubmit} onDrop={handleDrop} onDragLeave={handleDragLeave} onDragOver={handleDragOver} @@ -87,7 +122,7 @@ export default function DragAndDrop() { accept=".pdf, .doc, .docx" />

- Drag & Drop your files here or {" "} + Drag & Drop your Resume here or {" "}

- {files.map((file: any, index: any) => ( -
- {file.name} - removeFile(file.name, index)} + { + file && ( +
+

Files to be uploaded:

+
+ {file.name} + removeFile()} + > + remove + +
+
- ))} + )}
+ )}
); } \ No newline at end of file diff --git a/components/ui/skeleton.tsx b/components/ui/skeleton.tsx new file mode 100644 index 0000000..d7e45f7 --- /dev/null +++ b/components/ui/skeleton.tsx @@ -0,0 +1,15 @@ +import { cn } from "@/lib/utils" + +function Skeleton({ + className, + ...props +}: React.HTMLAttributes) { + return ( +
+ ) +} + +export { Skeleton } diff --git a/lib/models.ts b/lib/models.ts new file mode 100644 index 0000000..9af4016 --- /dev/null +++ b/lib/models.ts @@ -0,0 +1,13 @@ +type ProfileData = { + Education : { + GPA: string, + Major: string, + Minor: string, + University: string, + }, + Links : { + LinkedIn?: string, + GitHub?: string, + Website?: string, + }, +}; \ No newline at end of file