how to rewrite this database call so that the ui is updated immediately before the fetch request is made
async function completeGoal(id: number, completed: string, target: any) { // added target
if (target.dataset.goalcomplete == 'yes') { // added from here
target.parentElement.firstElementChild.src = "tikedfilled.svg";
target.parentElement.children[2].classList.add('goal-completed');
}
else if (target.dataset.goalcomplete == 'no') {
target.parentElement.firstElementChild.src = "tikbox.svg";
target.parentElement.children[2].classList.remove('goal-completed');
} // to here
try {
const response = await fetch(`/api/goals/${id}`, {
method: 'PATCH',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ completed }),
});
if (!response.ok) {
throw new Error('Network response not ok');
}
repopulateGoals();
}
catch (error) {
console.error('Error: ', error);
}
}