-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
68 lines (59 loc) · 2.05 KB
/
script.js
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
let items = [];
function renderItems() {
const itemList = document.getElementById('itemList');
itemList.innerHTML = '';
items.forEach(item => {
const listItem = document.createElement('li');
listItem.textContent = item.name;
const editButton = document.createElement('button');
editButton.textContent = 'Edit';
editButton.onclick = () => editItem(item.id);
const deleteButton = document.createElement('button');
deleteButton.textContent = 'Delete';
deleteButton.onclick = () => deleteItem(item.id);
listItem.appendChild(editButton);
listItem.appendChild(deleteButton);
itemList.appendChild(listItem);
});
}
function addItem() {
const newItemName = document.getElementById('newItemName').value;
if (newItemName.trim() !== '') {
fetch('/items', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ name: newItemName }),
})
.then(response => response.json())
.then(newItem => {
items.push(newItem);
renderItems();
document.getElementById('newItemName').value = '';
})
.catch(error => console.error('Error adding item:', error));
}
}
function editItem(itemId) {
// ... (Implementation for editing an item - you'll need to handle getting the updated name, sending a PUT request to the server, and updating the local `items` array)
console.log(`Editing item with ID: ${itemId}`);
}
function deleteItem(itemId) {
fetch(`/items/${itemId}`, {
method: 'DELETE'
})
.then(() => {
items = items.filter(item => item.id !== itemId);
renderItems();
})
.catch(error => console.error('Error deleting item:', error));
}
// Fetch initial data from the server
fetch('/items')
.then(response => response.json())
.then(data => {
items = data;
renderItems();
})
.catch(error => console.error('Error fetching items:', error));