diff --git a/src/components/CrazyRecipeForm.tsx b/src/components/CrazyRecipeForm.tsx index 1629cea..c90b3e7 100644 --- a/src/components/CrazyRecipeForm.tsx +++ b/src/components/CrazyRecipeForm.tsx @@ -162,7 +162,6 @@ const CrazyRecipeForm = ({ onSuccess, onCancel, editMode = false, initialData }: result = await updateRecipe(initialData.id, recipeData, formData.image || undefined); } else { // For create, author info is required - // @ts-ignore - Temporary fix for type mismatch if types aren't fully updated yet result = await createRecipe(recipeData, formData.image || undefined); } diff --git a/src/hooks/useAdmin.tsx b/src/hooks/useAdmin.tsx new file mode 100644 index 0000000..3846f6f --- /dev/null +++ b/src/hooks/useAdmin.tsx @@ -0,0 +1,52 @@ +import { useEffect, useState } from 'react'; +import { useAuth } from '@/hooks/useAuth'; +import { supabase } from '@/integrations/supabase/client'; + +export const useAdmin = () => { + const { user } = useAuth(); + const [isAdmin, setIsAdmin] = useState(false); + const [loading, setLoading] = useState(true); + + useEffect(() => { + const checkAdminStatus = async () => { + if (!user) { + setIsAdmin(false); + setLoading(false); + return; + } + + try { + // Try to use the RPC function first + const { data, error } = await supabase.rpc('is_admin'); + + if (error) { + // Fallback to checking profiles table directly if RPC fails + console.warn('RPC is_admin failed, checking profiles table', error); + const { data: profile, error: profileError } = await supabase + .from('profiles') + .select('is_admin') + .eq('id', user.id) + .single(); + + if (profileError) { + console.error('Error fetching profile for admin check:', profileError); + setIsAdmin(false); + } else { + setIsAdmin(!!profile?.is_admin); + } + } else { + setIsAdmin(!!data); + } + } catch (error) { + console.error('Error checking admin status:', error); + setIsAdmin(false); + } finally { + setLoading(false); + } + }; + + checkAdminStatus(); + }, [user]); + + return { isAdmin, loading }; +}; diff --git a/src/hooks/useRecipeService.ts b/src/hooks/useRecipeService.ts index 860ffc5..295a1e7 100644 --- a/src/hooks/useRecipeService.ts +++ b/src/hooks/useRecipeService.ts @@ -2,7 +2,7 @@ import { useState, useEffect, useCallback } from 'react'; import { useToast } from '@/hooks/use-toast'; import { useAuth } from '@/hooks/useAuth'; import * as recipeService from '@/services/recipeService'; -import type { CrazyRecipe, RecipeReview } from '@/services/recipeService'; +import type { CrazyRecipe, RecipeReview, CrazyRecipeInsert, CrazyRecipeUpdate } from '@/services/recipeService'; export const useRecipeService = () => { const { user } = useAuth(); @@ -188,7 +188,7 @@ export const useRecipeService = () => { // ======================================== const createRecipe = useCallback(async ( - recipeData: any, + recipeData: CrazyRecipeInsert, imageFile?: File ): Promise => { if (!user) { @@ -244,7 +244,7 @@ export const useRecipeService = () => { const updateRecipe = useCallback(async ( id: string, - recipeData: any, + recipeData: CrazyRecipeUpdate, imageFile?: File ): Promise => { if (!user) {