why is your code so complicated?
Jul 9, 2023
6
Level 1
Cannot retrieve value from database on page reload/refresh
I have a tickbox to mark tasks compleate, it also updates the tasks database is_complete field ticked = 1 unticked = 0 this is all working just fine, the problem I have is if I refresh or reload the page the UI is not showing the tasks as complete, the database still shows them as is_complete = 1
This is my js
const todoIsDoneCheckbox = document.getElementsByName("todo-checkbox");
for (let i = 0; i < todoIsDoneCheckbox.length; i++) {
const checkbox = todoIsDoneCheckbox[i];
const todoListItem = checkbox.parentNode;
const taskId = todoListItem.getAttribute("data-task-id");
const statusArray = todoListItem.getAttribute("data-status").split(",");
const isComplete = statusArray.indexOf("done") >= 0;
checkbox.checked = isComplete;
todoListItem.setAttribute("data-complete", isComplete);
if (isComplete) {
todoListItem.classList.add("completed");
}
checkbox.addEventListener("click", function (e) {
const isChecked = checkbox.checked;
const isComplete = isChecked ? 1 : 0;
// Send an AJAX request to update the task completion status
updateTaskCompletionStatus(taskId, isComplete)
.then(() => {
// Update the UI based on the task completion status
if (isChecked) {
todoListItem.classList.add("completed");
statusArray.push("done");
} else {
todoListItem.classList.remove("completed");
const index = statusArray.indexOf("done");
if (index !== -1) {
statusArray.splice(index, 1);
}
}
todoListItem.setAttribute(
"data-status",
statusArray.join(",")
);
todoListItem.setAttribute("data-complete", isComplete);
})
.catch((error) => {
console.error(
"Error updating task completion status:",
error
);
});
});
}
function updateTaskCompletionStatus(taskId, isComplete) {
// Get the CSRF token from the page header
const csrfToken = document
.querySelector('meta[name="csrf-token"]')
.getAttribute("content");
// Send an AJAX request to update the task completion status
return fetch(`/tasks/${taskId}/update-completion`, {
method: "PUT",
headers: {
"Content-Type": "application/json",
"X-CSRF-TOKEN": csrfToken,
},
body: JSON.stringify({ is_complete: isComplete }),
}).then((response) => response.json());
}
This is what I have on my form
@if ($tasks->count() > 0)
@php
$hasVisibleTasks = false;
@endphp
@foreach ($tasks as $task)
@php
$task = $task->fresh(); // Refresh the task instance from the database
@endphp
@if ($task->is_deleted === 0)
@php
$hasVisibleTasks = true;
$statusArray = [];
if ($task->is_completed) {
$statusArray[] = 'done';
}
if ($task->is_stared) {
$statusArray[] = 'stared';
}
$statusArray = array_merge($statusArray, explode(',', $task->tags));
$status = implode(',', $statusArray);
@endphp
in my AppsController I have
public function updateCompletionStatus(Request $request, Task $task)
{
$isComplete = $request->input('is_complete', false);
// Update the task's completion status in the database
$task->is_completed = $isComplete;
$task->save();
// Return a JSON response indicating the success status
return response()->json(['success' => true]);
}
I was trying to get the $tasks to update to their current state using @php $task = $task->fresh(); // Refresh the task instance from the database @endphp
but it doesn't seem to be working
Please or to participate in this conversation.