-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
233 lines (206 loc) · 8.1 KB
/
index.html
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>GoR Admin</title>
<link rel="stylesheet" href="css/bootstrap.css">
</head>
<body>
<script>
var jwt = localStorage.getItem("JWT");
if (jwt == ""){
window.location.replace("login.html");
}
</script>
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<div class="container-fluid">
<a class="navbar-brand" href="index.html">GoR Admin</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="index.html">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="discounts.html">Discounts</a>
</li>
<li class="nav-item">
<a class="nav-link" href="orders.html">Orders and Statistics</a>
</li>
<li class="nav-item">
<a class="nav-link" href="categories.html">Categories</a>
</li>
</ul>
<form class="d-flex" method="GET" action="/index.html">
<input class="form-control me-2" type="search" placeholder="Search Products" aria-label="Search" name="search">
<button class="btn btn-outline-success" type="submit">Search</button>
</form>
</div>
<a class="nav-link float-right link-light" id="logout" href="#" tabindex="-1" aria-disabled="true">Log out</a>
</div>
</nav>
<div class="container mt-5">
<div class="row">
<div class="col-12">
<h3 class="" id = "search-title">Welcome to GoR Admin!</h3>
</div>
</div>
<div class="row my-3" id="addProductCard">
<div class="col-12">
<div class="card w-100 p-2 pt-3" id = "add-card" style="width: 18rem;">
<h4 class="card-title" id = "search-title">Click here to add a product! <a href="product.html" class="btn btn-primary btn-sm">Add Product!</a></h4>
</div>
</div>
</div>
<div class="row" id="search-row">
<div class="col-12">
<table class="table table-hover" id="search-table" style = "visibility: hidden;">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">image</th>
<th scope="col">Product Name</th>
<th scope="col"># in stock</th>
<th scope="col">Price</th>
<th scope="col">discount</th>
<th scope="col"></th>
</tr>
</thead>
<tbody id = "search-body">
</tbody>
</table>
</div>
</div>
</div>
<div class="container mt-1">
<div class="row">
<div class="col-12">
<div class="card w-100 text-white bg-danger" id="stock-card" style="width: 18rem;">
<div class="card-body">
<h3 class="card-title">Out-of-Stock Items</h3>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-12">
<table class="table table-hover" id="stock-table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">image</th>
<th scope="col">Product Name</th>
<th scope="col"># in stock</th>
<th scope="col">Price</th>
<th scope="col">discount</th>
<th scope="col"></th>
</tr>
</thead>
<tbody id = "stock-body">
</tbody>
</table>
</div>
</div>
</div>
<script src="js/logout.js"></script>
<script>
const proxy = "https://fast-sea-38093.herokuapp.com/"
const url = "https://grocery-on-rails.herokuapp.com";
async function productSearch(value) {
const search_body = {
"sort" : "relevance",
}
const options = {
method: 'POST',
headers: {
'Authorization': 'Bearer ' + jwt,
'Content-Type': 'application/json'
},
body: JSON.stringify(search_body)
};
const response = await fetch(`${url}/search/${value}`, options);
const data = await response.json();
return data;
}
async function getEmpty(){
const options = {
method: 'GET',
headers: {
'Authorization': 'Bearer ' + jwt
}
};
const response = await fetch(url + "/admin/empty", options);
const data = await response.json();
return data;
}
function getParameter( parameterName ){
var parameters = new URLSearchParams( window.location.search );
return parameters.get( parameterName ); // Returns null if the query string is empty or the parameter is not found.
}
function productRow(i, product){
if (product.stock == 0){
row = '<tr class="table-danger">';
}else if (product.stock <= 5){
row = '<tr class="table-warning">';
}else{
row = '<tr class="table-success">';
}
return row + `<th scope="row">${i}</th>\
<td><img src="${product.image}" height="50"></td>
<td>${product.name}</td>\
<td>${product.stock}</td>\
<td>AED${product.price}</td>\
<td>AED${product.discount}</td>\
<td><a class="btn btn-primary" href="product.html?id=${product._id.$oid}" role="button">Edit</a></td>\
</tr>`;
}
function buildTable(data){
let body = "";
let i = 1;
for (let product of data){
body += productRow(i, product);
i++;
}
return body;
}
function printSearch(value, data)
{
const title = `There are ${data.length} Search results for "${value}":`;
document.getElementById("search-title").innerHTML = title;
//document.getElementById("search-card").style.visibility = "visible";
body = buildTable(data);
document.getElementById("search-body").innerHTML = body;
document.getElementById("search-table").style.visibility = "visible";
}
async function printEmpty(){
const data = await getEmpty();
body = buildTable(data);
const e = document.getElementById("stock-body");
if(e){
e.innerHTML = body;
}
}
async function checkSearch(){
let value = getParameter("search");
if (value){
document.getElementById("stock-table").remove();
document.getElementById("stock-card").remove();
document.getElementById("addProductCard").remove()
const data = await productSearch(value);
printSearch(value, data);
}
else{
document.getElementById("search-row").remove();
}
}
checkSearch();
printEmpty();
</script>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js" integrity="sha384-SR1sx49pcuLnqZUnnPwx6FCym0wLsk5JZuNx2bPPENzswTNFaQU1RDvt3wT4gWFG" crossorigin="anonymous"></script>
<script src="js/bootstrap.js"></script>
</body>
</html>