Skip to content

Commit

Permalink
submitting waiver
Browse files Browse the repository at this point in the history
  • Loading branch information
poojakedia committed Feb 6, 2024
1 parent 4308e40 commit f399b68
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 8 deletions.
40 changes: 32 additions & 8 deletions app/dashboard/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { Input } from "@/app/dashboard/components/input"
import { Button } from "@/app/dashboard/components/button"
import OrganizerView from "@/app/dashboard/views/organizerView"
import DirectorView from "@/app/dashboard/views/directorView"

import { UploadWaiver } from '../lib/actions';

import { useForm } from "react-hook-form";
import { zodResolver } from "@hookform/resolvers/zod";
Expand Down Expand Up @@ -51,21 +51,43 @@ export default function Dashboard() {


const {register,handleSubmit,reset, formState: { errors },} = useForm<UserUpdate>({resolver: zodResolver(UserUpdateSchema),defaultValues: userData,});

const onSubmit = (data: UserUpdate) => {
const [waiverFile, setWaiverFile] =useState<File | null>(null)
const onSubmit = async (data: UserUpdate) => {
console.log(data);
UpdateSelf(data);
}

const onWaiverSubmit = async () => {
console.log("the file is below: ")
console.log(waiverFile);
if (waiverFile){ //works
try{
await UploadWaiver(waiverFile as File);
} catch (error) {
console.error("Error uploading waiver:", error);
alert("Error uploading waiver");
}
} else {
console.error("No waiver file selected");//works
alert("Please select a waiver file");
}
}
const handleChangingFile = (event: React.ChangeEvent<HTMLInputElement>) =>{
if (event.target.files && event.target.files.length > 0) {
const selectedFile = event.target.files[0];
if (selectedFile.type === 'application/pdf') {
setWaiverFile(selectedFile);
console.log("Selected file:", selectedFile);
} else {
console.error("Invalid file format. Please select a PDF file.");
} };
}
useEffect(() => {
async function fetchUser() {
try {
const data = await getSelf();
setUserData(data);
// setLoading(false);
} catch (error) {
console.log(error);
// setLoading(false);
}
}

Expand All @@ -89,6 +111,7 @@ export default function Dashboard() {
</div>
</div>
<Card className="w-full max-w-2xl">
<form onSubmit = {handleSubmit(onWaiverSubmit)}>
<CardHeader>
<CardTitle>Registration</CardTitle>
<CardDescription>Check your registration status.</CardDescription>
Expand All @@ -98,11 +121,11 @@ export default function Dashboard() {
<>
<div className="flex flex-row items-center justify-center">
<a className="underline" href="waiver.pdf" rel="noopener noreferrer" target="_blank">Waiver</a>
<input className="ml-auto mr-0" type="file" accept=".pdf"></input>
<input className="ml-auto mr-0" type="file" accept=".pdf" onChange={handleChangingFile} required></input>
</div>
<div className="flex flex-row items-center justify-center">
<CardTitle>unregistered</CardTitle>
<Button className="ml-auto" onClick={()=>console.log("register button clicked")}>Register</Button>
<Button type="submit" className="ml-auto" onClick={()=>console.log("register button clicked")}>Register</Button>
</div>
</>
}
Expand All @@ -116,6 +139,7 @@ export default function Dashboard() {
</>
}
</CardContent>
</form>
</Card>
</div>
<Card className="w-full max-w-2xl">
Expand Down
40 changes: 40 additions & 0 deletions app/lib/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -380,4 +380,44 @@ export async function Reset(email:string, password:string, conpassword:string, m
}

return resp;
}


export async function GetWaiverInfo(){
console.log('getwaiverinfo is called');
const session = await auth();
if(session?.user){
const resp = await fetch("https://api.hackru.org/dev/waiver", {
method: "POST",
headers:{
"content-type":"application/json",
},
body: JSON.stringify({
email: session.user.email,
token: session.user.name,
})
});
if (!resp.ok) {
throw new Error("did not successfully retrieve waiver info");
}

const json = await resp.json();
return json.body;
}
else {
throw new Error("user session not found")
}

}

export async function UploadWaiver(file: File){
console.log("UploadWaiver function called");
const info = await GetWaiverInfo();
return await fetch(info.upload, {
method: "PUT",
headers: {
"content-type": "application/pdf",
},
body: file,
});
}

0 comments on commit f399b68

Please sign in to comment.