Initial commit: CMS completo com gerenciamento de leads e personalização de tema
This commit is contained in:
40
frontend/src/hooks/usePageContent.ts
Normal file
40
frontend/src/hooks/usePageContent.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
import { useState, useEffect } from 'react';
|
||||
|
||||
interface PageContentData {
|
||||
id: string;
|
||||
slug: string;
|
||||
content: any;
|
||||
updatedAt: string;
|
||||
}
|
||||
|
||||
export function usePageContent(slug: string) {
|
||||
const [content, setContent] = useState<any>(null);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
const fetchContent = async () => {
|
||||
try {
|
||||
const response = await fetch(`/api/pages/${slug}`);
|
||||
|
||||
if (response.ok) {
|
||||
const data: PageContentData = await response.json();
|
||||
setContent(data.content);
|
||||
} else if (response.status === 404) {
|
||||
// Página ainda não foi configurada no admin
|
||||
setContent(null);
|
||||
} else {
|
||||
throw new Error('Erro ao carregar conteúdo');
|
||||
}
|
||||
} catch (err) {
|
||||
setError(err instanceof Error ? err.message : 'Erro desconhecido');
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
fetchContent();
|
||||
}, [slug]);
|
||||
|
||||
return { content, loading, error };
|
||||
}
|
||||
36
frontend/src/hooks/useToast.ts
Normal file
36
frontend/src/hooks/useToast.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
"use client";
|
||||
|
||||
import { useState, useCallback } from 'react';
|
||||
|
||||
interface Toast {
|
||||
id: number;
|
||||
message: string;
|
||||
type: 'success' | 'error' | 'warning' | 'info';
|
||||
}
|
||||
|
||||
export function useToast() {
|
||||
const [toasts, setToasts] = useState<Toast[]>([]);
|
||||
|
||||
const showToast = useCallback((message: string, type: Toast['type'] = 'info') => {
|
||||
const id = Date.now();
|
||||
setToasts((prev) => [...prev, { id, message, type }]);
|
||||
}, []);
|
||||
|
||||
const removeToast = useCallback((id: number) => {
|
||||
setToasts((prev) => prev.filter((toast) => toast.id !== id));
|
||||
}, []);
|
||||
|
||||
const success = useCallback((message: string) => showToast(message, 'success'), [showToast]);
|
||||
const error = useCallback((message: string) => showToast(message, 'error'), [showToast]);
|
||||
const warning = useCallback((message: string) => showToast(message, 'warning'), [showToast]);
|
||||
const info = useCallback((message: string) => showToast(message, 'info'), [showToast]);
|
||||
|
||||
return {
|
||||
toasts,
|
||||
removeToast,
|
||||
success,
|
||||
error,
|
||||
warning,
|
||||
info,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user