mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-25 01:11:13 -05:00
Refactor: Implement complete plan
This commit is contained in:
52
src/hooks/useTechnicalSpecifications.ts
Normal file
52
src/hooks/useTechnicalSpecifications.ts
Normal file
@@ -0,0 +1,52 @@
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
import { supabase } from '@/integrations/supabase/client';
|
||||
|
||||
export interface TechnicalSpecification {
|
||||
id: string;
|
||||
entity_type: 'ride' | 'ride_model';
|
||||
entity_id: string;
|
||||
spec_name: string;
|
||||
spec_value: string;
|
||||
spec_unit?: string | null;
|
||||
category?: string | null;
|
||||
display_order: number;
|
||||
created_at: string;
|
||||
}
|
||||
|
||||
export function useTechnicalSpecifications(
|
||||
entityType: 'ride' | 'ride_model',
|
||||
entityId: string | undefined
|
||||
) {
|
||||
return useQuery({
|
||||
queryKey: ['technical-specifications', entityType, entityId],
|
||||
queryFn: async () => {
|
||||
if (!entityId) return [];
|
||||
|
||||
const tableName = entityType === 'ride'
|
||||
? 'ride_technical_specifications'
|
||||
: 'ride_model_technical_specifications';
|
||||
const idColumn = entityType === 'ride' ? 'ride_id' : 'ride_model_id';
|
||||
|
||||
const { data, error } = await (supabase as any)
|
||||
.from(tableName)
|
||||
.select('*')
|
||||
.eq(idColumn, entityId)
|
||||
.order('display_order');
|
||||
|
||||
if (error) throw error;
|
||||
|
||||
return (data || []).map((spec: any) => ({
|
||||
id: spec.id,
|
||||
entity_type: entityType,
|
||||
entity_id: entityId,
|
||||
spec_name: spec.spec_name,
|
||||
spec_value: spec.spec_value,
|
||||
spec_unit: spec.spec_unit || null,
|
||||
category: spec.category || null,
|
||||
display_order: spec.display_order,
|
||||
created_at: spec.created_at,
|
||||
})) as TechnicalSpecification[];
|
||||
},
|
||||
enabled: !!entityId
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user