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