import { ClipLoader } from 'react-spinners'; import LoanRepayment from './LoanRepayment.js'; import './PopoutPanel.css'; import { useState, useEffect } from 'react'; function PopoutPanel({ data = {}, userState = 'N/A', // Passed explicitly from Dashboard loading = false, error = null, closePanel, }) { console.log('PopoutPanel Props:', { data, loading, error, userState }); const [isCalculated, setIsCalculated] = useState(false); const [results, setResults] = useState([]); // Store loan repayment calculation results const [loadingCalculation, setLoadingCalculation] = useState(false); // ✅ Reset `results` when a new career is selected useEffect(() => { console.log(`Career changed to: ${data?.title}, clearing previous loan results.`); setResults([]); // ✅ Clears results when a new career is selected setIsCalculated(false); // ✅ Reset calculation state }, [data]); // Runs whenever `data` changes (i.e., new career is selected) // Handle loading state if (loading) { return (

Loading Career Details...

); } // Safely access nested data with fallbacks const { jobDescription = null, // Default to null if not provided tasks = null, // Default to null if not provided title = 'Career Details', economicProjections = {}, salaryData = [], schools = [], } = data; // Get program length for calculating tuition const getProgramLength = (degreeType) => { if (degreeType?.includes("Associate")) return 2; if (degreeType?.includes("Bachelor")) return 4; if (degreeType?.includes("Master")) return 6; if (degreeType?.includes("Doctoral") || degreeType?.includes("First Professional")) return 8; if (degreeType?.includes("Certificate")) return 1; return 4; // Default to 4 years if unspecified }; return (

{title}

{/* Job Description and Tasks */}

Job Description

{jobDescription || 'No description available'}

Expected Tasks

{tasks && tasks.length > 0 ? ( ) : (

No tasks available for this career path.

)}
{/* Schools Offering Programs Section */}

Schools Offering Programs

{Array.isArray(schools) && schools.length > 0 ? ( schools.map((school, index) => (
{school['INSTNM']}
Degree Type: {school['CREDDESC'] || 'Degree type information is not available for this program'}
In-State Tuition: ${school['In_state cost'] || 'Tuition information is not available for this school'}
Out-of-State Tuition: ${school['Out_state cost'] || 'Tuition information is not available for this school'}
Distance: {school['distance'] || 'Distance to school not available'}
Website: {school['Website']}
)) ) : (

No schools of higher education are available in your state for this career path.

)}
{/* Economic Projections */}

Economic Projections for {userState}

{economicProjections && typeof economicProjections === 'object' ? ( ) : (

No economic projections available for this career path.

)}
{/* Salary Data Points */}

Salary Data

{salaryData.length > 0 ? ( {salaryData.map((point, index) => ( ))}
Percentile Salary
{point.percentile} {point.value > 0 ? `$${parseInt(point.value, 10).toLocaleString()}` : 'N/A'}
) : (

Salary data is not available.

)}
{/* Loan Repayment Analysis */}

Loan Repayment Analysis

{/* Loan Repayment Calculation Results */} { const years = getProgramLength(school['CREDDESC']); return { schoolName: school['INSTNM'], inState: parseFloat(school['In_state cost'] * years) || 0, outOfState: parseFloat(school['Out_state cost'] * years) || 0, degreeType: school['CREDDESC'], }; })} salaryData={salaryData} setResults={setResults} setLoading={setLoadingCalculation} /> {/* Results Display */} {results.length > 0 && (

Comparisons by School over the life of the loan

{results.map((result, index) => (

{result.schoolName} - {result.degreeType || 'Degree type not available'}

Total Tuition: ${result.tuition}

Monthly Payment: ${result.monthlyPayment}

Total Monthly Payment (with extra): ${result.totalMonthlyPayment}

Total Loan Cost: ${result.totalLoanCost}

Net Gain: {result.netGain}

Monthly Salary (Gross): {result.monthlySalary}

))}
)}
); } export default PopoutPanel;