import React, { useState, useEffect } from "react";
import mockProfiles from "./mockData";
import {
Home,
Search,
MessageCircle,
UserCircle,
Heart,
X,
} from "lucide-react";
import Login from "./components/Login";
function App() {
const [currentIndex, setCurrentIndex] = useState(0);
const [matches, setMatches] = useState([]);
const [showContactDialog, setShowContactDialog] = useState(false);
const [userPreferences, setUserPreferences] = useState(() => {
const saved = localStorage.getItem("userPreferences");
return saved ? JSON.parse(saved) : null;
});
const [sortedProfiles, setSortedProfiles] = useState([]);
useEffect(() => {
if (userPreferences) {
localStorage.setItem("userPreferences", JSON.stringify(userPreferences));
fetchCompatibility(userPreferences, mockProfiles);
}
}, [userPreferences]);
const fetchCompatibility = async (userProfile, candidates) => {
try {
const response = await fetch("http://127.0.0.1:8000/compute_compatibility", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
user_profile: userProfile,
candidate_profiles: candidates.map((p) => p.profile),
}),
});
const data = await response.json();
console.log("API Response:", data);
if (data.all_matches) {
const mergedProfiles = candidates.map((profile) => {
const match = data.all_matches.find((m) => m.profile === profile.profile.name);
return {
profile: {
name: profile.profile.name,
age: profile.profile.age || "N/A",
occupation: profile.profile.occupation || "Unknown",
imageUrl: profile.profile.imageUrl || "/default.jpg",
bio: profile.profile.bio || "No bio available.",
budget: profile.profile.budget || "N/A",
},
compatibility: match ? match.compatibility : 0,
matchReasons: match ? match.matchReasons : [],
};
});
console.log("Merged Profiles:", mergedProfiles);
setSortedProfiles(mergedProfiles.sort((a, b) => b.compatibility - a.compatibility));
}
} catch (error) {
console.error("Error fetching compatibility:", error);
setSortedProfiles(mockProfiles);
}
};
const handleLogout = () => {
setUserPreferences(null);
localStorage.removeItem("userPreferences");
setSortedProfiles([]);
setMatches([]);
setCurrentIndex(0);
};
const currentMatch = sortedProfiles[currentIndex];
const handleLike = () => {
setShowContactDialog(true);
};
const handleConfirmContact = () => {
setMatches([...matches, currentMatch.profile.id]);
setShowContactDialog(false);
handleNext();
};
const handleNext = () => {
setCurrentIndex((prevIndex) => prevIndex + 1);
};
if (!userPreferences?.isLoggedIn) {
return
You've viewed all available matches!
{matches.length > 0 && (You matched with {matches.length} potential roommates!
{currentMatch.profile.occupation || "No Occupation Info"}
{currentMatch.profile.bio || "No bio available."}
{currentMatch?.profile?.budget ? `$${currentMatch.profile.budget} per month` : "N/A"}
Would you like to initiate contact with{" "} {currentMatch?.profile?.name}?