// src/components/CollegeProfileList.js import React, { useEffect, useState } from "react"; import { Link, useNavigate, useParams } from "react-router-dom"; import CareerSelectDropdown from "./CareerSelectDropdown.js"; import apiFetch from '../auth/apiFetch.js'; export default function CollegeProfileList() { const { careerId } = useParams(); // may be undefined const navigate = useNavigate(); /* ───────── existing lists ───────── */ const [rows, setRows] = useState([]); const [careerRows, setCareerRows] = useState([]); /* ───────── ui state ───────── */ const [showPicker, setShowPicker] = useState(false); const [loadingCareers, setLoadingCareers] = useState(true); const authFetch = apiFetch; /* ───────── load college plans ───────── */ /* ───────── load college plans ───────── */ useEffect(() => { (async () => { try { const r = await authFetch("/api/premium/college-profile/all"); if (!r.ok) throw new Error(`load college-profile/all → ${r.status}`); const d = await r.json(); setRows(d.collegeProfiles || []); } catch (err) { console.error("College profiles load failed:", err); setRows([]); } })(); }, []); /* ───────── load career profiles for the picker ───────── */ useEffect(() => { (async () => { try { const res = await authFetch("/api/premium/career-profile/all"); if (!res.ok) throw new Error(`load career-profile/all → ${res.status}`); const data = await res.json(); setCareerRows(data.careerProfiles || []); } catch (err) { console.error("Career profiles load failed:", err); } finally { setLoadingCareers(false); } })(); }, []); /* ───────── delete helper ───────── */ async function handleDelete(row) { if (!window.confirm("Delete this college plan?")) return; try { const res = await authFetch(`/api/premium/college-profile/by-fields`, { method: "DELETE", headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ career_title : row.career_title || null, selected_school: row.selected_school || null, selected_program: row.selected_program || null, created_at : row.created_at || null // optional disambiguator }) }); if (!res.ok) throw new Error(`delete failed → ${res.status}`); setRows((r) => r.filter(x => !( (x.career_title||'') === (row.career_title||'') && (x.selected_school||'') === (row.selected_school||'') && (x.selected_program||'')=== (row.selected_program||'') && (x.created_at||'') === (row.created_at||'') ) )); if (!res.ok) throw new Error(`delete failed → ${res.status}`); setRows((r) => r.filter(x => !( (x.career_title||'') === (row.career_title||'') && (x.selected_school||'') === (row.selected_school||'') && (x.selected_program||'')=== (row.selected_program||'') && (x.created_at||'') === (row.created_at||'') ) )); } catch (err) { console.error("Delete failed:", err); alert("Could not delete – see console."); } } return (
{/* ───────── header row ───────── */}

College Plans

{/* new‑plan button & inline picker */} {!showPicker ? ( ) : (
{ if (!careerObj) return; const title = careerObj.scenario_title || careerObj.career_name || ''; const start = careerObj.start_date || ''; navigate(`/profile/college/new?career=${encodeURIComponent(title)}&start=${encodeURIComponent(start)}`); }} />
)}
{/* ───────── table of existing college plans ───────── */} {rows.map((r) => ( ))} {rows.length === 0 && ( )}
Career School Program Created
{r.career_title} {r.selected_school} {r.selected_program} {r.created_at?.slice(0, 10)} edit
No college profiles yet. 
); }