Finalize three-state toggle feature

This commit is contained in:
2026-02-11 12:27:52 -08:00
parent d48b934f5d
commit 6be1cfb6ab
3 changed files with 79 additions and 51 deletions

View File

@@ -3,19 +3,28 @@ require 'db.php';
$data = json_decode(file_get_contents('php://input'), true);
$type = $data['type'];
$id = intval($data['id']);
$highlighted = isset($data['highlighted']) && $data['highlighted'] ? 1 : 0;
$type = $data['type'] ?? '';
$id = intval($data['id'] ?? 0);
if ($id <= 0 || ($type !== 'task' && $type !== 'subtask')) {
echo json_encode(['success' => false, 'error' => 'Invalid request']);
exit;
}
if ($type === 'task') {
$stmt = $pdo->prepare("UPDATE tasks SET highlighted = ? WHERE id = ?");
$stmt->execute([$highlighted, $id]);
echo json_encode(['success' => true]);
} elseif ($type === 'subtask') {
$stmt = $pdo->prepare("UPDATE subtasks SET highlighted = ? WHERE id = ?");
$stmt->execute([$highlighted, $id]);
echo json_encode(['success' => true]);
$stmt = $pdo->prepare("UPDATE tasks SET highlighted = (highlighted + 1) % 3 WHERE id = ?");
$stmt->execute([$id]);
$stmt = $pdo->prepare("SELECT highlighted FROM tasks WHERE id = ?");
$stmt->execute([$id]);
$newState = intval($stmt->fetchColumn());
} else {
echo json_encode(['success' => false, 'error' => 'Invalid type']);
$stmt = $pdo->prepare("UPDATE subtasks SET highlighted = (highlighted + 1) % 3 WHERE id = ?");
$stmt->execute([$id]);
$stmt = $pdo->prepare("SELECT highlighted FROM subtasks WHERE id = ?");
$stmt->execute([$id]);
$newState = intval($stmt->fetchColumn());
}
?>
echo json_encode(['success' => true, 'highlighted' => $newState]);