diff --git a/src/components/ScenarioEditModal.js b/src/components/ScenarioEditModal.js new file mode 100644 index 0000000..52dbf4c --- /dev/null +++ b/src/components/ScenarioEditModal.js @@ -0,0 +1,244 @@ +// src/components/ScenarioEditModal.js +import React, { useState, useEffect } from 'react'; +import authFetch from '../utils/authFetch'; + +const ScenarioEditModal = ({ + show, + onClose, + financialProfile, + setFinancialProfile, + collegeProfile, + setCollegeProfile, + apiURL, + authFetch, +}) => { + const [formData, setFormData] = useState({}); + + // Populate local formData whenever show=true + useEffect(() => { + if (!show) return; + + setFormData({ + // From financialProfile: + currentSalary: financialProfile?.current_salary ?? 0, + monthlyExpenses: financialProfile?.monthly_expenses ?? 0, + monthlyDebtPayments: financialProfile?.monthly_debt_payments ?? 0, + retirementSavings: financialProfile?.retirement_savings ?? 0, + emergencySavings: financialProfile?.emergency_fund ?? 0, + monthlyRetirementContribution: financialProfile?.retirement_contribution ?? 0, + monthlyEmergencyContribution: financialProfile?.emergency_contribution ?? 0, + surplusEmergencyAllocation: financialProfile?.extra_cash_emergency_pct ?? 50, + surplusRetirementAllocation: financialProfile?.extra_cash_retirement_pct ?? 50, + + // From collegeProfile: + studentLoanAmount: collegeProfile?.existing_college_debt ?? 0, + interestRate: collegeProfile?.interest_rate ?? 5, + loanTerm: collegeProfile?.loan_term ?? 10, + loanDeferralUntilGraduation: !!collegeProfile?.loan_deferral_until_graduation, + academicCalendar: collegeProfile?.academic_calendar ?? 'monthly', + annualFinancialAid: collegeProfile?.annual_financial_aid ?? 0, + calculatedTuition: collegeProfile?.tuition ?? 0, + extraPayment: collegeProfile?.extra_payment ?? 0, + partTimeIncome: 0, // or fetch from DB if you store it + gradDate: collegeProfile?.expected_graduation ?? '', + programType: collegeProfile?.program_type ?? '', + creditHoursPerYear: collegeProfile?.credit_hours_per_year ?? 0, + hoursCompleted: collegeProfile?.hours_completed ?? 0, + programLength: collegeProfile?.program_length ?? 0, + inCollege: + collegeProfile?.college_enrollment_status === 'currently_enrolled' || + collegeProfile?.college_enrollment_status === 'prospective_student', + expectedSalary: collegeProfile?.expected_salary ?? financialProfile?.current_salary ?? 0, + }); + }, [show, financialProfile, collegeProfile]); + + // Handle form changes in local state + const handleChange = (e) => { + const { name, type, value, checked } = e.target; + setFormData((prev) => ({ + ...prev, + [name]: + type === 'checkbox' + ? checked + : type === 'number' + ? parseFloat(value) || 0 + : value + })); + }; + + // SAVE: Update DB and local states, then close + const handleSave = async () => { + try { + // 1) Update the backend (financialProfile + collegeProfile): + // (Adjust endpoints/methods as needed in your codebase) + await authFetch(`${apiURL}/premium/financial-profile`, { + method: 'PUT', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ + current_salary: formData.currentSalary, + monthly_expenses: formData.monthlyExpenses, + monthly_debt_payments: formData.monthlyDebtPayments, + retirement_savings: formData.retirementSavings, + emergency_fund: formData.emergencySavings, + retirement_contribution: formData.monthlyRetirementContribution, + emergency_contribution: formData.monthlyEmergencyContribution, + extra_cash_emergency_pct: formData.surplusEmergencyAllocation, + extra_cash_retirement_pct: formData.surplusRetirementAllocation + }) + }); + + await authFetch(`${apiURL}/premium/college-profile`, { + method: 'PUT', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ + existing_college_debt: formData.studentLoanAmount, + interest_rate: formData.interestRate, + loan_term: formData.loanTerm, + loan_deferral_until_graduation: formData.loanDeferralUntilGraduation, + academic_calendar: formData.academicCalendar, + annual_financial_aid: formData.annualFinancialAid, + tuition: formData.calculatedTuition, + extra_payment: formData.extraPayment, + expected_graduation: formData.gradDate, + program_type: formData.programType, + credit_hours_per_year: formData.creditHoursPerYear, + hours_completed: formData.hoursCompleted, + program_length: formData.programLength, + college_enrollment_status: formData.inCollege + ? 'currently_enrolled' + : 'not_enrolled', + expected_salary: formData.expectedSalary + }) + }); + + // 2) Update local React state so your useEffect triggers re-simulation + setFinancialProfile((prev) => ({ + ...prev, + current_salary: formData.currentSalary, + monthly_expenses: formData.monthlyExpenses, + monthly_debt_payments: formData.monthlyDebtPayments, + retirement_savings: formData.retirementSavings, + emergency_fund: formData.emergencySavings, + retirement_contribution: formData.monthlyRetirementContribution, + emergency_contribution: formData.monthlyEmergencyContribution, + extra_cash_emergency_pct: formData.surplusEmergencyAllocation, + extra_cash_retirement_pct: formData.surplusRetirementAllocation + })); + + setCollegeProfile((prev) => ({ + ...prev, + existing_college_debt: formData.studentLoanAmount, + interest_rate: formData.interestRate, + loan_term: formData.loanTerm, + loan_deferral_until_graduation: formData.loanDeferralUntilGraduation, + academic_calendar: formData.academicCalendar, + annual_financial_aid: formData.annualFinancialAid, + tuition: formData.calculatedTuition, + extra_payment: formData.extraPayment, + expected_graduation: formData.gradDate, + program_type: formData.programType, + credit_hours_per_year: formData.creditHoursPerYear, + hours_completed: formData.hoursCompleted, + program_length: formData.programLength, + college_enrollment_status: formData.inCollege + ? 'currently_enrolled' + : 'not_enrolled', + expected_salary: formData.expectedSalary + })); + + // 3) Close the modal + onClose(); + } catch (err) { + console.error('Error saving scenario changes:', err); + // Optionally show a toast or error UI + } + }; + + // If show=false, don't render anything + if (!show) return null; + + return ( +