Branchwell

Become a Pro / website · becomeapro.ai / fix-dashboard-page.md

Raw 3.7 KB on b7854f3692d64213a2fce644d0b5101d3e345b03

Show the source

How to Fix the Dashboard Page Count Issues

After analyzing the dashboard code and database schema, here are the detailed steps to fix the instance count issues:

1. Fix the Dashboard Code

Open the file src/app/admin/dashboard/page.tsx and locate the category count code section (around line 290). You need to make the following changes:

Option A: If you've renamed the column to instance_count

// Get category counts from the categories table
const { data: categoryData, error: categoryError } = await supabase
  .from('categories')
  .select('id, name, instance_count')  // Changed from tool_count to instance_count
  .order('name');

if (!categoryError && categoryData) {
  // Initialize count for each category using instance_count
  categoryData.forEach(cat => {
    categoryCounts[cat.name] = cat.instance_count || 0;
  });
  
  // REMOVE the additional counting code to avoid double counting
  // The following code should be REMOVED:
  // for (const cat of categoryData) {
  //   // Count blog posts with this category
  //   const { count: blogCount, error: blogCountError } = await supabase
  //     .from('blog_posts')
  //     .select('*', { count: 'exact', head: true })
  //     .eq('category_id', cat.id);
  //   
  //   if (!blogCountError) {
  //     categoryCounts[cat.name] += blogCount || 0;
  //   }
  //   
  //   // Count news items with this category
  //   const { count: newsCount, error: newsCountError } = await supabase
  //     .from('news')
  //     .select('*', { count: 'exact', head: true })
  //     .eq('category', cat.name);
  //   
  //   if (!newsCountError) {
  //     categoryCounts[cat.name] += newsCount || 0;
  //   }
  // }
}

Option B: If you haven't renamed the column yet

// Get category counts from the categories table
const { data: categoryData, error: categoryError } = await supabase
  .from('categories')
  .select('id, name, tool_count')
  .order('name');

if (!categoryError && categoryData) {
  // Initialize count for each category using tool_count
  categoryData.forEach(cat => {
    categoryCounts[cat.name] = cat.tool_count || 0;
  });
  
  // REMOVE the additional counting code to avoid double counting
  // (remove the for loop shown above)
}

2. Update the Database Column Values

Run the appropriate SQL script to update the category count values in the database:

If you've renamed the column to instance_count:

-- Update instance_count values
UPDATE categories c
SET instance_count = (
    SELECT COUNT(*) FROM tools WHERE category_id = c.id
) + (
    SELECT COUNT(*) FROM news WHERE category_id = c.id
);

If you're still using tool_count:

-- Update tool_count values
UPDATE categories c
SET tool_count = (
    SELECT COUNT(*) FROM tools WHERE category_id = c.id
) + (
    SELECT COUNT(*) FROM news WHERE category_id = c.id
);

3. Maintain Counts Going Forward

To ensure counts stay accurate when tools and news are added/removed, make sure:

  1. When adding a new tool:
-- Increment the count for the category
UPDATE categories 
SET instance_count = instance_count + 1
WHERE id = [category_id_of_new_tool];
  1. When removing a tool:
-- Decrement the count for the category
UPDATE categories 
SET instance_count = instance_count - 1
WHERE id = [category_id_of_removed_tool];
  1. Apply similar logic for news items.

4. Testing

After making these changes:

  1. Verify the dashboard displays the correct counts
  2. Add a new tool or news item and verify the count increments properly
  3. Remove a tool or news item and verify the count decrements properly

This approach ensures the count is maintained in one place (the database) and avoids the double-counting issue in your dashboard code.