function toggleEdit(commentId) {
console.log("Toggling edit for comment ID:", commentId);
var commentText = document.getElementById('comment-' + commentId);
var editTextarea = document.getElementById('edit-comment-' + commentId);
var button = document.getElementById('edit-save-button-' + commentId);
if (editTextarea.style.display === 'none') {
// Switch to edit mode
console.log("Switching to edit mode");
commentText.style.display = 'none';
editTextarea.style.display = 'block';
button.textContent = 'Save';
} else {
// Save the edited comment
console.log("Saving edited comment");
var editedComment = editTextarea.value;
console.log(" edited comment is:",editTextarea.value);
console.log(" controller method:",'<?=$user_base_url?>/profil/save_comment');
$.ajax({
url: '<?=$user_base_url?>/profil/save_comment',//+ new Date().getTime(),
type: 'POST',
data: {
id: commentId,
comment: editedComment
},
success: function(response) {
console.log("AJAX call successful");
console.log(response);
if (response.success) {
console.log("Comment saved successfully");
// Update the displayed comment and switch back to view mode
commentText.innerHTML = editedComment.replace(/\n/g, '<br>');
commentText.style.display = 'block';
editTextarea.style.display = 'none';
button.textContent = 'Edit';
} else {
console.error("Error saving comment: ", response);
alert('Error saving comment.');
}
},
error: function(xhr, status, error) {
console.error("AJAX call failed:", status, error);
alert('AJAX call failed.');
}
});
}
}