In the last session, we have covered loading tasks after login (using React LoginForm) from Rails API using Rails JWT authentication mechanism. We also implemented the React form for Adding a Task using Fluent UI’s Dialog components. In this session we cover deleting a task and moving on to how we dockerize our Rails/React application.
Step 1: Verify the Rails delete action
Our destroy action from: backend/app/controllers/api/v1/tasks_controller.rb
before_action :set_task, only: %i[show update destroy]
....
def destroy
@task.destroy!
head :no_content
end
private
def set_task
@task = current_user.tasks.find(params[:id])
end
We will implement the frontend deleteTask() service. A few important details:
current_user.tasks.find(params[:id])ensures users can only delete their own tasks.@task.destroy!deletes the record.head :no_contentreturns HTTP 204 No Content.
Because the response has no JSON body, our frontend must not call response.json() after deletion.
Step 1: Add deleteTask() to taskService.ts
Add this function below our existing createTask() function:
export async function deleteTask(taskId: number, accessToken: string): Promise<void> {
const response = await fetch(
`${import.meta.env.VITE_API_BASE_URL}/api/v1/tasks/${taskId}`,
{
method: "DELETE",
headers: {
Authorization: `Bearer ${accessToken}`
}
}
);
if (!response.ok) {
throw new Error("Failed to delete task");
}
}