improvement for the backend api

This commit is contained in:
ivannoskov
2025-11-19 12:56:04 +03:00
parent b0a9934220
commit d4672ea32b
40 changed files with 2383 additions and 3313 deletions

View File

@@ -45,7 +45,7 @@ import {
Users,
TrendingUp,
} from "lucide-react";
import type { CreativeDetail } from "@/lib/types/api";
import type { Creative } from "@/lib/types/api";
interface PageProps {
params: Promise<{ id: string }>;
@@ -54,7 +54,7 @@ interface PageProps {
export default function CreativeDetailPage({ params }: PageProps) {
const resolvedParams = use(params);
const router = useRouter();
const [creative, setCreative] = useState<CreativeDetail | null>(null);
const [creative, setCreative] = useState<Creative | null>(null);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
@@ -79,9 +79,12 @@ export default function CreativeDetailPage({ params }: PageProps) {
try {
await creativesApi.update(creative.id, {
is_archived: !creative.is_archived,
status: creative.status === "active" ? "archived" : "active",
});
setCreative({
...creative,
status: creative.status === "active" ? "archived" : "active",
});
setCreative({ ...creative, is_archived: !creative.is_archived });
} catch (err: any) {
alert(err?.error?.message || "Ошибка обновления креатива");
}
@@ -162,16 +165,20 @@ export default function CreativeDetailPage({ params }: PageProps) {
<h1 className="text-3xl font-bold tracking-tight">
{creative.name}
</h1>
{creative.is_archived && <Badge variant="secondary">Архив</Badge>}
{creative.status === "archived" && (
<Badge variant="secondary">Архив</Badge>
)}
</div>
<div className="text-sm text-muted-foreground">
Целевой канал: {creative.target_channel.title}
Целевой канал: {creative.target_channel_title}
</div>
</div>
<div className="flex gap-2">
<Button variant="outline" onClick={handleArchive}>
<Archive className="mr-2 h-4 w-4" />
{creative.is_archived ? "Разархивировать" : "Архивировать"}
{creative.status === "archived"
? "Разархивировать"
: "Архивировать"}
</Button>
<Button variant="destructive" onClick={handleDelete}>
<Trash2 className="mr-2 h-4 w-4" />
@@ -190,10 +197,10 @@ export default function CreativeDetailPage({ params }: PageProps) {
</CardHeader>
<CardContent>
<div className="text-2xl font-bold">
{formatNumber(creative.total_purchases)}
{formatNumber(creative.placements_count)}
</div>
<p className="text-xs text-muted-foreground mt-1">
С этим креативом
Всего размещений
</p>
</CardContent>
</Card>
@@ -201,29 +208,16 @@ export default function CreativeDetailPage({ params }: PageProps) {
<Card>
<CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
<CardTitle className="text-sm font-medium">
Подписчиков привлечено
Целевой канал
</CardTitle>
<Users className="h-4 w-4 text-muted-foreground" />
</CardHeader>
<CardContent>
<div className="text-2xl font-bold">
{formatNumber(creative.total_subscriptions)}
</div>
<p className="text-xs text-muted-foreground mt-1">Всего</p>
</CardContent>
</Card>
<Card>
<CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
<CardTitle className="text-sm font-medium">Средний CPF</CardTitle>
<TrendingUp className="h-4 w-4 text-muted-foreground" />
</CardHeader>
<CardContent>
<div className="text-2xl font-bold">
{formatMetric(creative.avg_cpf)}
<div className="text-lg font-medium">
{creative.target_channel_title}
</div>
<p className="text-xs text-muted-foreground mt-1">
По всем закупам
Канал для рекламы
</p>
</CardContent>
</Card>
@@ -261,58 +255,29 @@ export default function CreativeDetailPage({ params }: PageProps) {
<Card>
<CardHeader>
<CardTitle>Закупы с этим креативом</CardTitle>
<CardDescription>История использования креатива</CardDescription>
<CardTitle>Информация о размещениях</CardTitle>
<CardDescription>Статистика использования креатива</CardDescription>
</CardHeader>
<CardContent>
{creative.purchases.length === 0 ? (
<p className="text-center text-muted-foreground py-8">
Закупов с этим креативом пока нет
</p>
) : (
<Table>
<TableHeader>
<TableRow>
<TableHead>Внешний канал</TableHead>
<TableHead>Дата</TableHead>
<TableHead className="text-right">Стоимость</TableHead>
<TableHead className="text-right">Подписчики</TableHead>
<TableHead className="text-right">CPF</TableHead>
<TableHead></TableHead>
</TableRow>
</TableHeader>
<TableBody>
{creative.purchases.map((purchase) => (
<TableRow key={purchase.id}>
<TableCell className="font-medium">
{purchase.external_channel.title}
</TableCell>
<TableCell>
{formatDate(
purchase.actual_date || purchase.scheduled_date
)}
</TableCell>
<TableCell className="text-right">
{formatCurrency(purchase.cost)}
</TableCell>
<TableCell className="text-right">
{formatNumber(purchase.subscriptions_count)}
</TableCell>
<TableCell className="text-right">
{formatMetric(purchase.cpf)}
</TableCell>
<TableCell className="text-right">
<Button asChild variant="ghost" size="sm">
<Link href={`/purchases/${purchase.id}`}>
Открыть
</Link>
</Button>
</TableCell>
</TableRow>
))}
</TableBody>
</Table>
)}
<div className="space-y-4">
<div>
<h4 className="text-sm font-medium mb-2">Всего размещений:</h4>
<p className="text-2xl font-bold">
{creative.placements_count}
</p>
</div>
<div>
<h4 className="text-sm font-medium mb-2">Создан:</h4>
<p className="text-sm">{formatDate(creative.created_at)}</p>
</div>
<div>
<Button asChild variant="outline" className="w-full">
<Link href={`/purchases?creative_id=${creative.id}`}>
Посмотреть все размещения
</Link>
</Button>
</div>
</div>
</CardContent>
</Card>
</div>

View File

@@ -48,8 +48,8 @@ export default function CreateCreativePage() {
const loadChannels = async () => {
try {
const response = await channelsApi.list({ is_active: true });
setChannels(response.data);
const response = await channelsApi.list();
setChannels(response.target_channels.filter((c) => c.is_active));
} catch (err) {
console.error("Error loading channels:", err);
}

View File

@@ -18,7 +18,12 @@ import { Button } from "@/components/ui/button";
import { Badge } from "@/components/ui/badge";
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs";
import { creativesApi } from "@/lib/api";
import { formatNumber, formatMetric, truncate } from "@/lib/utils/format";
import {
formatNumber,
formatMetric,
truncate,
formatDate,
} from "@/lib/utils/format";
import {
Folder,
Loader2,
@@ -48,7 +53,7 @@ export default function CreativesPage() {
try {
setLoading(true);
const response = await creativesApi.list();
setCreatives(response.data);
setCreatives(response.creatives);
} catch (err: any) {
setError(err?.error?.message || "Ошибка загрузки креативов");
} finally {
@@ -56,9 +61,13 @@ export default function CreativesPage() {
}
};
const handleArchive = async (id: string, isArchived: boolean) => {
const handleArchive = async (
id: string,
currentStatus: "active" | "archived"
) => {
try {
await creativesApi.update(id, { is_archived: !isArchived });
const newStatus = currentStatus === "active" ? "archived" : "active";
await creativesApi.update(id, { status: newStatus });
loadCreatives();
} catch (err: any) {
alert(err?.error?.message || "Ошибка обновления креатива");
@@ -77,8 +86,8 @@ export default function CreativesPage() {
};
const filteredCreatives = creatives.filter((creative) => {
if (activeTab === "active") return !creative.is_archived;
if (activeTab === "archived") return creative.is_archived;
if (activeTab === "active") return creative.status === "active";
if (activeTab === "archived") return creative.status === "archived";
return true;
});
@@ -117,10 +126,10 @@ export default function CreativesPage() {
>
<TabsList>
<TabsTrigger value="active">
Активные ({creatives.filter((c) => !c.is_archived).length})
Активные ({creatives.filter((c) => c.status === "active").length})
</TabsTrigger>
<TabsTrigger value="archived">
Архив ({creatives.filter((c) => c.is_archived).length})
Архив ({creatives.filter((c) => c.status === "archived").length})
</TabsTrigger>
<TabsTrigger value="all">Все ({creatives.length})</TabsTrigger>
</TabsList>
@@ -166,12 +175,12 @@ export default function CreativesPage() {
<CardTitle className="truncate text-base">
{creative.name}
</CardTitle>
{creative.is_archived && (
{creative.status === "archived" && (
<Badge variant="secondary">Архив</Badge>
)}
</div>
<CardDescription className="text-xs">
{creative.target_channel.title}
{creative.target_channel_title}
</CardDescription>
</div>
<Folder className="h-5 w-5 text-muted-foreground shrink-0" />
@@ -184,29 +193,21 @@ export default function CreativesPage() {
</p>
</div>
<div className="grid grid-cols-3 gap-2 text-center">
<div className="grid grid-cols-2 gap-2 text-center">
<div className="rounded-lg border bg-muted/50 p-2">
<div className="text-sm font-medium">
{formatNumber(creative.total_purchases)}
{formatNumber(creative.placements_count)}
</div>
<div className="text-xs text-muted-foreground">
Закупов
Размещений
</div>
</div>
<div className="rounded-lg border bg-muted/50 p-2">
<div className="text-sm font-medium">
{formatNumber(creative.total_subscriptions)}
<div className="text-xs font-medium">
{formatDate(creative.created_at)}
</div>
<div className="text-xs text-muted-foreground">
Подписчиков
</div>
</div>
<div className="rounded-lg border bg-muted/50 p-2">
<div className="text-sm font-medium">
{formatMetric(creative.avg_cpf)}
</div>
<div className="text-xs text-muted-foreground">
CPF
Создан
</div>
</div>
</div>
@@ -227,7 +228,7 @@ export default function CreativesPage() {
variant="ghost"
size="sm"
onClick={() =>
handleArchive(creative.id, creative.is_archived)
handleArchive(creative.id, creative.status)
}
>
<Archive className="h-4 w-4" />