85 lines
2.5 KiB
JavaScript
85 lines
2.5 KiB
JavaScript
const form = document.getElementById('ajouterArticleForm');
|
|
const imgField = document.getElementById('articleImage');
|
|
const titleField = document.getElementById('articleTitle');
|
|
const categoryField = document.getElementById('articleCategory');
|
|
const publishedField = document.getElementById('articlePublished');
|
|
|
|
const errorEmpty = document.getElementById('errorEmpty');
|
|
const errorImage = document.getElementById('errorImage');
|
|
const errorExists = document.getElementById('errorExists');
|
|
const successMsg = document.getElementById('successMsg');
|
|
|
|
// Simulation BDD
|
|
const titreExistants = ['décoration noel', 'coupe de chien'];
|
|
|
|
form.addEventListener('submit', function(e) {
|
|
e.preventDefault();
|
|
|
|
const titre = titleField.value.trim().toLowerCase();
|
|
const fichierImage = imgField.files[0];
|
|
const contenu = tinymce.get("articleContent").getContent(); // ← CORRECTION IMPORTANTE
|
|
const categorie = categoryField.value;
|
|
|
|
// Reset messages
|
|
errorEmpty.classList.add('d-none');
|
|
errorImage.classList.add('d-none');
|
|
errorExists.classList.add('d-none');
|
|
successMsg.classList.add('d-none');
|
|
|
|
// Catégorie obligatoire
|
|
if (categorie === "") {
|
|
errorEmpty.textContent = "Veuillez choisir une catégorie.";
|
|
errorEmpty.classList.remove('d-none');
|
|
return;
|
|
}
|
|
|
|
// Titre obligatoire
|
|
if (titre === "") {
|
|
errorEmpty.textContent = "Le titre de l'article est obligatoire.";
|
|
errorEmpty.classList.remove('d-none');
|
|
return;
|
|
}
|
|
|
|
// Titre déjà existant
|
|
if (titreExistants.includes(titre)) {
|
|
errorExists.classList.remove('d-none');
|
|
return;
|
|
}
|
|
|
|
// Contenu obligatoire
|
|
if (contenu.trim() === "") {
|
|
errorEmpty.textContent = "Le contenu de l'article ne peut pas être vide.";
|
|
errorEmpty.classList.remove('d-none');
|
|
return;
|
|
}
|
|
|
|
// Image invalide
|
|
if (fichierImage) {
|
|
const validFormats = ['image/jpeg', 'image/png'];
|
|
if (!validFormats.includes(fichierImage.type)) {
|
|
errorImage.classList.remove('d-none');
|
|
return;
|
|
}
|
|
}
|
|
|
|
// Simuler enregistrement
|
|
titreExistants.push(titre);
|
|
|
|
// Succès
|
|
successMsg.classList.remove('d-none');
|
|
|
|
// Redirection
|
|
setTimeout(() => {
|
|
window.location.href = "../html/liste_categorie_article.html";
|
|
}, 1000);
|
|
});
|
|
|
|
// TinyMCE
|
|
tinymce.init({
|
|
selector: '#articleContent',
|
|
height: 400,
|
|
language: 'fr',
|
|
plugins: 'lists fullscreen',
|
|
toolbar: 'undo redo | bold italic underline | bullist numlist | fullscreen',
|
|
});
|