Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/components/CrazyRecipeForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
52 changes: 52 additions & 0 deletions src/hooks/useAdmin.tsx
Original file line number Diff line number Diff line change
@@ -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 };
};
6 changes: 3 additions & 3 deletions src/hooks/useRecipeService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -188,7 +188,7 @@ export const useRecipeService = () => {
// ========================================

const createRecipe = useCallback(async (
recipeData: any,
recipeData: CrazyRecipeInsert,
imageFile?: File
): Promise<CrazyRecipe | null> => {
if (!user) {
Expand Down Expand Up @@ -244,7 +244,7 @@ export const useRecipeService = () => {

const updateRecipe = useCallback(async (
id: string,
recipeData: any,
recipeData: CrazyRecipeUpdate,
imageFile?: File
): Promise<CrazyRecipe | null> => {
if (!user) {
Expand Down