|
| 1 | +import React, { useEffect } from "react"; |
| 2 | +import { useState } from "react"; |
| 3 | +import { useAuth } from "../../context/AuthContext.tsx"; |
| 4 | +import { Opportunity } from "../../types/opportunity.ts"; |
| 5 | +import { getCookie } from "../../utils.ts"; |
| 6 | + |
| 7 | +export default function SavedPage() { |
| 8 | + const { auth } = useAuth(); |
| 9 | + |
| 10 | + if (!auth.isAuthenticated) { |
| 11 | + window.location.href = "/login"; |
| 12 | + } |
| 13 | + |
| 14 | + const [saved, setSaved] = useState<null | Opportunity[]>(null); |
| 15 | + |
| 16 | + const csrfToken = getCookie('csrf_access_token'); |
| 17 | + |
| 18 | + const fetchSaved = async () => { |
| 19 | + try { |
| 20 | + const response = await fetch( |
| 21 | + `${process.env.REACT_APP_BACKEND_SERVER}/savedOpportunities`, { |
| 22 | + credentials: "include", |
| 23 | + } |
| 24 | + ); |
| 25 | + |
| 26 | + if (!response.ok) { |
| 27 | + throw new Error("Saved not found"); |
| 28 | + } |
| 29 | + |
| 30 | + const data = await response.json(); |
| 31 | + setSaved(data); |
| 32 | + console.log(data); |
| 33 | + } catch { |
| 34 | + console.log("Error fetching saved"); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + useEffect(() => { |
| 39 | + fetchSaved(); |
| 40 | + }, []); |
| 41 | + |
| 42 | + return ( |
| 43 | + <section className="center container-xl"> |
| 44 | + <h1 className="text-center my-4 text-4xl font-extrabold leading-none tracking-tight text-gray-900 md:text-5xl lg:text-6xl"> |
| 45 | + Saved Opportunities |
| 46 | + </h1> |
| 47 | + {!saved && "Loading..."} |
| 48 | + {saved && ( |
| 49 | + <table> |
| 50 | + <tr> |
| 51 | + <th>Name</th> |
| 52 | + <th>Description</th> |
| 53 | + <th>Recommended Experience</th> |
| 54 | + <th>Pay</th> |
| 55 | + <th>Credits</th> |
| 56 | + <th>Semester</th> |
| 57 | + <th>Year</th> |
| 58 | + <th>Application Due</th> |
| 59 | + <th>Location</th> |
| 60 | + <th>Unsave</th> |
| 61 | + </tr> |
| 62 | + {saved.map((opportunity) => ( |
| 63 | + <tr key={opportunity.id}> |
| 64 | + <td>{opportunity.name}</td> |
| 65 | + <td>{opportunity.description}</td> |
| 66 | + <td>{opportunity.recommended_experience}</td> |
| 67 | + <td>{opportunity.pay}</td> |
| 68 | + <td>{opportunity.credits}</td> |
| 69 | + <td>{opportunity.semester}</td> |
| 70 | + <td>{opportunity.year}</td> |
| 71 | + <td style={{ |
| 72 | + color: (() => { |
| 73 | + const today = new Date(); |
| 74 | + const dueDate = new Date(opportunity.application_due); |
| 75 | + const oneWeek = 7 * 24 * 60 * 60 * 1000; |
| 76 | + |
| 77 | + if (dueDate < today) { |
| 78 | + return "red"; |
| 79 | + } else if (dueDate.getTime() - today.getTime() <= oneWeek) { |
| 80 | + return "orange"; |
| 81 | + } else { |
| 82 | + return "black"; |
| 83 | + } |
| 84 | + })() |
| 85 | + }}> |
| 86 | + {new Date(opportunity.application_due).toLocaleDateString("en-US")} |
| 87 | + </td> |
| 88 | + <td>{opportunity.location}</td> |
| 89 | + <td> |
| 90 | + <button className="p-2 bg-blue-600 text-white font-semibold rounded-lg shadow-md hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-blue-500" |
| 91 | + onClick={async () => { |
| 92 | + try { |
| 93 | + const headers: Record<string, string> = { |
| 94 | + "Content-Type": "application/json", // Good practice for cross-origin requests |
| 95 | + }; |
| 96 | + if (csrfToken) { |
| 97 | + headers["X-CSRF-TOKEN"] = csrfToken; // Include the token only when defined |
| 98 | + } |
| 99 | + |
| 100 | + const response = await fetch( |
| 101 | + `${process.env.REACT_APP_BACKEND_SERVER}/unsaveOpportunity/${opportunity.id}`, { |
| 102 | + method: "DELETE", |
| 103 | + credentials: "include", |
| 104 | + headers, |
| 105 | + }); |
| 106 | + |
| 107 | + if (!response.ok) { |
| 108 | + throw new Error("Failed to unsave"); |
| 109 | + } |
| 110 | + |
| 111 | + setSaved(prev => prev ? prev.filter(o => o.id !== opportunity.id) : prev); |
| 112 | + } catch { |
| 113 | + console.log("Error unsaving opportunity"); |
| 114 | + } |
| 115 | + }} |
| 116 | + > |
| 117 | + Unsave |
| 118 | + </button> |
| 119 | + </td> |
| 120 | + </tr> |
| 121 | + ))} |
| 122 | + </table> |
| 123 | + )} |
| 124 | + </section> |
| 125 | + ); |
| 126 | +}; |
0 commit comments