Untitled

mail@pastecode.io avatar
unknown
plain_text
5 months ago
1.4 kB
2
Indexable
import { supabase } from "$lib/supabaseClient";

export async function load({ url }) {
  const sortBy = url.searchParams.get('sortBy') || 'Best';
  
  // First, fetch the lawyer stats
  let statsQuery = supabase
    .from('lawyer_stats')
    .select('*')
    .order(sortBy === 'Best' ? 'bayesian_avg' : 'review_count', { ascending: false })
    .limit(10);

  const { data: statsData, error: statsError } = await statsQuery;

  if (statsError) {
    console.error("Error fetching lawyer stats:", statsError);
    return { lawyers: [] };
  }

  // Get the IDs of the lawyers we need to fetch
  const lawyerIds = statsData.map(stat => stat.lawyer_id);

  // Now fetch the corresponding lawyer data
  let lawyersQuery = supabase
    .from('lawyers')
    .select('id, jmeno, slug, zamereni, firma_www')
    .in('id', lawyerIds);

  const { data: lawyersData, error: lawyersError } = await lawyersQuery;

  if (lawyersError) {
    console.error("Error fetching lawyer data:", lawyersError);
    return { lawyers: [] };
  }

  // Combine the data
  const lawyers = lawyerIds.map(id => {
    const stats = statsData.find(stat => stat.lawyer_id === id);
    const lawyer = lawyersData.find(l => l.id === id);
    return {
      ...lawyer,
      num_reviews: stats.review_count,
      rating: stats.avg_rating,
      bayesian_avg: stats.bayesian_avg
    };
  });

  return {
    lawyers,
  };
}
Leave a Comment