53 lines
1.3 KiB
JavaScript
53 lines
1.3 KiB
JavaScript
const form = document.getElementById("addCategoryForm");
|
|
const messages = document.getElementById("messages");
|
|
|
|
const nameField = document.getElementById("categoryName");
|
|
const descField = document.getElementById("categoryDescription");
|
|
|
|
// Catégories existantes (simulation BDD)
|
|
const existingCategories = ["Actualités", "Chien", "Chat", "Boutique"];
|
|
|
|
|
|
function showMessage(type, text) {
|
|
messages.innerHTML = "";
|
|
|
|
const div = document.createElement("div");
|
|
div.className = `alert alert-${type}`;
|
|
div.textContent = text;
|
|
|
|
messages.appendChild(div);
|
|
}
|
|
|
|
|
|
form.addEventListener("submit", function (e) {
|
|
e.preventDefault();
|
|
messages.innerHTML = "";
|
|
|
|
const nom = nameField.value.trim();
|
|
|
|
// Champ obligatoire
|
|
if (!nom) {
|
|
showMessage("danger", "Le nom de la catégorie est obligatoire.");
|
|
return;
|
|
}
|
|
|
|
// Catégorie existante
|
|
if (existingCategories.includes(nom)) {
|
|
showMessage("danger", "Cette catégorie existe déjà. Veuillez en choisir une autre.");
|
|
return;
|
|
}
|
|
|
|
// Ajout catégorie (simulation)
|
|
existingCategories.push(nom);
|
|
|
|
showMessage("success", "Catégorie ajoutée avec succès !");
|
|
|
|
// Reset formulaire
|
|
form.reset();
|
|
|
|
// Redirection
|
|
setTimeout(() => {
|
|
window.location.href = "../html/accueil_blog.html";
|
|
}, 1500);
|
|
});
|