mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-25 18:11:13 -05:00
Fix React Hooks Order Violation
This commit is contained in:
@@ -48,44 +48,7 @@ export default function AdminBlog() {
|
|||||||
const [featuredImageId, setFeaturedImageId] = useState('');
|
const [featuredImageId, setFeaturedImageId] = useState('');
|
||||||
const [featuredImageUrl, setFeaturedImageUrl] = useState('');
|
const [featuredImageUrl, setFeaturedImageUrl] = useState('');
|
||||||
|
|
||||||
const { data: posts, isLoading } = useQuery({
|
// All mutations must be called before conditional returns
|
||||||
queryKey: ['admin-blog-posts'],
|
|
||||||
queryFn: async () => {
|
|
||||||
const { data, error } = await supabase
|
|
||||||
.from('blog_posts')
|
|
||||||
.select('*')
|
|
||||||
.order('created_at', { ascending: false });
|
|
||||||
if (error) throw error;
|
|
||||||
return data as BlogPost[];
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
// Show loading state while checking permissions
|
|
||||||
if (loading) {
|
|
||||||
return (
|
|
||||||
<AdminLayout>
|
|
||||||
<div className="flex items-center justify-center min-h-[60vh]">
|
|
||||||
<div className="text-center">
|
|
||||||
<div className="animate-spin rounded-full h-12 w-12 border-b-2 border-primary mx-auto mb-4"></div>
|
|
||||||
<p className="text-muted-foreground">Loading...</p>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</AdminLayout>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Redirect if not admin or superuser
|
|
||||||
useEffect(() => {
|
|
||||||
if (!loading && !isAdmin()) {
|
|
||||||
navigate('/');
|
|
||||||
}
|
|
||||||
}, [loading, isAdmin, navigate]);
|
|
||||||
|
|
||||||
// Don't render if not admin
|
|
||||||
if (!loading && !isAdmin()) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
const saveMutation = useMutation({
|
const saveMutation = useMutation({
|
||||||
mutationFn: async ({ isDraft }: { isDraft: boolean }) => {
|
mutationFn: async ({ isDraft }: { isDraft: boolean }) => {
|
||||||
const postData = {
|
const postData = {
|
||||||
@@ -140,6 +103,44 @@ export default function AdminBlog() {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const { data: posts, isLoading } = useQuery({
|
||||||
|
queryKey: ['admin-blog-posts'],
|
||||||
|
queryFn: async () => {
|
||||||
|
const { data, error } = await supabase
|
||||||
|
.from('blog_posts')
|
||||||
|
.select('*')
|
||||||
|
.order('created_at', { ascending: false });
|
||||||
|
if (error) throw error;
|
||||||
|
return data as BlogPost[];
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
// useEffect must be called before conditional returns
|
||||||
|
useEffect(() => {
|
||||||
|
if (!loading && !isAdmin()) {
|
||||||
|
navigate('/');
|
||||||
|
}
|
||||||
|
}, [loading, isAdmin, navigate]);
|
||||||
|
|
||||||
|
// Show loading state while checking permissions
|
||||||
|
if (loading) {
|
||||||
|
return (
|
||||||
|
<AdminLayout>
|
||||||
|
<div className="flex items-center justify-center min-h-[60vh]">
|
||||||
|
<div className="text-center">
|
||||||
|
<div className="animate-spin rounded-full h-12 w-12 border-b-2 border-primary mx-auto mb-4"></div>
|
||||||
|
<p className="text-muted-foreground">Loading...</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</AdminLayout>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Don't render if not admin
|
||||||
|
if (!loading && !isAdmin()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
const resetForm = () => {
|
const resetForm = () => {
|
||||||
setTitle('');
|
setTitle('');
|
||||||
setSlug('');
|
setSlug('');
|
||||||
|
|||||||
Reference in New Issue
Block a user