fix: resolve content update payload parsing and add update vs create logic

- Extract content from formData.content instead of passing whole object
- Add logic to call updateContent() for existing content vs createContent() for new
- Fix 400 Invalid JSON error caused by sending object instead of string
- Handle different formData formats: string, {content: string}, {text: string}

The frontend was sending html_content as {type:'html', content:'...'} object
but server expected a plain string. Now properly extracts the content value.
This commit is contained in:
2025-09-20 17:40:23 +02:00
parent 2177055c76
commit 6f682372b5
2 changed files with 104 additions and 15 deletions

View File

@@ -113,22 +113,31 @@ export class InsertrEditor {
console.log('💾 Saving content:', meta.contentId, formData);
try {
// Extract content value based on type
// Extract content value from formData
let contentValue;
if (meta.element.tagName.toLowerCase() === 'a') {
// For links, save the text content (URL is handled separately if needed)
contentValue = formData.text || formData;
if (typeof formData === 'string') {
contentValue = formData;
} else if (formData.content) {
contentValue = formData.content;
} else if (formData.text) {
contentValue = formData.text;
} else {
contentValue = formData.text || formData;
contentValue = formData;
}
// Universal upsert - server handles ID extraction/generation from markup
const contentType = this.determineContentType(meta.element);
const result = await this.apiClient.createContent(
contentValue,
contentType,
meta.htmlMarkup // Always send HTML markup - server is smart about ID handling
);
let result;
if (meta.contentId) {
// Update existing content
result = await this.apiClient.updateContent(meta.contentId, contentValue);
} else {
// Create new content - server handles ID extraction/generation from markup
const contentType = this.determineContentType(meta.element);
result = await this.apiClient.createContent(
contentValue,
contentType,
meta.htmlMarkup // Always send HTML markup - server is smart about ID handling
);
}
if (result) {
// Store the backend-generated/confirmed ID in the element