Skip to content

Commit 4f195c6

Browse files
fix merge conflicts
2 parents 78c8049 + 9df7c2b commit 4f195c6

File tree

4 files changed

+43
-25
lines changed

4 files changed

+43
-25
lines changed

public/index.html

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ <h3>Borrowing to go here</h3>
1919
<form>
2020
<input type="text" id="search-input" placeholder="Enter your search here..." autocomplete="off">
2121
<button id="search-btn">search</button>
22+
<button id="all-items-btn">all items</button>
2223
</form>
2324
<section class="table-container">
2425
<table id="items-table">

public/script.js

+37-20
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ var itemIdInput = document.getElementById('item-id');
88
var successDiv= document.getElementById('success');
99
var reqForm = document.getElementById('request-form');
1010
var submitItemBtn = document.getElementById('submit-item');
11+
var allItemsBtn = document.getElementById('all-items-btn');
1112

1213
function request(url, method, cb) {
1314
var xhr = new XMLHttpRequest();
@@ -107,6 +108,14 @@ searchBtn.addEventListener("click", function(e) {
107108
}
108109
});
109110

111+
allItemsBtn.addEventListener("click", function(e) {
112+
e.preventDefault();
113+
114+
115+
// requestData uses a callback populate/ musicPopulate to populate the DOM
116+
request("/search?q=", 'GET', updateDom);
117+
118+
});
110119

111120
function updateDom(err, data) {
112121
if (err) {
@@ -118,26 +127,34 @@ function updateDom(err, data) {
118127
var table = document.getElementById("items-table");
119128
clearList(table);
120129

121-
items.forEach(function(item) {
122-
// adding our item names
123-
var row = document.createElement("tr");
124-
var name = document.createElement("td");
125-
var loanBtn = document.createElement('button');
126-
loanBtn.textContent = 'borrow';
127-
loanBtn.setAttribute('id', item.id);
128-
loanBtn.setAttribute('onclick', "borrow(this.id)");
129-
name.innerHTML = item.name;
130-
row.appendChild(name);
131-
132-
// adding our item descriptions
133-
var description = document.createElement("td");
134-
description.innerHTML = item.description;
135-
row.appendChild(description);
136-
row.appendChild(loanBtn);
137-
// add everything to the table
138-
table.appendChild(row);
139-
140-
});
130+
if(items.length > 0){
131+
items.forEach(function(item) {
132+
// adding our item names
133+
var row = document.createElement("tr");
134+
var name = document.createElement("td");
135+
var loanBtn = document.createElement('button');
136+
loanBtn.textContent = 'borrow';
137+
loanBtn.setAttribute('id', item.id);
138+
loanBtn.setAttribute('onclick', "borrow(this.id)");
139+
name.innerHTML = item.name;
140+
row.appendChild(name);
141+
142+
// adding our item descriptions
143+
var description = document.createElement("td");
144+
description.innerHTML = item.description;
145+
row.appendChild(description);
146+
row.appendChild(loanBtn);
147+
// add everything to the table
148+
table.appendChild(row);
149+
150+
});
151+
}
152+
else{
153+
var errorMsg = document.createElement('h3');
154+
errorMsg.textContent = 'Sorry no items match that search';
155+
table.appendChild(errorMsg);
156+
}
157+
141158
}
142159
}
143160

src/handlers.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ const handlers = {
3838
public(req, res, endpoint) {
3939
fs.readFile(buildPath(endpoint), (err, file) => {
4040
if (err) {
41-
res.writeHead(500, { "Content-Type": "text/html" });
42-
res.end("<h1>Server Error</h1>");
41+
res.writeHead(404, { "Content-Type": "text/html" });
42+
res.end("<h1>Page not found</h1>");
4343
console.log("public error");
4444
} else {
4545
res.writeHead(200, {
@@ -62,7 +62,7 @@ const handlers = {
6262
} else {
6363
res.writeHead(200, "Content-type: application/json");
6464
res.end(JSON.stringify(result));
65-
}
65+
}
6666
});
6767
},
6868
requestItem(req, res) {

src/router.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ const router = (req, res) => {
55

66
if (endpoint === "") {
77
handlers.home(req, res);
8-
} else if (endpoint.includes("search?q=")) {
9-
handlers.search(req, res, endpoint);
8+
} else if (endpoint.startsWith("search?q=")) {
9+
handlers.search(req, res, endpoint); // some arguments maybe
1010
} else if (endpoint === "request-item") {
1111
handlers.requestItem(req, res);
1212
} else if (endpoint === "add-item") {

0 commit comments

Comments
 (0)