feat: 11.0 - Frontend - Create habit from form
This commit is contained in:
@@ -516,6 +516,7 @@
|
||||
// Create habit
|
||||
async function createHabit() {
|
||||
const nameInput = document.getElementById('habitName');
|
||||
const createBtn = document.getElementById('habitCreateBtn');
|
||||
const name = nameInput.value.trim();
|
||||
const frequency = document.querySelector('input[name="frequency"]:checked').value;
|
||||
|
||||
@@ -524,6 +525,11 @@
|
||||
return;
|
||||
}
|
||||
|
||||
// Disable button during submission (loading state)
|
||||
createBtn.disabled = true;
|
||||
const originalText = createBtn.textContent;
|
||||
createBtn.textContent = 'Se creează...';
|
||||
|
||||
try {
|
||||
const response = await fetch('/api/habits', {
|
||||
method: 'POST',
|
||||
@@ -532,16 +538,27 @@
|
||||
});
|
||||
|
||||
if (response.ok) {
|
||||
// Clear input field after successful creation
|
||||
nameInput.value = '';
|
||||
|
||||
hideHabitModal();
|
||||
showToast('Obișnuință creată cu succes!');
|
||||
loadHabits();
|
||||
} else {
|
||||
const error = await response.text();
|
||||
showToast('Eroare la crearea obișnuinței: ' + error);
|
||||
|
||||
// Re-enable button on error (modal stays open)
|
||||
createBtn.disabled = false;
|
||||
createBtn.textContent = originalText;
|
||||
}
|
||||
} catch (error) {
|
||||
showToast('Eroare la conectarea cu serverul');
|
||||
console.error(error);
|
||||
|
||||
// Re-enable button on error (modal stays open)
|
||||
createBtn.disabled = false;
|
||||
createBtn.textContent = originalText;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user