diff --git a/src/app/[locale]/(dashboard)/trends/page.tsx b/src/app/[locale]/(dashboard)/trends/page.tsx index b753e07..ec53b86 100644 --- a/src/app/[locale]/(dashboard)/trends/page.tsx +++ b/src/app/[locale]/(dashboard)/trends/page.tsx @@ -114,11 +114,23 @@ export default function TrendsPage() { type: trendsArray.length > 0 ? "success" : "info" }); } else { - throw new Error('Failed to scan trends'); + let errorMsg = 'Failed to scan trends'; + const errorText = await res.text(); + try { + const errorData = JSON.parse(errorText); + errorMsg = errorData.message || errorData.error || errorMsg; + } catch (e) { + if (errorText) errorMsg = errorText; + } + throw new Error(errorMsg); } - } catch (error) { + } catch (error: any) { console.error('Scan error:', error); - toaster.create({ title: "Failed to scan trends", type: "error" }); + toaster.create({ + title: "Tarama Başarısız", + description: error.message || "Bilinmeyen bir hata oluştu.", + type: "error" + }); } finally { setIsScanning(false); } diff --git a/src/components/generate/GeneratedContentResult.tsx b/src/components/generate/GeneratedContentResult.tsx index d336763..e413149 100644 --- a/src/components/generate/GeneratedContentResult.tsx +++ b/src/components/generate/GeneratedContentResult.tsx @@ -153,27 +153,27 @@ export function GeneratedContentResult({ bundle, masterContentId, onReset }: Pro throw new Error(masterData.message || 'Failed to fetch master content from backend'); } - // Find the content record for this platform const contents = masterData.contents || masterData.data?.contents || []; const platformContent = contents.find((c: any) => { - const type = c.type?.toLowerCase() || ''; + const type = (c.type || '').toLowerCase(); const pName = platformName.toLowerCase(); - const title = c.title?.toLowerCase() || ''; + const title = (c.title || '').toLowerCase(); - // Match perfectly by title suffix (e.g., "Topic - medium") + // Priority 1: Match specifically by title suffix (e.g., "Topic - Medium") if (title.endsWith(`- ${pName}`)) return true; - if (title.includes(pName)) return true; - // Match medium to blog since backend saves medium as BLOG type - if (pName === 'medium' && type === 'blog') return true; + // Priority 2: Match medium to blog since backend saves medium as BLOG type + if (pName === 'medium' && (type === 'blog' || type === 'medium')) return true; + if (pName === 'blog' && (type === 'blog' || type === 'medium')) return true; + // Priority 3: Direct type match return type === pName; }); if (!platformContent) { - console.error(`Save failed. platformName: ${platformName}, contents found: ${contents.length}`); - throw new Error(`No saved content found for ${platformName}`); + console.error(`Save failed. platformName: ${platformName}, contents found:`, contents); + throw new Error(`Kayıtlı içerik bulunamadı: ${platformName}. Lütfen sayfayı yenileyip tekrar deneyin.`); } // Update the content @@ -184,11 +184,17 @@ export function GeneratedContentResult({ bundle, masterContentId, onReset }: Pro const updateRes = await fetch(`/api/backend/content/${platformContent.id}`, { method: 'PUT', - headers, + headers: { + ...headers, + 'Authorization': `Bearer ${session?.accessToken}` + }, body: JSON.stringify(updatePayload), }); - if (!updateRes.ok) throw new Error('Failed to update content'); + if (!updateRes.ok) { + const errData = await updateRes.json().catch(() => ({})); + throw new Error(errData.message || 'İçerik güncellenirken bir hata oluştu.'); + } toaster.create({ title: "Saved!", @@ -333,24 +339,31 @@ export function GeneratedContentResult({ bundle, masterContentId, onReset }: Pro const contents = masterData.contents || masterData.data?.contents || []; const platformContent = contents.find((c: any) => { - const type = c.type?.toLowerCase() || ''; + const type = (c.type || '').toLowerCase(); const pName = platformName.toLowerCase(); - const title = c.title?.toLowerCase() || ''; + const title = (c.title || '').toLowerCase(); if (title.endsWith(`- ${pName}`)) return true; - if (pName === 'medium' && type === 'blog') return true; + if (pName === 'medium' && (type === 'blog' || type === 'medium')) return true; + if (pName === 'blog' && (type === 'blog' || type === 'medium')) return true; return type === pName; }); if (platformContent) { - await fetch(`/api/backend/content/${platformContent.id}`, { + const saveRes = await fetch(`/api/backend/content/${platformContent.id}`, { method: 'PUT', - headers, + headers: { + ...headers, + 'Authorization': `Bearer ${session?.accessToken}` + }, body: JSON.stringify({ imageUrl }), }); + + if (!saveRes.ok) throw new Error('Görsel güncellenirken hata oluştu.'); + toaster.create({ - title: "Image Saved", - description: "Content image updated successfully.", + title: "Görsel Kaydedildi", + description: "İçerik görseli başarıyla güncellendi.", type: "success", }); }