165 lines
5.0 KiB
JavaScript
165 lines
5.0 KiB
JavaScript
import { simpleNotification } from '../z_comps/notify.js';
|
|
|
|
|
|
// VACIAR LA INFORMACIÓN DE LA BASE DE DATOS EN LA UI
|
|
let title_post = data[0];
|
|
let body_post = data[1];
|
|
|
|
// https://summernote.org/getting-started/#without-bootstrap-lite
|
|
|
|
// configuración inicial de campo de editor de texto
|
|
// Inicializa Summernote con tus opciones
|
|
$('#summernote').summernote({
|
|
placeholder: 'Escribe aquí...',
|
|
tabsize: 2,
|
|
width: 1200,
|
|
height: 500,
|
|
toolbar: [
|
|
['misc', ['undo', 'redo', 'clear']],
|
|
['heading', ['h1', 'h2', 'h3', 'h4', 'h5', 'h6']], // tamaño de encabezado
|
|
['style', ['style','bold', 'italic', 'underline']],
|
|
['fontsize', ['fontsize']],
|
|
['color', ['color']],
|
|
['height', ['height']],
|
|
['font', ['strikethrough', 'superscript', 'subscript', 'clear']],
|
|
['para', ['ul', 'ol', 'paragraph']],
|
|
['object', ['link', 'table', 'picture', 'video', 'hr']],
|
|
['misc', [ 'help', 'codeview']], //'fullscreen'
|
|
['fontname', ['fontname']],
|
|
|
|
],
|
|
callbacks: {
|
|
onInit: function() {
|
|
const btn_submit = document.getElementById('btn-submit');
|
|
const btn_cancel = document.getElementById('btn-cancel');
|
|
const toolbar = document.querySelector('.note-toolbar');
|
|
const iTitle = document.querySelector('input[name="title"]');
|
|
|
|
|
|
if (btn_submit && toolbar && iTitle) {
|
|
|
|
const titleWrapper = document.createElement('div');
|
|
titleWrapper.classList.add('note-title-wrapper', 'mb-2'); // margen inferior
|
|
|
|
// Clonar el input o moverlo (según tu preferencia)
|
|
titleWrapper.appendChild(iTitle);
|
|
|
|
// Insertar al inicio de la barra de herramientas
|
|
toolbar.prepend(titleWrapper);
|
|
|
|
// Crear un contenedor para el botón de enviar
|
|
const btnGroup = document.createElement('div');
|
|
btnGroup.classList.add('note-btn-group', 'ms-2');
|
|
|
|
// agregar btn enviar cambios
|
|
btnGroup.appendChild(btn_submit);
|
|
// agregar btn cancelar
|
|
btnGroup.appendChild(btn_cancel);
|
|
// agregar btnGroup al toolbar
|
|
toolbar.appendChild(btnGroup);
|
|
|
|
}
|
|
}
|
|
}
|
|
});
|
|
|
|
// Luego, carga el contenido en el editor
|
|
$('#summernote').summernote('code', body_post);
|
|
|
|
// También puedes llenar el campo de título si quieres:
|
|
document.querySelector('[name="title"]').value = title_post;
|
|
|
|
|
|
|
|
// ENVIAR LOS NUEVOS CAMBIOS A LA BASE DE DATOS
|
|
|
|
// async function a_sendpost(title, body, id) {
|
|
// try {
|
|
// let response = await fetch('/user/save-post', {
|
|
// method: 'POST',
|
|
// headers: {
|
|
// 'Content-Type': 'application/json'
|
|
// },
|
|
// body: JSON.stringify({ "id": id, "title": title, "body": body}),
|
|
// credentials: 'include'
|
|
// });
|
|
|
|
|
|
// if (!response.ok) {
|
|
// throw new Error('Error en la respuesta del servidor <-');
|
|
// }
|
|
|
|
// const result = await response.json();
|
|
|
|
// simpleNotification('Éxito', `Publicación actualizada: ${result.title_post}`, 'success');
|
|
// // Opcional: Limpiar el editor después de guardar
|
|
// $('#summernote').summernote('code', '');
|
|
// document.querySelector("input[name='title']").value = '';
|
|
|
|
// } catch (error) {
|
|
// console.error('Error al guardar:', error);
|
|
// simpleNotification('Error', 'No se pudo guardar la publicación', 'error');
|
|
// }
|
|
// }
|
|
|
|
|
|
async function a_sendpost(title, body, id) {
|
|
try {
|
|
let response = await fetch('/user/save-post', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
},
|
|
body: JSON.stringify({ "id": id, "title": title, "body": body }),
|
|
credentials: 'include'
|
|
});
|
|
|
|
if (!response.ok) {
|
|
throw new Error('Error en la respuesta del servidor <-');
|
|
}
|
|
|
|
const result = await response.json();
|
|
|
|
simpleNotification('Éxito', `Actualizando: ${result.title_post}...`, 'success'); // Añade tiempo
|
|
|
|
// Redirigir después de 2 segundos (puedes ajustar)
|
|
setTimeout(() => {
|
|
window.location.href = result.redirect_url || '/user/my-posts';
|
|
}, 2000);
|
|
|
|
} catch (error) {
|
|
console.error('Error al guardar:', error);
|
|
simpleNotification('Error', 'No se pudo guardar la publicación', 'error');
|
|
}
|
|
}
|
|
|
|
|
|
document.getElementById('post-form').addEventListener('submit', function(e) {
|
|
e.preventDefault();
|
|
|
|
let new_title_post = document.querySelector("input[name='title']").value.trim();
|
|
let new_body_post = $('#summernote').summernote('code'); // Obtener el contenido del editor
|
|
let body_gt11 = new_body_post.length > 11 ? new_body_post : '';
|
|
|
|
// Validación (descomenta cuando necesites)
|
|
if (!new_title_post || (!new_body_post || body_gt11 === '')) {
|
|
simpleNotification('Error', 'El título y el contenido son obligatorios', 'error');
|
|
return;
|
|
}
|
|
|
|
// en caso que la información no cambie
|
|
if (new_title_post === title_post && new_body_post === body_post) {
|
|
simpleNotification('Sin Cambios', 'No se han realizado cambios en la publicación.', 'warning');
|
|
return;
|
|
}
|
|
// en caso de que si hayan cambios
|
|
else{
|
|
// console.log(id_post);
|
|
a_sendpost(new_title_post, new_body_post, id_post);
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|