summaryrefslogtreecommitdiff
path: root/web/app.js
blob: e66c878d22f1b4ff2a5bc5d5eaae6a5a5f1d659e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
const API_BASE = window.location.origin;

// ── Fetch ─────────────────────────────────────────────────────────────────────

async function fetchTasks() {
  const res = await fetch(`${API_BASE}/api/tasks`);
  if (!res.ok) throw new Error(`HTTP ${res.status}`);
  return res.json();
}

// ── Render ────────────────────────────────────────────────────────────────────

function formatDate(iso) {
  if (!iso) return '';
  return new Date(iso).toLocaleString(undefined, {
    month: 'short', day: 'numeric',
    hour: '2-digit', minute: '2-digit',
  });
}

function createTaskCard(task) {
  const card = document.createElement('div');
  card.className = 'task-card';
  card.dataset.taskId = task.id;

  // Header: name + state badge
  const header = document.createElement('div');
  header.className = 'task-card-header';

  const name = document.createElement('span');
  name.className = 'task-name';
  name.textContent = task.name;

  const badge = document.createElement('span');
  badge.className = 'state-badge';
  badge.dataset.state = task.state;
  badge.textContent = task.state.replace(/_/g, ' ');

  header.append(name, badge);
  card.appendChild(header);

  // Meta: priority + created_at
  const meta = document.createElement('div');
  meta.className = 'task-meta';
  if (task.priority) {
    const prio = document.createElement('span');
    prio.textContent = task.priority;
    meta.appendChild(prio);
  }
  if (task.created_at) {
    const when = document.createElement('span');
    when.textContent = formatDate(task.created_at);
    meta.appendChild(when);
  }
  if (meta.children.length) card.appendChild(meta);

  // Description (truncated via CSS)
  if (task.description) {
    const desc = document.createElement('div');
    desc.className = 'task-description';
    desc.textContent = task.description;
    card.appendChild(desc);
  }

  // Footer: Run button (only for PENDING / FAILED)
  if (task.state === 'PENDING' || task.state === 'FAILED') {
    const footer = document.createElement('div');
    footer.className = 'task-card-footer';

    const btn = document.createElement('button');
    btn.className = 'btn-run';
    btn.textContent = 'Run';
    btn.addEventListener('click', () => handleRun(task.id, btn, footer));

    footer.appendChild(btn);
    card.appendChild(footer);
  }

  return card;
}

function renderTaskList(tasks) {
  const container = document.querySelector('.task-list');

  if (!tasks || tasks.length === 0) {
    container.innerHTML = '<div id="loading">No tasks found.</div>';
    return;
  }

  // Replace contents with task cards
  container.innerHTML = '';
  for (const task of tasks) {
    container.appendChild(createTaskCard(task));
  }
}

// ── Run action ────────────────────────────────────────────────────────────────

async function runTask(taskId) {
  const res = await fetch(`${API_BASE}/api/tasks/${taskId}/run`, { method: 'POST' });
  if (!res.ok) {
    let msg = `HTTP ${res.status}`;
    try { const body = await res.json(); msg = body.error || body.message || msg; } catch {}
    throw new Error(msg);
  }
  return res.json();
}

async function handleRun(taskId, btn, footer) {
  btn.disabled = true;
  btn.textContent = 'Queuing…';

  // Remove any previous error
  const prev = footer.querySelector('.task-error');
  if (prev) prev.remove();

  try {
    await runTask(taskId);
    // Refresh list immediately so state flips to QUEUED
    const tasks = await fetchTasks();
    renderTaskList(tasks);
  } catch (err) {
    btn.disabled = false;
    btn.textContent = 'Run';

    const errEl = document.createElement('span');
    errEl.className = 'task-error';
    errEl.textContent = `Failed to queue: ${err.message}`;
    footer.appendChild(errEl);
  }
}

// ── Polling ───────────────────────────────────────────────────────────────────

async function poll() {
  try {
    const tasks = await fetchTasks();
    renderTaskList(tasks);
  } catch {
    document.querySelector('.task-list').innerHTML =
      '<div id="loading">Could not reach server.</div>';
  }
}

function startPolling(intervalMs = 10_000) {
  poll();
  setInterval(poll, intervalMs);
}

// ── Boot ──────────────────────────────────────────────────────────────────────

startPolling();