Skip to content

Latest commit

 

History

History
128 lines (112 loc) · 3.8 KB

File metadata and controls

128 lines (112 loc) · 3.8 KB

Dashboard Migration to Firebase

Overview

Successfully migrated Dashboard.tsx from REST API (localhost:5000) to Firebase backend services.

Changes Made

1. Updated Imports

// Added Firebase imports
import { useFirebaseAuth } from "@/integrations/firebase/useFirebaseAuth";
import { getUserProjects, getUser } from "@/integrations/firebase/firebaseService";

// Removed REST API constant
// const API_BASE = "http://localhost:5000";

2. Component Integration

  • Added useFirebaseAuth() hook to get current authenticated user
  • Replaced REST API authentication (buildwave_token) with Firebase Authentication
  • Updated user profile fetch to use getUser(userId) from firebaseService
  • Updated projects fetch to use getUserProjects(userId) from firebaseService

3. Updated fetchDashboard Function

Before:

const token = localStorage.getItem("buildwave_token");
const res = await fetch(`${API_BASE}/api/projects`, {
  headers: { Authorization: `Bearer ${token}` }
});

After:

if (!authUser) throw new Error("User not authenticated");
const userProfile = await getUser(authUser.uid);
const userProjects = await getUserProjects(authUser.uid);

4. Updated Logout Handler

Before:

const handleLogout = () => {
  localStorage.removeItem("buildwave_token");
  navigate("/");
};

After:

const handleLogout = async () => {
  try {
    await logout(); // Firebase logout
    localStorage.removeItem("buildwave_uid");
    localStorage.removeItem("buildwave_user");
    localStorage.removeItem("buildwave_email");
    navigate("/");
  } catch (err) {
    console.error("Logout error:", err);
  }
};

5. Updated ProjectCard Component

  • Removed REST API-specific fields (level, discipline, lastUpdate)
  • Updated to use Firebase project structure (category, description, createdAt)
  • Added formatDate() function to properly format Firebase Timestamp objects
  • Improved date display logic with relative time format (today, yesterday, N days ago, etc.)
  • Added description field to project card display

6. Added Authentication Check

useEffect(() => {
  if (!authUser && !loading) {
    navigate("/");
  }
}, [authUser, navigate, loading]);

Data Structure Mapping

Firebase Project Model

{
  id: string;              // Document ID
  title: string;
  description: string;
  status: string;          // "In Progress", "Under Review", "Completed", "On Hold", "Pending Review"
  userId: string;
  category: string;        // Instead of: level, discipline
  progress?: number;       // 0-100
  createdAt: Timestamp;    // Firebase Timestamp
  updatedAt: Timestamp;
  // ... other fields
}

Firebase User Model

{
  id: string;              // Document ID (UID)
  name: string;
  email: string;
  school?: string;
  // ... other fields
}

Testing Checklist

  • User authenticates via AuthModal
  • User redirected to Dashboard after signup
  • Dashboard loads user profile correctly
  • Dashboard loads user's projects correctly
  • Empty state displays when no projects exist
  • Project cards display correctly with all fields
  • Can click project to view details
  • Logout button works and clears localStorage
  • Can log back in and see same projects

Next Steps

Other components still using REST API:

  1. ProjectRequestModal.tsx - Use createProject() from firebaseService
  2. TrackProjectModal.tsx - Use getProject() from firebaseService
  3. Admin.tsx - Use admin functions from firebaseService (already done in AdminUsers.tsx)
  4. AdminLogin.tsx - Migrate to useFirebaseAuth
  5. AdminProjectDetail.tsx - Use project functions from firebaseService
  6. TrackProject.tsx - Use getProject() from firebaseService
  7. useAuth.tsx - Replace with useFirebaseAuth