Skip to content

Commit 9b4e7af

Browse files
committed
Added basic field validation
1 parent 1113a52 commit 9b4e7af

4 files changed

Lines changed: 53 additions & 4 deletions

File tree

api/products-api.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
module.exports = function(productService) {
23

34
async function all(req, res) {

public/client.html

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@
1515
<th>Product name</th>
1616
<th>Category name</th>
1717
<th>Price</th>
18+
<th></th>
1819
</tr>
1920
{{#each productList}}
2021
<tr>
2122
<td>{{description}}</td>
2223
<td>{{category_description}}</td>
2324
<td>{{price}}</td>
25+
<td><button onclick="editProduct({{id}})" >Edit</button></td>
2426
</tr>
2527
{{/each}}
2628
</table>
@@ -33,15 +35,25 @@
3335
{{/each}}
3436
</script>
3537

38+
<script type="text/x-template" class="errorsTemplate">
39+
<ul>
40+
{{#each errors}}
41+
<li>{{this}}</li>
42+
{{/each}}
43+
</ul>
44+
</script>
45+
3646
</head>
3747
<body>
3848
<div id="container">
3949
<h1>Products via API</h1>
50+
4051
<div class="products">
41-
4252
</div>
4353
<div>
4454
<h1>Add product</h1>
55+
<div class="errors">
56+
</div>
4557
<div>
4658
<label for="productName">Name</label>
4759
<input type="text" name="productName" class="productName" >

public/client.js

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
document.addEventListener('DOMContentLoaded', function() {
22

3+
function compileTemplate(selector) {
4+
let template = document.querySelector(selector);
5+
let templateInstance = Handlebars.compile(template.innerHTML);
6+
return templateInstance;
7+
}
8+
39
let productListTemplate = document.querySelector('.productListTemplate');
410
let categoryListTemplate = document.querySelector('.categoryListTemplate');
511

612
let categoryListTemplateInstance = Handlebars.compile(categoryListTemplate.innerHTML);
713
let productListTemplateInstance = Handlebars.compile(productListTemplate.innerHTML);
8-
14+
let errorsTemplateInstance = compileTemplate('.errorsTemplate');
15+
916
let productsElem = document.querySelector('.products');
1017
let categoriesElem = document.querySelector('.categories');
18+
let errorsElem = document.querySelector('.errors');
1119

1220
let productNameElem = document.querySelector('.productName');
1321
let priceElem = document.querySelector('.price');
@@ -56,7 +64,20 @@ document.addEventListener('DOMContentLoaded', function() {
5664
let category_id = categoryIdElem.value;
5765
let price = priceElem.value;
5866

59-
productService.addProduct({
67+
let errors = [];
68+
if (!description) {
69+
errors.push('Enter a product description');
70+
}
71+
if (!category_id) {
72+
errors.push('Select a Category');
73+
}
74+
if (!description) {
75+
errors.push('Enter a price');
76+
}
77+
78+
if (errors.length === 0) {
79+
errorsElem.innerHTML = '';
80+
productService.addProduct({
6081
category_id,
6182
description,
6283
price
@@ -68,13 +89,23 @@ document.addEventListener('DOMContentLoaded', function() {
6889
.catch(function(err){
6990
alert(err);
7091
});
92+
}
93+
else {
94+
errorsElem.innerHTML = errorsTemplateInstance({errors});
95+
}
96+
7197
});
7298

7399
showCategoryDropdown();
74100
showProducts();
75101

102+
76103
});
77104

105+
function editProduct(id) {
106+
alert(id);
107+
}
108+
78109
function ProductService() {
79110
function getProducts(){
80111
return axios.get('/api/products')
@@ -85,6 +116,7 @@ function ProductService() {
85116
}
86117

87118
return {
119+
addProduct,
88120
getProducts
89121
}
90122
}

public/style.css

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,8 @@ table {
102102

103103
th, td {
104104
padding: 15px;
105-
}
105+
}
106+
107+
.errors {
108+
color: red;
109+
}

0 commit comments

Comments
 (0)