feat: placements update
This commit is contained in:
@@ -78,14 +78,12 @@ export function AddChannelDialog({
|
||||
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()
|
||||
);
|
||||
// Step 1: Create channel via API to get channel_id
|
||||
const createResponse = await channelsApi.create([{ username }]);
|
||||
const channelId = createResponse.results[0]?.channel.id;
|
||||
|
||||
if (!channel) {
|
||||
setError("Канал не найден в каталоге");
|
||||
if (!channelId) {
|
||||
setError("Не удалось получить ID канала");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -93,7 +91,7 @@ export function AddChannelDialog({
|
||||
await placementsApi.createBulk(workspaceId, projectId, {
|
||||
channels: [
|
||||
{
|
||||
channel_id: channel.id,
|
||||
channel_id: channelId,
|
||||
status: "Без статуса",
|
||||
},
|
||||
],
|
||||
|
||||
@@ -78,6 +78,13 @@ export function CreatePlacementsDialog({
|
||||
.filter(Boolean)
|
||||
);
|
||||
|
||||
const getExistingChannelIdByUsername = (usernameLower: string): string | null => {
|
||||
const matched = existingPlacements.find(
|
||||
(p) => p.channel.username?.toLowerCase() === usernameLower
|
||||
);
|
||||
return matched?.channel.id ?? null;
|
||||
};
|
||||
|
||||
const handleOpenChange = (isOpen: boolean) => {
|
||||
if (!isOpen) {
|
||||
resetForm();
|
||||
@@ -116,33 +123,43 @@ export function CreatePlacementsDialog({
|
||||
}
|
||||
|
||||
const lowerUsername = username.toLowerCase();
|
||||
if (existingUsernames.has(lowerUsername)) {
|
||||
setError("Этот канал уже добавлен в проект");
|
||||
return;
|
||||
}
|
||||
if (selectedChannels.some((c) => c.username.toLowerCase() === lowerUsername)) {
|
||||
setError("Канал уже добавлен в список");
|
||||
return;
|
||||
}
|
||||
|
||||
// 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 already has placements in this project, reuse its channel_id.
|
||||
// Channels are global, so we don't need to create them again - we just need
|
||||
// the channel_id to create a new placement for this channel.
|
||||
if (existingUsernames.has(lowerUsername)) {
|
||||
const existingChannelId = getExistingChannelIdByUsername(lowerUsername);
|
||||
if (!existingChannelId) {
|
||||
setError("Не удалось найти ID канала в проекте");
|
||||
return;
|
||||
}
|
||||
setSelectedChannels([...selectedChannels, { username, channelId: existingChannelId }]);
|
||||
setNewChannelInput("");
|
||||
setError(null);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!channel) {
|
||||
setError("Канал не найден в каталоге");
|
||||
// Channel doesn't exist in project yet. Create it globally via API to get channel_id.
|
||||
// If channel already exists globally, API will return existing channel.
|
||||
try {
|
||||
const createResponse = await channelsApi.create([{ username }]);
|
||||
const channelId = createResponse.results[0]?.channel.id;
|
||||
|
||||
if (!channelId) {
|
||||
setError("Не удалось получить ID канала");
|
||||
return;
|
||||
}
|
||||
|
||||
setSelectedChannels([...selectedChannels, { username, channelId: channel.id }]);
|
||||
setSelectedChannels([...selectedChannels, { username, channelId }]);
|
||||
setNewChannelInput("");
|
||||
setError(null);
|
||||
} catch (err) {
|
||||
console.error("Failed to look up channel:", err);
|
||||
setError("Не удалось найти канал");
|
||||
console.error("Failed to create channel:", err);
|
||||
setError("Не удалось создать канал");
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user