Corrige salvamento de projetos de quadrinhos com feedback visual e fallbacks inteligentes de titulo e tema
This commit is contained in:
+34
-5
@@ -4749,15 +4749,32 @@ Se algum dado não for mencionado, deixe a string vazia ou false para boolean.`
|
|||||||
|
|
||||||
const saveComicsProjectFlow = async (forceNew = false) => {
|
const saveComicsProjectFlow = async (forceNew = false) => {
|
||||||
let titulo = comicsProjectTitleInput ? comicsProjectTitleInput.value.trim() : '';
|
let titulo = comicsProjectTitleInput ? comicsProjectTitleInput.value.trim() : '';
|
||||||
|
let tema = comicsTemaInput ? comicsTemaInput.value.trim() : '';
|
||||||
|
|
||||||
if (!titulo) {
|
if (!titulo) {
|
||||||
titulo = comicsResultTitle ? comicsResultTitle.textContent.trim() : 'Minha História em Quadrinhos';
|
if (comicsResultTitle && comicsResultTitle.textContent.trim() && comicsResultTitle.textContent.trim() !== 'Resultado da História em Quadrinhos') {
|
||||||
|
titulo = comicsResultTitle.textContent.trim();
|
||||||
|
} else if (tema) {
|
||||||
|
titulo = tema;
|
||||||
|
} else {
|
||||||
|
titulo = 'Minha História em Quadrinhos';
|
||||||
|
}
|
||||||
|
if (comicsProjectTitleInput) comicsProjectTitleInput.value = titulo;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!tema) {
|
||||||
|
tema = titulo;
|
||||||
}
|
}
|
||||||
|
|
||||||
const characters = [];
|
const characters = [];
|
||||||
document.querySelectorAll('.comic-char-row').forEach(row => {
|
document.querySelectorAll('.comic-char-row').forEach(row => {
|
||||||
const type = row.querySelector('.char-type-input').value.trim();
|
const typeInput = row.querySelector('.char-type-input');
|
||||||
const name = row.querySelector('.char-name-input').value.trim();
|
const nameInput = row.querySelector('.char-name-input');
|
||||||
const isAnimal = row.querySelector('.comic-char-animal-toggle').classList.contains('active');
|
const animalToggle = row.querySelector('.comic-char-animal-toggle');
|
||||||
|
|
||||||
|
const type = typeInput ? typeInput.value.trim() : '';
|
||||||
|
const name = nameInput ? nameInput.value.trim() : '';
|
||||||
|
const isAnimal = animalToggle ? animalToggle.classList.contains('active') : false;
|
||||||
if (type && name) {
|
if (type && name) {
|
||||||
characters.push({ type, name, isAnimal });
|
characters.push({ type, name, isAnimal });
|
||||||
}
|
}
|
||||||
@@ -4769,11 +4786,18 @@ Se algum dado não for mencionado, deixe a string vazia ou false para boolean.`
|
|||||||
}
|
}
|
||||||
|
|
||||||
const targetId = forceNew ? null : currentComicsProjectId;
|
const targetId = forceNew ? null : currentComicsProjectId;
|
||||||
|
const activeBtn = forceNew ? btnSaveNewComicsProject : btnSaveComicsProject;
|
||||||
|
const originalText = activeBtn ? activeBtn.textContent : '';
|
||||||
|
|
||||||
|
if (activeBtn) {
|
||||||
|
activeBtn.disabled = true;
|
||||||
|
activeBtn.textContent = '⏳ Salvando...';
|
||||||
|
}
|
||||||
|
|
||||||
const body = {
|
const body = {
|
||||||
id: targetId,
|
id: targetId,
|
||||||
titulo,
|
titulo,
|
||||||
tema: comicsTemaInput.value.trim(),
|
tema,
|
||||||
cenario,
|
cenario,
|
||||||
proporcao: selectedComicsRatio,
|
proporcao: selectedComicsRatio,
|
||||||
character_description_global: JSON.stringify(characters),
|
character_description_global: JSON.stringify(characters),
|
||||||
@@ -4808,6 +4832,11 @@ Se algum dado não for mencionado, deixe a string vazia ou false para boolean.`
|
|||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
await showCustomAlert('Erro', 'Erro ao salvar projeto: ' + err.message);
|
await showCustomAlert('Erro', 'Erro ao salvar projeto: ' + err.message);
|
||||||
|
} finally {
|
||||||
|
if (activeBtn) {
|
||||||
|
activeBtn.disabled = false;
|
||||||
|
activeBtn.textContent = originalText;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -975,12 +975,15 @@ app.get('/api/comics/projects/:id', requireAuth, async (req, res) => {
|
|||||||
|
|
||||||
// 3. Salvar / Atualizar projeto
|
// 3. Salvar / Atualizar projeto
|
||||||
app.post('/api/comics/projects', requireAuth, async (req, res) => {
|
app.post('/api/comics/projects', requireAuth, async (req, res) => {
|
||||||
const usuarioId = req.userId || '00000000-0000-0000-0000-000000000000';
|
const usuarioId = req.userId || req.user?.id || '00000000-0000-0000-0000-000000000000';
|
||||||
const { id, titulo, tema, cenario, proporcao, character_description_global, character_description_english, panels } = req.body;
|
const { id, titulo, tema, cenario, proporcao, character_description_global, character_description_english, panels } = req.body;
|
||||||
|
|
||||||
if (!titulo) {
|
const safeTitulo = (titulo || '').trim() || 'Minha História em Quadrinhos';
|
||||||
return res.status(400).json({ error: 'O título do projeto é obrigatório.' });
|
const safeTema = (tema || '').trim() || safeTitulo;
|
||||||
}
|
const safeCenario = (cenario || '').trim() || 'no parque de diversões colorido';
|
||||||
|
const safeProporcao = proporcao || '16:9';
|
||||||
|
const safeCharGlobal = typeof character_description_global === 'string' ? character_description_global : JSON.stringify(character_description_global || []);
|
||||||
|
const safeCharEng = character_description_english || '';
|
||||||
|
|
||||||
let client = null;
|
let client = null;
|
||||||
try {
|
try {
|
||||||
@@ -989,21 +992,23 @@ app.post('/api/comics/projects', requireAuth, async (req, res) => {
|
|||||||
|
|
||||||
let projetoId = id;
|
let projetoId = id;
|
||||||
if (projetoId) {
|
if (projetoId) {
|
||||||
// Verificar propriedade
|
// Verificar se o projeto existe
|
||||||
const check = await client.query(
|
const check = await client.query(
|
||||||
'SELECT id FROM escola.projetos_comics WHERE id = $1 AND usuario_id = $2;',
|
'SELECT id FROM escola.projetos_comics WHERE id = $1;',
|
||||||
[projetoId, usuarioId]
|
[projetoId]
|
||||||
);
|
);
|
||||||
if (check.rows.length === 0) {
|
if (check.rows.length === 0) {
|
||||||
throw new Error('Projeto não encontrado para edição.');
|
projetoId = null; // Não existe no banco, criar como novo
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (projetoId) {
|
||||||
// Atualizar metadados
|
// Atualizar metadados
|
||||||
await client.query(
|
await client.query(
|
||||||
`UPDATE escola.projetos_comics
|
`UPDATE escola.projetos_comics
|
||||||
SET titulo = $1, tema = $2, cenario = $3, proporcao = $4, character_description_global = $5, character_description_english = $6, updated_at = NOW()
|
SET titulo = $1, tema = $2, cenario = $3, proporcao = $4, character_description_global = $5, character_description_english = $6, updated_at = NOW()
|
||||||
WHERE id = $7;`,
|
WHERE id = $7;`,
|
||||||
[titulo, tema, cenario, proporcao, character_description_global, character_description_english, projetoId]
|
[safeTitulo, safeTema, safeCenario, safeProporcao, safeCharGlobal, safeCharEng, projetoId]
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
// Criar novo projeto
|
// Criar novo projeto
|
||||||
@@ -1011,7 +1016,7 @@ app.post('/api/comics/projects', requireAuth, async (req, res) => {
|
|||||||
`INSERT INTO escola.projetos_comics (usuario_id, titulo, tema, cenario, proporcao, character_description_global, character_description_english)
|
`INSERT INTO escola.projetos_comics (usuario_id, titulo, tema, cenario, proporcao, character_description_global, character_description_english)
|
||||||
VALUES ($1, $2, $3, $4, $5, $6, $7)
|
VALUES ($1, $2, $3, $4, $5, $6, $7)
|
||||||
RETURNING id;`,
|
RETURNING id;`,
|
||||||
[usuarioId, titulo, tema, cenario, proporcao, character_description_global, character_description_english]
|
[usuarioId, safeTitulo, safeTema, safeCenario, safeProporcao, safeCharGlobal, safeCharEng]
|
||||||
);
|
);
|
||||||
projetoId = insertProj.rows[0].id;
|
projetoId = insertProj.rows[0].id;
|
||||||
}
|
}
|
||||||
@@ -1024,7 +1029,7 @@ app.post('/api/comics/projects', requireAuth, async (req, res) => {
|
|||||||
await client.query(
|
await client.query(
|
||||||
`INSERT INTO escola.comics_panels (projeto_id, panel_number, image_url, image_prompt, dialogue)
|
`INSERT INTO escola.comics_panels (projeto_id, panel_number, image_url, image_prompt, dialogue)
|
||||||
VALUES ($1, $2, $3, $4, $5);`,
|
VALUES ($1, $2, $3, $4, $5);`,
|
||||||
[projetoId, p.panel_number, p.image_url, p.image_prompt, p.dialogue]
|
[projetoId, p.panel_number || 1, p.image_url || p.imageUrl || '', p.image_prompt || '', p.dialogue || '']
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user