import React, { useState } from 'react'; import { Button } from './ui/button.js'; /** * LoanRepayment * ──────────────────────────────────────────────────────────────── * If `schools.length === 0` we first show a quick-fill estimator * that lets a free-tier user type a school name, choose “degree type”, * and enter an annual-tuition guess. We then create a minimal * school object and push it into `schools`, after which the full * repayment form (your original code) appears. */ function LoanRepayment({ /* ORIGINAL PROPS */ schools = [], salaryData, setResults, setLoading, setPersistedROI, programLength, /* NEW: parent must hand us a setter so we can inject the stub */ setSchools, }) { /* ------------------------- quick-fill state ------------------------- */ const [scratch, setScratch] = useState({ school: '', programType: '', tuition: '', }); /* ------------------------- calculator state ------------------------ */ const [expectedSalary, setExpectedSalary] = useState(0); const [tuitionType, setTuitionType] = useState('inState'); const [interestRate, setInterestRate] = useState(5.5); const [loanTerm, setLoanTerm] = useState(10); const [extraPayment, setExtraPayment] = useState(0); const [currentSalary, setCurrentSalary] = useState(0); const [error, setError] = useState(null); /* ------------------------- validation ------------------------------ */ const validateInputs = () => { if (!schools?.length) { setError('Missing school data.'); return false; } if (isNaN(interestRate)||interestRate<=0) { setError('Interest rate > 0'); return false; } if (isNaN(loanTerm)||loanTerm<=0) { setError('Loan term > 0'); return false; } if (isNaN(extraPayment)||extraPayment<0) { setError('Extra pmt ≥ 0'); return false; } if (isNaN(currentSalary)||currentSalary<0){ setError('Current salary ≥0');return false; } if (isNaN(expectedSalary)||expectedSalary<0){setError('Expected salary ≥0');return false;} setError(null); return true; }; /* ------------------------- main calculation ----------------------- */ const calculateLoanDetails = () => { if (!validateInputs()) return; setLoading?.(true); const pickTuition = (school, resid, grad) => { const tryNum = v => isNaN(v) ? 0 : Number(v); if (grad) { return resid === 'inState' ? tryNum(school.inStateGraduate) || tryNum(school.inState) || tryNum(school.tuition) : tryNum(school.outStateGraduate) || tryNum(school.outOfState)|| tryNum(school.tuition); } /* under-grad */ return resid === 'inState' ? tryNum(school.inState) || tryNum(school.inStateGraduate) || tryNum(school.tuition) : tryNum(school.outOfState) || tryNum(school.outStateGraduate) || tryNum(school.tuition); }; const results = schools.map((school) => { /* your existing repayment logic — unchanged */ const programLen = Number(school.programLength); let ugYears=0, gradYears=0; if (school.degreeType.includes('Associate')) ugYears=2; else if (school.degreeType.includes('Bachelor')) ugYears=4; else if (school.degreeType.includes('Master')) { ugYears=4; gradYears=2; } else if (school.degreeType.includes('First Professional')||school.degreeType.includes('Doctoral')) { ugYears=4; gradYears=4; } else if (school.degreeType.includes('Certificate')) ugYears=1; else { ugYears=Math.min(programLen,4); gradYears=Math.max(programLen-4,0); } const ugTuit = pickTuition(school, tuitionType, /*grad?*/ false); const gradTuit = pickTuition(school, tuitionType, /*grad?*/ true); let totalTuition = ugYears * ugTuit + gradYears* gradTuit; const r = Number(interestRate)/12/100; const n = Number(loanTerm)*12; const pmtMin = totalTuition * (r*Math.pow(1+r,n))/(Math.pow(1+r,n)-1); const pmt = Number(pmtMin)+Number(extraPayment); let bal=totalTuition, months=0; while (bal>0 && months

Estimate student-loan payments

setScratch({...scratch,school:e.target.value})} /> setScratch({...scratch,tuition:e.target.value})} /> ); } /* ================================================================= */ /* ORIGINAL DETAILED REPAYMENT FORM */ /* ================================================================= */ return (
{e.preventDefault();calculateLoanDetails();}} className="space-y-4 border rounded p-6"> {/* Tuition type */}
{/* Interest rate */}
setInterestRate(e.target.value)} className="border rounded p-2 w-full"/>
{/* Loan term */}
setLoanTerm(e.target.value)} className="border rounded p-2 w-full"/>
{/* Extra payment */}
setExtraPayment(e.target.value)} className="border rounded p-2 w-full"/>
{/* Current salary */}
setCurrentSalary(e.target.value)} className="border rounded p-2 w-full"/>
{/* Expected salary */}
setExpectedSalary(e.target.value)} className="border rounded p-2 w-full"/>
{/* Submit */}
{error &&
{error}
}
); } export default LoanRepayment;