Spaces:
Sleeping
Sleeping
Commit Β·
6a71f57
1
Parent(s): b6e2e3b
feat: implement Goals, Loans, and Settings pages with real APIs
Browse files- backend/app/auth/router.py +38 -0
- backend/app/goals/__init__.py +0 -0
- backend/app/goals/router.py +85 -0
- backend/app/loans/__init__.py +0 -0
- backend/app/loans/router.py +58 -0
- backend/app/main.py +4 -0
- frontend/src/app/goals/page.tsx +148 -2
- frontend/src/app/loans/page.tsx +163 -2
- frontend/src/app/settings/page.tsx +216 -2
- frontend/src/lib/api.ts +80 -1
backend/app/auth/router.py
CHANGED
|
@@ -175,13 +175,51 @@ def refresh_token(req: RefreshRequest, db: Session = Depends(get_db)):
|
|
| 175 |
|
| 176 |
@router.get("/me")
|
| 177 |
def get_me(current_user: User = Depends(get_current_user)):
|
|
|
|
| 178 |
return {
|
| 179 |
"user_id": current_user.id,
|
| 180 |
"email": current_user.email,
|
| 181 |
"name": current_user.profile_data.get("name", "User"),
|
| 182 |
"financial_personality": current_user.financial_personality,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
}
|
| 184 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 185 |
@router.post("/logout")
|
| 186 |
def logout():
|
| 187 |
# Stateless JWT β client drops the token.
|
|
|
|
| 175 |
|
| 176 |
@router.get("/me")
|
| 177 |
def get_me(current_user: User = Depends(get_current_user)):
|
| 178 |
+
settings = current_user.ai_personalization_settings or {}
|
| 179 |
return {
|
| 180 |
"user_id": current_user.id,
|
| 181 |
"email": current_user.email,
|
| 182 |
"name": current_user.profile_data.get("name", "User"),
|
| 183 |
"financial_personality": current_user.financial_personality,
|
| 184 |
+
"ai_personalization_settings": settings,
|
| 185 |
+
"notifications_enabled": settings.get("notifications_enabled", True),
|
| 186 |
+
"ai_coaching_enabled": settings.get("ai_coaching_enabled", True),
|
| 187 |
+
"fraud_alerts_enabled": settings.get("fraud_alerts_enabled", True),
|
| 188 |
}
|
| 189 |
|
| 190 |
+
|
| 191 |
+
class SettingsUpdateRequest(BaseModel):
|
| 192 |
+
name: Optional[str] = None
|
| 193 |
+
financial_personality: Optional[str] = None
|
| 194 |
+
notifications_enabled: Optional[bool] = None
|
| 195 |
+
ai_coaching_enabled: Optional[bool] = None
|
| 196 |
+
fraud_alerts_enabled: Optional[bool] = None
|
| 197 |
+
|
| 198 |
+
|
| 199 |
+
@router.patch("/settings")
|
| 200 |
+
def update_settings(
|
| 201 |
+
body: SettingsUpdateRequest,
|
| 202 |
+
current_user: User = Depends(get_current_user),
|
| 203 |
+
db: Session = Depends(get_db),
|
| 204 |
+
):
|
| 205 |
+
if body.name is not None:
|
| 206 |
+
profile = dict(current_user.profile_data or {})
|
| 207 |
+
profile["name"] = body.name.strip()
|
| 208 |
+
current_user.profile_data = profile
|
| 209 |
+
if body.financial_personality is not None:
|
| 210 |
+
current_user.financial_personality = body.financial_personality.strip()
|
| 211 |
+
settings = dict(current_user.ai_personalization_settings or {})
|
| 212 |
+
if body.notifications_enabled is not None:
|
| 213 |
+
settings["notifications_enabled"] = body.notifications_enabled
|
| 214 |
+
if body.ai_coaching_enabled is not None:
|
| 215 |
+
settings["ai_coaching_enabled"] = body.ai_coaching_enabled
|
| 216 |
+
if body.fraud_alerts_enabled is not None:
|
| 217 |
+
settings["fraud_alerts_enabled"] = body.fraud_alerts_enabled
|
| 218 |
+
current_user.ai_personalization_settings = settings
|
| 219 |
+
db.commit()
|
| 220 |
+
db.refresh(current_user)
|
| 221 |
+
return get_me(current_user)
|
| 222 |
+
|
| 223 |
@router.post("/logout")
|
| 224 |
def logout():
|
| 225 |
# Stateless JWT β client drops the token.
|
backend/app/goals/__init__.py
ADDED
|
File without changes
|
backend/app/goals/router.py
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from datetime import datetime
|
| 2 |
+
from typing import Optional
|
| 3 |
+
|
| 4 |
+
from fastapi import APIRouter, Depends, HTTPException
|
| 5 |
+
from pydantic import BaseModel, Field
|
| 6 |
+
from sqlalchemy.orm import Session
|
| 7 |
+
|
| 8 |
+
from app.database.database import get_db
|
| 9 |
+
from app.database.models import Goal, User
|
| 10 |
+
|
| 11 |
+
router = APIRouter(prefix="/api/goals", tags=["Goals"])
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def _resolve_user(db: Session, user_id: Optional[str]) -> str:
|
| 15 |
+
if user_id:
|
| 16 |
+
return user_id
|
| 17 |
+
user = db.query(User).first()
|
| 18 |
+
if not user:
|
| 19 |
+
raise HTTPException(status_code=404, detail="No users found. Seed the database first.")
|
| 20 |
+
return user.id
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
class ContributeRequest(BaseModel):
|
| 24 |
+
amount: float = Field(gt=0)
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
@router.get("")
|
| 28 |
+
def list_goals(user_id: Optional[str] = None, db: Session = Depends(get_db)):
|
| 29 |
+
uid = _resolve_user(db, user_id)
|
| 30 |
+
goals = db.query(Goal).filter(Goal.user_id == uid).order_by(Goal.target_date.asc()).all()
|
| 31 |
+
items = []
|
| 32 |
+
total_target = 0.0
|
| 33 |
+
total_saved = 0.0
|
| 34 |
+
for g in goals:
|
| 35 |
+
progress = min(100.0, (g.current_amount / g.target_amount * 100) if g.target_amount > 0 else 0)
|
| 36 |
+
total_target += g.target_amount
|
| 37 |
+
total_saved += g.current_amount
|
| 38 |
+
days_left = None
|
| 39 |
+
if g.target_date:
|
| 40 |
+
days_left = max(0, (g.target_date.replace(tzinfo=None) - datetime.utcnow()).days)
|
| 41 |
+
plan = g.ai_generated_plan or {}
|
| 42 |
+
items.append({
|
| 43 |
+
"id": g.id,
|
| 44 |
+
"title": g.title,
|
| 45 |
+
"target_amount": g.target_amount,
|
| 46 |
+
"current_amount": g.current_amount,
|
| 47 |
+
"progress_percent": round(progress, 1),
|
| 48 |
+
"target_date": g.target_date.isoformat() if g.target_date else None,
|
| 49 |
+
"days_left": days_left,
|
| 50 |
+
"monthly_contribution": plan.get("monthly_contribution"),
|
| 51 |
+
"months_remaining": plan.get("months_remaining"),
|
| 52 |
+
"on_track": progress >= 50 or (days_left is not None and days_left > 180),
|
| 53 |
+
})
|
| 54 |
+
return {
|
| 55 |
+
"goals": items,
|
| 56 |
+
"summary": {
|
| 57 |
+
"count": len(items),
|
| 58 |
+
"total_target": round(total_target, 2),
|
| 59 |
+
"total_saved": round(total_saved, 2),
|
| 60 |
+
"overall_progress": round(total_saved / total_target * 100, 1) if total_target > 0 else 0,
|
| 61 |
+
},
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
@router.post("/{goal_id}/contribute")
|
| 66 |
+
def contribute_to_goal(
|
| 67 |
+
goal_id: str,
|
| 68 |
+
body: ContributeRequest,
|
| 69 |
+
user_id: Optional[str] = None,
|
| 70 |
+
db: Session = Depends(get_db),
|
| 71 |
+
):
|
| 72 |
+
uid = _resolve_user(db, user_id)
|
| 73 |
+
goal = db.query(Goal).filter(Goal.id == goal_id, Goal.user_id == uid).first()
|
| 74 |
+
if not goal:
|
| 75 |
+
raise HTTPException(status_code=404, detail="Goal not found")
|
| 76 |
+
goal.current_amount = min(goal.target_amount, goal.current_amount + body.amount)
|
| 77 |
+
db.commit()
|
| 78 |
+
db.refresh(goal)
|
| 79 |
+
progress = min(100.0, goal.current_amount / goal.target_amount * 100) if goal.target_amount > 0 else 0
|
| 80 |
+
return {
|
| 81 |
+
"id": goal.id,
|
| 82 |
+
"current_amount": goal.current_amount,
|
| 83 |
+
"progress_percent": round(progress, 1),
|
| 84 |
+
"completed": goal.current_amount >= goal.target_amount,
|
| 85 |
+
}
|
backend/app/loans/__init__.py
ADDED
|
File without changes
|
backend/app/loans/router.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Optional
|
| 2 |
+
|
| 3 |
+
from fastapi import APIRouter, Depends, Query
|
| 4 |
+
from pydantic import BaseModel, Field
|
| 5 |
+
from sqlalchemy.orm import Session
|
| 6 |
+
|
| 7 |
+
from app.database.database import get_db
|
| 8 |
+
from app.database.models import User
|
| 9 |
+
from app.ai.loan_predictor import calculate_loan_eligibility, generate_loan_comparison
|
| 10 |
+
|
| 11 |
+
router = APIRouter(prefix="/api/loans", tags=["Loans"])
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def _resolve_user(db: Session, user_id: Optional[str]) -> str:
|
| 15 |
+
if user_id:
|
| 16 |
+
return user_id
|
| 17 |
+
user = db.query(User).first()
|
| 18 |
+
if user:
|
| 19 |
+
return user.id
|
| 20 |
+
return "anonymous"
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
class LoanEligibilityRequest(BaseModel):
|
| 24 |
+
salary: float = Field(gt=0, description="Annual salary in USD")
|
| 25 |
+
credit_score: int = Field(ge=300, le=850)
|
| 26 |
+
existing_loans: int = Field(ge=0, le=20)
|
| 27 |
+
employment_years: float = Field(ge=0, le=50)
|
| 28 |
+
age: int = Field(ge=18, le=80)
|
| 29 |
+
loan_amount: float = Field(gt=0)
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
@router.post("/eligibility")
|
| 33 |
+
def loan_eligibility(
|
| 34 |
+
body: LoanEligibilityRequest,
|
| 35 |
+
user_id: Optional[str] = None,
|
| 36 |
+
db: Session = Depends(get_db),
|
| 37 |
+
):
|
| 38 |
+
_resolve_user(db, user_id)
|
| 39 |
+
result = calculate_loan_eligibility(
|
| 40 |
+
body.salary,
|
| 41 |
+
body.credit_score,
|
| 42 |
+
body.existing_loans,
|
| 43 |
+
body.employment_years,
|
| 44 |
+
body.age,
|
| 45 |
+
body.loan_amount,
|
| 46 |
+
)
|
| 47 |
+
comparison = generate_loan_comparison(body.loan_amount)
|
| 48 |
+
return {**result, "comparison": comparison[:12]}
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
@router.get("/comparison")
|
| 52 |
+
def loan_comparison(
|
| 53 |
+
loan_amount: float = Query(..., gt=0),
|
| 54 |
+
user_id: Optional[str] = None,
|
| 55 |
+
db: Session = Depends(get_db),
|
| 56 |
+
):
|
| 57 |
+
_resolve_user(db, user_id)
|
| 58 |
+
return {"loan_amount": loan_amount, "comparison": generate_loan_comparison(loan_amount)}
|
backend/app/main.py
CHANGED
|
@@ -22,6 +22,8 @@ from app.dashboard.router import router as dashboard_router
|
|
| 22 |
from app.notifications.router import router as notifications_router
|
| 23 |
from app.transactions.router import router as transactions_router
|
| 24 |
from app.payments.router import router as payments_router
|
|
|
|
|
|
|
| 25 |
|
| 26 |
# βββ Observability ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 27 |
from app.middleware.logging import RequestLoggingMiddleware, metrics, api_logger
|
|
@@ -134,6 +136,8 @@ app.include_router(dashboard_router)
|
|
| 134 |
app.include_router(notifications_router)
|
| 135 |
app.include_router(transactions_router)
|
| 136 |
app.include_router(payments_router)
|
|
|
|
|
|
|
| 137 |
|
| 138 |
# βββ Core endpoints βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 139 |
@app.get("/", tags=["Core"])
|
|
|
|
| 22 |
from app.notifications.router import router as notifications_router
|
| 23 |
from app.transactions.router import router as transactions_router
|
| 24 |
from app.payments.router import router as payments_router
|
| 25 |
+
from app.goals.router import router as goals_router
|
| 26 |
+
from app.loans.router import router as loans_router
|
| 27 |
|
| 28 |
# βββ Observability ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 29 |
from app.middleware.logging import RequestLoggingMiddleware, metrics, api_logger
|
|
|
|
| 136 |
app.include_router(notifications_router)
|
| 137 |
app.include_router(transactions_router)
|
| 138 |
app.include_router(payments_router)
|
| 139 |
+
app.include_router(goals_router)
|
| 140 |
+
app.include_router(loans_router)
|
| 141 |
|
| 142 |
# βββ Core endpoints βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 143 |
@app.get("/", tags=["Core"])
|
frontend/src/app/goals/page.tsx
CHANGED
|
@@ -1,4 +1,150 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
export default function GoalsPage() {
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
}
|
|
|
|
| 1 |
+
"use client";
|
| 2 |
+
|
| 3 |
+
import { useEffect, useState, useCallback } from "react";
|
| 4 |
+
import { motion } from "framer-motion";
|
| 5 |
+
import { Target, TrendingUp, Calendar, Sparkles, Plus, CheckCircle2 } from "lucide-react";
|
| 6 |
+
import { goalsApi, FinancialGoal } from "@/lib/api";
|
| 7 |
+
import { useAuthStore } from "@/lib/stores/authStore";
|
| 8 |
+
|
| 9 |
+
function Skeleton({ className }: { className?: string }) {
|
| 10 |
+
return <div className={`shimmer rounded-lg ${className}`} />;
|
| 11 |
+
}
|
| 12 |
+
|
| 13 |
export default function GoalsPage() {
|
| 14 |
+
const { user } = useAuthStore();
|
| 15 |
+
const [goals, setGoals] = useState<FinancialGoal[]>([]);
|
| 16 |
+
const [summary, setSummary] = useState({ count: 0, total_target: 0, total_saved: 0, overall_progress: 0 });
|
| 17 |
+
const [loading, setLoading] = useState(true);
|
| 18 |
+
const [error, setError] = useState<string | null>(null);
|
| 19 |
+
const [contributing, setContributing] = useState<string | null>(null);
|
| 20 |
+
|
| 21 |
+
const load = useCallback(async () => {
|
| 22 |
+
setLoading(true);
|
| 23 |
+
setError(null);
|
| 24 |
+
try {
|
| 25 |
+
const res = await goalsApi.list(user?.user_id);
|
| 26 |
+
setGoals(res.goals);
|
| 27 |
+
setSummary(res.summary);
|
| 28 |
+
} catch (e) {
|
| 29 |
+
setError((e as Error).message);
|
| 30 |
+
} finally {
|
| 31 |
+
setLoading(false);
|
| 32 |
+
}
|
| 33 |
+
}, [user?.user_id]);
|
| 34 |
+
|
| 35 |
+
useEffect(() => {
|
| 36 |
+
load();
|
| 37 |
+
}, [load]);
|
| 38 |
+
|
| 39 |
+
const handleContribute = async (goalId: string, amount: number) => {
|
| 40 |
+
setContributing(goalId);
|
| 41 |
+
try {
|
| 42 |
+
await goalsApi.contribute(goalId, amount, user?.user_id);
|
| 43 |
+
await load();
|
| 44 |
+
} catch {
|
| 45 |
+
/* ignore */
|
| 46 |
+
} finally {
|
| 47 |
+
setContributing(null);
|
| 48 |
+
}
|
| 49 |
+
};
|
| 50 |
+
|
| 51 |
+
return (
|
| 52 |
+
<motion.div initial={{ opacity: 0 }} animate={{ opacity: 1 }} className="flex flex-col gap-6">
|
| 53 |
+
<div>
|
| 54 |
+
<h1 className="text-3xl font-bold tracking-tight text-white">Financial Goals</h1>
|
| 55 |
+
<p className="text-zinc-400 mt-1 text-sm">Track savings targets with AI-powered contribution plans</p>
|
| 56 |
+
</div>
|
| 57 |
+
|
| 58 |
+
<div className="grid grid-cols-2 gap-4 sm:grid-cols-4">
|
| 59 |
+
{[
|
| 60 |
+
{ label: "Active Goals", value: loading ? "β" : String(summary.count), icon: Target, color: "text-emerald-400" },
|
| 61 |
+
{ label: "Total Saved", value: loading ? "β" : `$${summary.total_saved.toLocaleString()}`, icon: TrendingUp, color: "text-blue-400" },
|
| 62 |
+
{ label: "Target Amount", value: loading ? "β" : `$${summary.total_target.toLocaleString()}`, icon: Calendar, color: "text-purple-400" },
|
| 63 |
+
{ label: "Overall Progress", value: loading ? "β" : `${summary.overall_progress}%`, icon: Sparkles, color: "text-amber-400" },
|
| 64 |
+
].map((s) => (
|
| 65 |
+
<div key={s.label} className="glass-card p-5">
|
| 66 |
+
<p className="text-xs text-zinc-500 uppercase tracking-wider">{s.label}</p>
|
| 67 |
+
<p className={`mt-2 text-2xl font-bold ${s.color}`}>{s.value}</p>
|
| 68 |
+
</div>
|
| 69 |
+
))}
|
| 70 |
+
</div>
|
| 71 |
+
|
| 72 |
+
{error && (
|
| 73 |
+
<div className="rounded-2xl border border-amber-500/20 bg-amber-500/5 px-5 py-3 text-sm text-amber-400">
|
| 74 |
+
Could not load goals β {error}
|
| 75 |
+
</div>
|
| 76 |
+
)}
|
| 77 |
+
|
| 78 |
+
{loading ? (
|
| 79 |
+
<div className="space-y-4">
|
| 80 |
+
{[1, 2, 3].map((i) => <Skeleton key={i} className="h-32 w-full rounded-2xl" />)}
|
| 81 |
+
</div>
|
| 82 |
+
) : goals.length === 0 ? (
|
| 83 |
+
<div className="glass-card flex flex-col items-center gap-3 py-16 text-center">
|
| 84 |
+
<Target className="h-12 w-12 text-zinc-600" />
|
| 85 |
+
<p className="text-zinc-400">No goals yet. Run seed_demo to load sample goals.</p>
|
| 86 |
+
</div>
|
| 87 |
+
) : (
|
| 88 |
+
<div className="grid gap-4 lg:grid-cols-2">
|
| 89 |
+
{goals.map((g) => (
|
| 90 |
+
<motion.div
|
| 91 |
+
key={g.id}
|
| 92 |
+
layout
|
| 93 |
+
className="glass-card p-6 border border-white/8 hover:border-emerald-500/20 transition-colors"
|
| 94 |
+
>
|
| 95 |
+
<div className="flex items-start justify-between gap-3">
|
| 96 |
+
<div>
|
| 97 |
+
<h2 className="text-lg font-semibold text-white">{g.title}</h2>
|
| 98 |
+
<p className="text-sm text-zinc-400 mt-0.5">
|
| 99 |
+
${g.current_amount.toLocaleString()} of ${g.target_amount.toLocaleString()}
|
| 100 |
+
</p>
|
| 101 |
+
</div>
|
| 102 |
+
{g.progress_percent >= 100 ? (
|
| 103 |
+
<CheckCircle2 className="h-6 w-6 text-emerald-400 flex-shrink-0" />
|
| 104 |
+
) : (
|
| 105 |
+
<span className={`text-xs font-semibold px-2 py-1 rounded-full ${g.on_track ? "bg-emerald-500/15 text-emerald-400" : "bg-amber-500/15 text-amber-400"}`}>
|
| 106 |
+
{g.on_track ? "On track" : "Needs attention"}
|
| 107 |
+
</span>
|
| 108 |
+
)}
|
| 109 |
+
</div>
|
| 110 |
+
|
| 111 |
+
<div className="mt-4 h-2.5 rounded-full bg-white/10 overflow-hidden">
|
| 112 |
+
<motion.div
|
| 113 |
+
initial={{ width: 0 }}
|
| 114 |
+
animate={{ width: `${Math.min(100, g.progress_percent)}%` }}
|
| 115 |
+
transition={{ duration: 0.8 }}
|
| 116 |
+
className="h-full rounded-full bg-gradient-to-r from-emerald-500 to-cyan-500"
|
| 117 |
+
/>
|
| 118 |
+
</div>
|
| 119 |
+
<p className="text-xs text-zinc-500 mt-1">{g.progress_percent}% complete</p>
|
| 120 |
+
|
| 121 |
+
<div className="mt-4 flex flex-wrap gap-4 text-xs text-zinc-400">
|
| 122 |
+
{g.days_left != null && <span>{g.days_left} days left</span>}
|
| 123 |
+
{g.monthly_contribution != null && (
|
| 124 |
+
<span>AI plan: ${g.monthly_contribution.toLocaleString()}/mo Β· {g.months_remaining} mo</span>
|
| 125 |
+
)}
|
| 126 |
+
</div>
|
| 127 |
+
|
| 128 |
+
{g.progress_percent < 100 && (
|
| 129 |
+
<div className="mt-4 flex gap-2">
|
| 130 |
+
{[100, 250, 500].map((amt) => (
|
| 131 |
+
<button
|
| 132 |
+
key={amt}
|
| 133 |
+
type="button"
|
| 134 |
+
disabled={contributing === g.id}
|
| 135 |
+
onClick={() => handleContribute(g.id, amt)}
|
| 136 |
+
className="flex items-center gap-1 rounded-lg border border-white/10 bg-white/5 px-3 py-1.5 text-xs text-zinc-300 hover:bg-emerald-500/10 hover:border-emerald-500/30 hover:text-emerald-400 transition-all disabled:opacity-50"
|
| 137 |
+
>
|
| 138 |
+
<Plus className="h-3 w-3" />
|
| 139 |
+
+${amt}
|
| 140 |
+
</button>
|
| 141 |
+
))}
|
| 142 |
+
</div>
|
| 143 |
+
)}
|
| 144 |
+
</motion.div>
|
| 145 |
+
))}
|
| 146 |
+
</div>
|
| 147 |
+
)}
|
| 148 |
+
</motion.div>
|
| 149 |
+
);
|
| 150 |
}
|
frontend/src/app/loans/page.tsx
CHANGED
|
@@ -1,4 +1,165 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
export default function LoansPage() {
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
}
|
|
|
|
| 1 |
+
"use client";
|
| 2 |
+
|
| 3 |
+
import { useState } from "react";
|
| 4 |
+
import { motion } from "framer-motion";
|
| 5 |
+
import { Landmark, Calculator, Shield, Sparkles, Loader2 } from "lucide-react";
|
| 6 |
+
import { loansApi } from "@/lib/api";
|
| 7 |
+
import { useAuthStore } from "@/lib/stores/authStore";
|
| 8 |
+
|
| 9 |
export default function LoansPage() {
|
| 10 |
+
const { user } = useAuthStore();
|
| 11 |
+
const [form, setForm] = useState({
|
| 12 |
+
salary: 85000,
|
| 13 |
+
credit_score: 720,
|
| 14 |
+
existing_loans: 1,
|
| 15 |
+
employment_years: 4,
|
| 16 |
+
age: 32,
|
| 17 |
+
loan_amount: 25000,
|
| 18 |
+
});
|
| 19 |
+
const [loading, setLoading] = useState(false);
|
| 20 |
+
const [result, setResult] = useState<Awaited<ReturnType<typeof loansApi.eligibility>> | null>(null);
|
| 21 |
+
const [error, setError] = useState<string | null>(null);
|
| 22 |
+
|
| 23 |
+
const analyze = async (e: React.FormEvent) => {
|
| 24 |
+
e.preventDefault();
|
| 25 |
+
setLoading(true);
|
| 26 |
+
setError(null);
|
| 27 |
+
try {
|
| 28 |
+
const res = await loansApi.eligibility(form, user?.user_id);
|
| 29 |
+
setResult(res);
|
| 30 |
+
} catch (err) {
|
| 31 |
+
setError((err as Error).message);
|
| 32 |
+
setResult(null);
|
| 33 |
+
} finally {
|
| 34 |
+
setLoading(false);
|
| 35 |
+
}
|
| 36 |
+
};
|
| 37 |
+
|
| 38 |
+
const scoreColor =
|
| 39 |
+
result && result.loan_score >= 70 ? "text-emerald-400" : result && result.loan_score >= 50 ? "text-amber-400" : "text-red-400";
|
| 40 |
+
|
| 41 |
+
return (
|
| 42 |
+
<motion.div initial={{ opacity: 0 }} animate={{ opacity: 1 }} className="flex flex-col gap-6">
|
| 43 |
+
<div>
|
| 44 |
+
<h1 className="text-3xl font-bold tracking-tight text-white">Loans</h1>
|
| 45 |
+
<p className="text-zinc-400 mt-1 text-sm">AI eligibility scoring, EMI estimates, and rate comparisons</p>
|
| 46 |
+
</div>
|
| 47 |
+
|
| 48 |
+
<div className="grid gap-6 lg:grid-cols-2">
|
| 49 |
+
<form onSubmit={analyze} className="glass-card p-6 space-y-4">
|
| 50 |
+
<h2 className="text-base font-semibold text-white flex items-center gap-2">
|
| 51 |
+
<Calculator className="h-4 w-4 text-emerald-400" />
|
| 52 |
+
Loan application details
|
| 53 |
+
</h2>
|
| 54 |
+
{[
|
| 55 |
+
{ key: "salary", label: "Annual salary ($)", type: "number" },
|
| 56 |
+
{ key: "loan_amount", label: "Loan amount ($)", type: "number" },
|
| 57 |
+
{ key: "credit_score", label: "Credit score", type: "number", min: 300, max: 850 },
|
| 58 |
+
{ key: "existing_loans", label: "Existing loans (count)", type: "number", min: 0 },
|
| 59 |
+
{ key: "employment_years", label: "Years employed", type: "number", step: 0.5 },
|
| 60 |
+
{ key: "age", label: "Age", type: "number", min: 18, max: 80 },
|
| 61 |
+
].map((f) => (
|
| 62 |
+
<div key={f.key}>
|
| 63 |
+
<label className="text-xs text-zinc-400">{f.label}</label>
|
| 64 |
+
<input
|
| 65 |
+
type="number"
|
| 66 |
+
value={form[f.key as keyof typeof form]}
|
| 67 |
+
onChange={(e) => setForm({ ...form, [f.key]: Number(e.target.value) })}
|
| 68 |
+
className="mt-1 w-full rounded-xl border border-white/10 bg-white/5 px-3 py-2 text-sm text-white focus:border-emerald-500/40 focus:outline-none"
|
| 69 |
+
min={f.min}
|
| 70 |
+
max={f.max}
|
| 71 |
+
step={f.step ?? 1}
|
| 72 |
+
/>
|
| 73 |
+
</div>
|
| 74 |
+
))}
|
| 75 |
+
<button
|
| 76 |
+
type="submit"
|
| 77 |
+
disabled={loading}
|
| 78 |
+
className="w-full flex items-center justify-center gap-2 rounded-xl bg-gradient-to-r from-emerald-500 to-cyan-500 py-3 text-sm font-semibold text-white disabled:opacity-50"
|
| 79 |
+
>
|
| 80 |
+
{loading ? <Loader2 className="h-4 w-4 animate-spin" /> : <Sparkles className="h-4 w-4" />}
|
| 81 |
+
Analyze eligibility
|
| 82 |
+
</button>
|
| 83 |
+
</form>
|
| 84 |
+
|
| 85 |
+
<div className="space-y-4">
|
| 86 |
+
{!result && !error && (
|
| 87 |
+
<div className="glass-card flex flex-col items-center justify-center gap-3 py-16 text-center h-full min-h-[280px]">
|
| 88 |
+
<Landmark className="h-12 w-12 text-zinc-600" />
|
| 89 |
+
<p className="text-zinc-400 text-sm">Enter your details and run analysis</p>
|
| 90 |
+
</div>
|
| 91 |
+
)}
|
| 92 |
+
{error && (
|
| 93 |
+
<div className="glass-card p-5 border border-red-500/20 text-red-400 text-sm">{error}</div>
|
| 94 |
+
)}
|
| 95 |
+
{result && (
|
| 96 |
+
<>
|
| 97 |
+
<div className="glass-card p-6 border border-emerald-500/20">
|
| 98 |
+
<p className="text-xs text-zinc-500 uppercase tracking-wider">Decision</p>
|
| 99 |
+
<p className="text-xl font-bold text-white mt-1">{result.approval_status}</p>
|
| 100 |
+
<div className="mt-4 grid grid-cols-2 gap-4">
|
| 101 |
+
<div>
|
| 102 |
+
<p className="text-xs text-zinc-500">Approval chance</p>
|
| 103 |
+
<p className="text-2xl font-bold text-emerald-400">{result.approval_probability}%</p>
|
| 104 |
+
</div>
|
| 105 |
+
<div>
|
| 106 |
+
<p className="text-xs text-zinc-500">Loan score</p>
|
| 107 |
+
<p className={`text-2xl font-bold ${scoreColor}`}>{result.loan_score}</p>
|
| 108 |
+
</div>
|
| 109 |
+
<div>
|
| 110 |
+
<p className="text-xs text-zinc-500">Monthly EMI</p>
|
| 111 |
+
<p className="text-lg font-semibold text-white">${result.monthly_emi.toLocaleString()}</p>
|
| 112 |
+
</div>
|
| 113 |
+
<div>
|
| 114 |
+
<p className="text-xs text-zinc-500">Risk level</p>
|
| 115 |
+
<p className="text-lg font-semibold text-amber-400">{result.risk_level}</p>
|
| 116 |
+
</div>
|
| 117 |
+
</div>
|
| 118 |
+
</div>
|
| 119 |
+
|
| 120 |
+
{result.recommendations.length > 0 && (
|
| 121 |
+
<div className="glass-card p-5">
|
| 122 |
+
<p className="text-sm font-semibold text-white mb-2 flex items-center gap-2">
|
| 123 |
+
<Shield className="h-4 w-4 text-cyan-400" />
|
| 124 |
+
AI recommendations
|
| 125 |
+
</p>
|
| 126 |
+
<ul className="space-y-2">
|
| 127 |
+
{result.recommendations.map((r, i) => (
|
| 128 |
+
<li key={i} className="text-sm text-zinc-300">{r}</li>
|
| 129 |
+
))}
|
| 130 |
+
</ul>
|
| 131 |
+
</div>
|
| 132 |
+
)}
|
| 133 |
+
|
| 134 |
+
{result.comparison.length > 0 && (
|
| 135 |
+
<div className="glass-card p-5 overflow-x-auto">
|
| 136 |
+
<p className="text-sm font-semibold text-white mb-3">Rate & tenure comparison</p>
|
| 137 |
+
<table className="w-full text-xs">
|
| 138 |
+
<thead>
|
| 139 |
+
<tr className="text-zinc-500 text-left">
|
| 140 |
+
<th className="pb-2 pr-4">Rate</th>
|
| 141 |
+
<th className="pb-2 pr-4">Tenure</th>
|
| 142 |
+
<th className="pb-2 pr-4">EMI</th>
|
| 143 |
+
<th className="pb-2">Interest</th>
|
| 144 |
+
</tr>
|
| 145 |
+
</thead>
|
| 146 |
+
<tbody>
|
| 147 |
+
{result.comparison.slice(0, 8).map((row, i) => (
|
| 148 |
+
<tr key={i} className="border-t border-white/5 text-zinc-300">
|
| 149 |
+
<td className="py-2 pr-4">{row.rate}</td>
|
| 150 |
+
<td className="py-2 pr-4">{row.tenure}</td>
|
| 151 |
+
<td className="py-2 pr-4 text-white">${row.emi.toLocaleString()}</td>
|
| 152 |
+
<td className="py-2">${row.interest.toLocaleString()}</td>
|
| 153 |
+
</tr>
|
| 154 |
+
))}
|
| 155 |
+
</tbody>
|
| 156 |
+
</table>
|
| 157 |
+
</div>
|
| 158 |
+
)}
|
| 159 |
+
</>
|
| 160 |
+
)}
|
| 161 |
+
</div>
|
| 162 |
+
</div>
|
| 163 |
+
</motion.div>
|
| 164 |
+
);
|
| 165 |
}
|
frontend/src/app/settings/page.tsx
CHANGED
|
@@ -1,4 +1,218 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
export default function SettingsPage() {
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
}
|
|
|
|
| 1 |
+
"use client";
|
| 2 |
+
|
| 3 |
+
import { useEffect, useState } from "react";
|
| 4 |
+
import { motion } from "framer-motion";
|
| 5 |
+
import { User, Bell, Sparkles, Shield, Save, Loader2, CheckCircle2 } from "lucide-react";
|
| 6 |
+
import { authApi } from "@/lib/api";
|
| 7 |
+
import { useAuthStore } from "@/lib/stores/authStore";
|
| 8 |
+
|
| 9 |
+
const PERSONALITIES = [
|
| 10 |
+
"Balanced Investor",
|
| 11 |
+
"Conservative Saver",
|
| 12 |
+
"Growth Focused",
|
| 13 |
+
"Aggressive Trader",
|
| 14 |
+
"Debt Reducer",
|
| 15 |
+
];
|
| 16 |
+
|
| 17 |
export default function SettingsPage() {
|
| 18 |
+
const { user, restoreSession } = useAuthStore();
|
| 19 |
+
const [loading, setLoading] = useState(true);
|
| 20 |
+
const [saving, setSaving] = useState(false);
|
| 21 |
+
const [saved, setSaved] = useState(false);
|
| 22 |
+
const [form, setForm] = useState({
|
| 23 |
+
name: "",
|
| 24 |
+
email: "",
|
| 25 |
+
financial_personality: "Balanced Investor",
|
| 26 |
+
notifications_enabled: true,
|
| 27 |
+
ai_coaching_enabled: true,
|
| 28 |
+
fraud_alerts_enabled: true,
|
| 29 |
+
});
|
| 30 |
+
|
| 31 |
+
useEffect(() => {
|
| 32 |
+
(async () => {
|
| 33 |
+
try {
|
| 34 |
+
const me = await authApi.me();
|
| 35 |
+
setForm({
|
| 36 |
+
name: me.name,
|
| 37 |
+
email: me.email,
|
| 38 |
+
financial_personality: me.financial_personality || "Balanced Investor",
|
| 39 |
+
notifications_enabled: me.notifications_enabled ?? true,
|
| 40 |
+
ai_coaching_enabled: me.ai_coaching_enabled ?? true,
|
| 41 |
+
fraud_alerts_enabled: me.fraud_alerts_enabled ?? true,
|
| 42 |
+
});
|
| 43 |
+
} catch {
|
| 44 |
+
if (user) {
|
| 45 |
+
setForm((f) => ({
|
| 46 |
+
...f,
|
| 47 |
+
name: user.name,
|
| 48 |
+
email: user.email,
|
| 49 |
+
financial_personality: user.financial_personality || "Balanced Investor",
|
| 50 |
+
}));
|
| 51 |
+
}
|
| 52 |
+
} finally {
|
| 53 |
+
setLoading(false);
|
| 54 |
+
}
|
| 55 |
+
})();
|
| 56 |
+
}, [user]);
|
| 57 |
+
|
| 58 |
+
const handleSave = async (e: React.FormEvent) => {
|
| 59 |
+
e.preventDefault();
|
| 60 |
+
setSaving(true);
|
| 61 |
+
setSaved(false);
|
| 62 |
+
try {
|
| 63 |
+
await authApi.updateSettings({
|
| 64 |
+
name: form.name,
|
| 65 |
+
financial_personality: form.financial_personality,
|
| 66 |
+
notifications_enabled: form.notifications_enabled,
|
| 67 |
+
ai_coaching_enabled: form.ai_coaching_enabled,
|
| 68 |
+
fraud_alerts_enabled: form.fraud_alerts_enabled,
|
| 69 |
+
});
|
| 70 |
+
await restoreSession();
|
| 71 |
+
setSaved(true);
|
| 72 |
+
setTimeout(() => setSaved(false), 3000);
|
| 73 |
+
} catch {
|
| 74 |
+
/* ignore */
|
| 75 |
+
} finally {
|
| 76 |
+
setSaving(false);
|
| 77 |
+
}
|
| 78 |
+
};
|
| 79 |
+
|
| 80 |
+
const Toggle = ({
|
| 81 |
+
label,
|
| 82 |
+
desc,
|
| 83 |
+
checked,
|
| 84 |
+
onChange,
|
| 85 |
+
}: {
|
| 86 |
+
label: string;
|
| 87 |
+
desc: string;
|
| 88 |
+
checked: boolean;
|
| 89 |
+
onChange: (v: boolean) => void;
|
| 90 |
+
}) => (
|
| 91 |
+
<div className="flex items-center justify-between gap-4 py-3 border-b border-white/5 last:border-0">
|
| 92 |
+
<div>
|
| 93 |
+
<p className="text-sm font-medium text-white">{label}</p>
|
| 94 |
+
<p className="text-xs text-zinc-500 mt-0.5">{desc}</p>
|
| 95 |
+
</div>
|
| 96 |
+
<button
|
| 97 |
+
type="button"
|
| 98 |
+
role="switch"
|
| 99 |
+
aria-checked={checked}
|
| 100 |
+
onClick={() => onChange(!checked)}
|
| 101 |
+
className={`relative h-6 w-11 rounded-full transition-colors ${checked ? "bg-emerald-500" : "bg-zinc-700"}`}
|
| 102 |
+
>
|
| 103 |
+
<span
|
| 104 |
+
className={`absolute top-0.5 h-5 w-5 rounded-full bg-white transition-transform ${checked ? "left-5" : "left-0.5"}`}
|
| 105 |
+
/>
|
| 106 |
+
</button>
|
| 107 |
+
</div>
|
| 108 |
+
);
|
| 109 |
+
|
| 110 |
+
if (loading) {
|
| 111 |
+
return (
|
| 112 |
+
<div className="flex justify-center py-20 text-zinc-500 text-sm">Loading settings...</div>
|
| 113 |
+
);
|
| 114 |
+
}
|
| 115 |
+
|
| 116 |
+
return (
|
| 117 |
+
<motion.div initial={{ opacity: 0 }} animate={{ opacity: 1 }} className="flex flex-col gap-6 max-w-2xl">
|
| 118 |
+
<div>
|
| 119 |
+
<h1 className="text-3xl font-bold tracking-tight text-white">Settings</h1>
|
| 120 |
+
<p className="text-zinc-400 mt-1 text-sm">Account preferences and AI personalization</p>
|
| 121 |
+
</div>
|
| 122 |
+
|
| 123 |
+
<form onSubmit={handleSave} className="space-y-6">
|
| 124 |
+
<section className="glass-card p-6">
|
| 125 |
+
<h2 className="text-base font-semibold text-white flex items-center gap-2 mb-4">
|
| 126 |
+
<User className="h-4 w-4 text-blue-400" />
|
| 127 |
+
Profile
|
| 128 |
+
</h2>
|
| 129 |
+
<div className="space-y-4">
|
| 130 |
+
<div>
|
| 131 |
+
<label className="text-xs text-zinc-400">Display name</label>
|
| 132 |
+
<input
|
| 133 |
+
value={form.name}
|
| 134 |
+
onChange={(e) => setForm({ ...form, name: e.target.value })}
|
| 135 |
+
className="mt-1 w-full rounded-xl border border-white/10 bg-white/5 px-3 py-2 text-sm text-white focus:border-emerald-500/40 focus:outline-none"
|
| 136 |
+
/>
|
| 137 |
+
</div>
|
| 138 |
+
<div>
|
| 139 |
+
<label className="text-xs text-zinc-400">Email</label>
|
| 140 |
+
<input
|
| 141 |
+
value={form.email}
|
| 142 |
+
disabled
|
| 143 |
+
className="mt-1 w-full rounded-xl border border-white/10 bg-white/[0.02] px-3 py-2 text-sm text-zinc-500 cursor-not-allowed"
|
| 144 |
+
/>
|
| 145 |
+
</div>
|
| 146 |
+
<div>
|
| 147 |
+
<label className="text-xs text-zinc-400">Financial personality</label>
|
| 148 |
+
<select
|
| 149 |
+
value={form.financial_personality}
|
| 150 |
+
onChange={(e) => setForm({ ...form, financial_personality: e.target.value })}
|
| 151 |
+
className="mt-1 w-full rounded-xl border border-white/10 bg-white/5 px-3 py-2 text-sm text-white focus:border-emerald-500/40 focus:outline-none"
|
| 152 |
+
>
|
| 153 |
+
{PERSONALITIES.map((p) => (
|
| 154 |
+
<option key={p} value={p} className="bg-zinc-900">
|
| 155 |
+
{p}
|
| 156 |
+
</option>
|
| 157 |
+
))}
|
| 158 |
+
</select>
|
| 159 |
+
</div>
|
| 160 |
+
</div>
|
| 161 |
+
</section>
|
| 162 |
+
|
| 163 |
+
<section className="glass-card p-6">
|
| 164 |
+
<h2 className="text-base font-semibold text-white flex items-center gap-2 mb-2">
|
| 165 |
+
<Bell className="h-4 w-4 text-amber-400" />
|
| 166 |
+
Notifications & AI
|
| 167 |
+
</h2>
|
| 168 |
+
<Toggle
|
| 169 |
+
label="Push notifications"
|
| 170 |
+
desc="Fraud alerts, insights, and budget warnings"
|
| 171 |
+
checked={form.notifications_enabled}
|
| 172 |
+
onChange={(v) => setForm({ ...form, notifications_enabled: v })}
|
| 173 |
+
/>
|
| 174 |
+
<Toggle
|
| 175 |
+
label="AI coaching"
|
| 176 |
+
desc="Weekly coach reports and dashboard briefings"
|
| 177 |
+
checked={form.ai_coaching_enabled}
|
| 178 |
+
onChange={(v) => setForm({ ...form, ai_coaching_enabled: v })}
|
| 179 |
+
/>
|
| 180 |
+
<Toggle
|
| 181 |
+
label="Fraud alerts"
|
| 182 |
+
desc="Real-time suspicious activity notifications"
|
| 183 |
+
checked={form.fraud_alerts_enabled}
|
| 184 |
+
onChange={(v) => setForm({ ...form, fraud_alerts_enabled: v })}
|
| 185 |
+
/>
|
| 186 |
+
</section>
|
| 187 |
+
|
| 188 |
+
<section className="glass-card p-6 flex items-start gap-3">
|
| 189 |
+
<Shield className="h-5 w-5 text-emerald-400 flex-shrink-0 mt-0.5" />
|
| 190 |
+
<div className="text-sm text-zinc-400">
|
| 191 |
+
<p className="text-white font-medium">Security</p>
|
| 192 |
+
<p className="mt-1">Sessions use JWT encryption. Password changes require backend support in this demo.</p>
|
| 193 |
+
</div>
|
| 194 |
+
</section>
|
| 195 |
+
|
| 196 |
+
<button
|
| 197 |
+
type="submit"
|
| 198 |
+
disabled={saving}
|
| 199 |
+
className="flex items-center justify-center gap-2 rounded-xl bg-gradient-to-r from-emerald-500 to-cyan-500 px-6 py-3 text-sm font-semibold text-white disabled:opacity-50 w-full sm:w-auto"
|
| 200 |
+
>
|
| 201 |
+
{saving ? (
|
| 202 |
+
<Loader2 className="h-4 w-4 animate-spin" />
|
| 203 |
+
) : saved ? (
|
| 204 |
+
<CheckCircle2 className="h-4 w-4" />
|
| 205 |
+
) : (
|
| 206 |
+
<Save className="h-4 w-4" />
|
| 207 |
+
)}
|
| 208 |
+
{saved ? "Saved" : "Save changes"}
|
| 209 |
+
</button>
|
| 210 |
+
</form>
|
| 211 |
+
|
| 212 |
+
<div className="glass-card p-4 flex items-center gap-2 text-xs text-zinc-500">
|
| 213 |
+
<Sparkles className="h-4 w-4 text-cyan-400" />
|
| 214 |
+
User ID: {user?.user_id?.slice(0, 8) ?? "β"}β¦
|
| 215 |
+
</div>
|
| 216 |
+
</motion.div>
|
| 217 |
+
);
|
| 218 |
}
|
frontend/src/lib/api.ts
CHANGED
|
@@ -123,13 +123,92 @@ export const authApi = {
|
|
| 123 |
method: "POST",
|
| 124 |
body: JSON.stringify({ email, password, name }),
|
| 125 |
}),
|
| 126 |
-
me: () =>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
logout: () => {
|
| 128 |
tokenStore.clear();
|
| 129 |
return Promise.resolve();
|
| 130 |
},
|
| 131 |
};
|
| 132 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
// βββ Dashboard ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 134 |
export interface DashboardOverview {
|
| 135 |
total_balance: number;
|
|
|
|
| 123 |
method: "POST",
|
| 124 |
body: JSON.stringify({ email, password, name }),
|
| 125 |
}),
|
| 126 |
+
me: () =>
|
| 127 |
+
apiFetch<{
|
| 128 |
+
user_id: string;
|
| 129 |
+
email: string;
|
| 130 |
+
name: string;
|
| 131 |
+
financial_personality: string;
|
| 132 |
+
notifications_enabled?: boolean;
|
| 133 |
+
ai_coaching_enabled?: boolean;
|
| 134 |
+
fraud_alerts_enabled?: boolean;
|
| 135 |
+
}>("/api/auth/me"),
|
| 136 |
+
updateSettings: (data: {
|
| 137 |
+
name?: string;
|
| 138 |
+
financial_personality?: string;
|
| 139 |
+
notifications_enabled?: boolean;
|
| 140 |
+
ai_coaching_enabled?: boolean;
|
| 141 |
+
fraud_alerts_enabled?: boolean;
|
| 142 |
+
}) =>
|
| 143 |
+
apiFetch<{
|
| 144 |
+
user_id: string;
|
| 145 |
+
email: string;
|
| 146 |
+
name: string;
|
| 147 |
+
financial_personality: string;
|
| 148 |
+
notifications_enabled?: boolean;
|
| 149 |
+
ai_coaching_enabled?: boolean;
|
| 150 |
+
fraud_alerts_enabled?: boolean;
|
| 151 |
+
}>("/api/auth/settings", { method: "PATCH", body: JSON.stringify(data) }),
|
| 152 |
logout: () => {
|
| 153 |
tokenStore.clear();
|
| 154 |
return Promise.resolve();
|
| 155 |
},
|
| 156 |
};
|
| 157 |
|
| 158 |
+
export interface FinancialGoal {
|
| 159 |
+
id: string;
|
| 160 |
+
title: string;
|
| 161 |
+
target_amount: number;
|
| 162 |
+
current_amount: number;
|
| 163 |
+
progress_percent: number;
|
| 164 |
+
target_date: string | null;
|
| 165 |
+
days_left: number | null;
|
| 166 |
+
monthly_contribution?: number;
|
| 167 |
+
months_remaining?: number;
|
| 168 |
+
on_track: boolean;
|
| 169 |
+
}
|
| 170 |
+
|
| 171 |
+
export const goalsApi = {
|
| 172 |
+
list: (userId?: string) => {
|
| 173 |
+
const qs = userId ? `?user_id=${userId}` : "";
|
| 174 |
+
return apiFetch<{
|
| 175 |
+
goals: FinancialGoal[];
|
| 176 |
+
summary: { count: number; total_target: number; total_saved: number; overall_progress: number };
|
| 177 |
+
}>(`/api/goals${qs}`);
|
| 178 |
+
},
|
| 179 |
+
contribute: (goalId: string, amount: number, userId?: string) => {
|
| 180 |
+
const qs = userId ? `?user_id=${userId}` : "";
|
| 181 |
+
return apiFetch<{ id: string; current_amount: number; progress_percent: number; completed: boolean }>(
|
| 182 |
+
`/api/goals/${goalId}/contribute${qs}`,
|
| 183 |
+
{ method: "POST", body: JSON.stringify({ amount }) }
|
| 184 |
+
);
|
| 185 |
+
},
|
| 186 |
+
};
|
| 187 |
+
|
| 188 |
+
export const loansApi = {
|
| 189 |
+
eligibility: (data: {
|
| 190 |
+
salary: number;
|
| 191 |
+
credit_score: number;
|
| 192 |
+
existing_loans: number;
|
| 193 |
+
employment_years: number;
|
| 194 |
+
age: number;
|
| 195 |
+
loan_amount: number;
|
| 196 |
+
}, userId?: string) => {
|
| 197 |
+
const qs = userId ? `?user_id=${userId}` : "";
|
| 198 |
+
return apiFetch<{
|
| 199 |
+
approval_probability: number;
|
| 200 |
+
approval_status: string;
|
| 201 |
+
risk_level: string;
|
| 202 |
+
loan_score: number;
|
| 203 |
+
emi: number;
|
| 204 |
+
monthly_emi: number;
|
| 205 |
+
recommendations: string[];
|
| 206 |
+
issues: string[];
|
| 207 |
+
comparison: Array<{ rate: string; tenure: string; emi: number; total_amount: number; interest: number }>;
|
| 208 |
+
}>(`/api/loans/eligibility${qs}`, { method: "POST", body: JSON.stringify(data) });
|
| 209 |
+
},
|
| 210 |
+
};
|
| 211 |
+
|
| 212 |
// βββ Dashboard ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 213 |
export interface DashboardOverview {
|
| 214 |
total_balance: number;
|