Skip to content

Commit

Permalink
Add sample UI
Browse files Browse the repository at this point in the history
  • Loading branch information
jaguililla committed Jun 27, 2024
1 parent 796de55 commit ac0e0f2
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 18 deletions.
6 changes: 3 additions & 3 deletions src/main/resources/static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ <h2>Open</h2>
<table>
<thead>
<tr>
<th>Id</th>
<th>Title</th>
<th>Order</th>
<th>Completed</th>
</tr>
</thead>
<tbody id="tasks"></tbody>
Expand All @@ -43,9 +43,9 @@ <h2>New</h2>

<template id="todoRow">
<tr>
<td>a</td>
<td>b</td>
<td>title</td>
<td>0</td>
<td><input type="checkbox" checked></td>
</tr>
</template>

Expand Down
43 changes: 28 additions & 15 deletions src/main/resources/static/main.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@

const http = new XMLHttpRequest();

const tbody = document.querySelector("tbody#tasks");
const template = document.querySelector("#todoRow");
const titleInput = document.querySelector("#title");
const orderInput = document.querySelector("#order");

function httpSend(method, url, body, callback) {
http.open(method, url);
http.setRequestHeader('Content-type', 'application/json');
Expand All @@ -16,33 +21,41 @@ function httpPost(url, body, callback) {
httpSend('POST', url, body, callback);
}

function addTask(task) {
const clone = template.content.cloneNode(true);
const td = clone.querySelectorAll("td");
const input = clone.querySelectorAll("input");
td[0].textContent = task.title;
td[1].textContent = task.order;
input.checked = task.completed;
tbody.appendChild(clone);
}

function add() {
let body = { title: 'a', order: 0 };
const body = {
title: titleInput.value,
order: orderInput.valueAsNumber
};

httpPost('/tasks', body, () => {
titleInput.value = "";
orderInput.value = 0;

console.log(http.responseText)
addTask(JSON.parse(http.responseText));
});
}

function main() {
const tbody = document.querySelector("tbody#tasks");
const template = document.querySelector("#todoRow");

httpGet('/tasks', null, () => {
let response = JSON.parse(http.responseText);
for (const tr of tbody.children) {
for (const tr of tbody.children)
tr.remove();
}

for (const task of response) {
const clone = template.content.cloneNode(true);
let td = clone.querySelectorAll("td");
td[0].textContent = task.title;
td[1].textContent = task.order;
tbody.appendChild(clone);
}
const response = JSON.parse(http.responseText);
for (const task of response)
addTask(task);

console.log(http.responseText)
console.log(http.responseText);
});
}

Expand Down

0 comments on commit ac0e0f2

Please sign in to comment.