feat: fix api requests
This commit is contained in:
@@ -66,14 +66,34 @@ export function AddChannelDialog({
|
||||
try {
|
||||
setLoading(true);
|
||||
|
||||
// Step 1: Verify/add channel via channels API
|
||||
await channelsApi.list({ username });
|
||||
const username = parseChannelInput(inputValue);
|
||||
|
||||
// Step 2: Create empty placement
|
||||
if (!username) {
|
||||
setError("Не удалось распознать username из ссылки");
|
||||
return;
|
||||
}
|
||||
|
||||
if (existingUsernames.has(username.toLowerCase())) {
|
||||
setError("Этот канал уже добавлен в проект");
|
||||
return;
|
||||
}
|
||||
|
||||
// Step 1: Find channel by username to get channel_id
|
||||
const channelsResponse = await channelsApi.list({ username });
|
||||
const channel = channelsResponse.items.find(
|
||||
(c) => c.username?.toLowerCase() === username.toLowerCase()
|
||||
);
|
||||
|
||||
if (!channel) {
|
||||
setError("Канал не найден в каталоге");
|
||||
return;
|
||||
}
|
||||
|
||||
// Step 2: Create empty placement with channel_id
|
||||
await placementsApi.createBulk(workspaceId, projectId, {
|
||||
channels: [
|
||||
{
|
||||
username,
|
||||
channel_id: channel.id,
|
||||
status: "Без статуса",
|
||||
},
|
||||
],
|
||||
|
||||
@@ -24,12 +24,12 @@ import {
|
||||
} from "@/components/ui/select";
|
||||
import { cn } from "@/lib/utils";
|
||||
import { parseChannelInput } from "@/lib/utils/channel-parser";
|
||||
import { placementsApi, creativesApi } from "@/lib/api";
|
||||
import type { Placement, Creative } from "@/lib/types/api";
|
||||
import { placementsApi, creativesApi, channelsApi } from "@/lib/api";
|
||||
import type { Placement, Creative, Channel } from "@/lib/types/api";
|
||||
|
||||
interface ChannelInput {
|
||||
username: string;
|
||||
isNew: boolean;
|
||||
channelId: string;
|
||||
}
|
||||
|
||||
interface CreatePlacementsDialogProps {
|
||||
@@ -108,7 +108,7 @@ export function CreatePlacementsDialog({
|
||||
}
|
||||
};
|
||||
|
||||
const addChannel = () => {
|
||||
const addChannel = async () => {
|
||||
const username = parseChannelInput(newChannelInput);
|
||||
if (!username) {
|
||||
setError("Не удалось распознать username");
|
||||
@@ -125,9 +125,25 @@ export function CreatePlacementsDialog({
|
||||
return;
|
||||
}
|
||||
|
||||
setSelectedChannels([...selectedChannels, { username, isNew: true }]);
|
||||
setNewChannelInput("");
|
||||
setError(null);
|
||||
// Look up channel by username to get channel_id
|
||||
try {
|
||||
const channelsResponse = await channelsApi.list({ username });
|
||||
const channel = channelsResponse.items.find(
|
||||
(c) => c.username?.toLowerCase() === lowerUsername
|
||||
);
|
||||
|
||||
if (!channel) {
|
||||
setError("Канал не найден в каталоге");
|
||||
return;
|
||||
}
|
||||
|
||||
setSelectedChannels([...selectedChannels, { username, channelId: channel.id }]);
|
||||
setNewChannelInput("");
|
||||
setError(null);
|
||||
} catch (err) {
|
||||
console.error("Failed to look up channel:", err);
|
||||
setError("Не удалось найти канал");
|
||||
}
|
||||
};
|
||||
|
||||
const removeChannel = (username: string) => {
|
||||
@@ -158,7 +174,7 @@ export function CreatePlacementsDialog({
|
||||
await placementsApi.createBulk(workspaceId, projectId, {
|
||||
creative_id: creativeId || undefined,
|
||||
channels: selectedChannels.map((c) => ({
|
||||
username: c.username,
|
||||
channel_id: c.channelId,
|
||||
status: DEFAULT_STATUS,
|
||||
})),
|
||||
details: Object.keys(details).length > 0 ? details : undefined,
|
||||
|
||||
Reference in New Issue
Block a user