Skip to content

Commit e12816d

Browse files
authored
Merge pull request #1 from evanshortiss/main
[WIP] feat: ui polls products on an interval now
2 parents a99c85f + d5f0ee3 commit e12816d

13 files changed

+481
-37
lines changed

.dockerignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules
2+
Dockerfile*
3+
devfile.yaml

Dockerfile

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ FROM node:14
22
ENV COOLSTORE_GW_ENDPOINT=http://localhost:8090
33
WORKDIR /usr/src/app
44
COPY package*.json ./
5-
RUN npm install
5+
RUN npm ci --omit=dev
66
COPY . .
7+
RUN chmod -R a+rw /usr/src/app
8+
USER 1001
79
EXPOSE 8080
810
CMD [ "npm", "start" ]

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ Example:
3333
SECURE_COOLSTORE_GW_ENDPOINT=https://gateway-vertx.somewhere.tld
3434
```
3535

36+
The UI fetches products every 5 seconds by default. This can be changed by setting the `PRODUCT_REFRESH_INTERVAL` to a value represented in milliseconds, e.g `PRODUCT_REFRESH_INTERVAL=3000` for a 3 second refresh interval.
37+
3638
The outcome is an online store with a catalog of product items and an inventory of stock:
3739

3840
<img src="coolstore-web.png" width="600" alt="Coolstore Shop">

app/controllers/controllers.js

+28-17
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
angular.module('app')
44

55
.controller("HomeController",
6-
['$scope', '$http', '$filter', /*'Notifications',*/ 'catalog', 'Auth',
7-
function ($scope, $http, $filter, /*Notifications,*/ catalog, $auth) {
6+
['$scope', '$http', '$filter', '$timeout', 'COOLSTORE_CONFIG', /*'Notifications',*/ 'catalog', 'Auth',
7+
function ($scope, $http, $filter, $timeout, COOLSTORE_CONFIG, /*Notifications,*/ catalog, $auth) {
88

99
$scope.products = [];
1010

@@ -19,24 +19,35 @@ angular.module('app')
1919
$auth.login();
2020
};
2121

22-
23-
// initialize products
24-
catalog.getProducts().then(function (data) {
25-
if (data.error != undefined && data.error != "") {
26-
// Notifications.error("Error retrieving products: " + data.error);
27-
return;
28-
}
29-
$scope.products = data.map(function (el) {
30-
return {
31-
quantity: "1",
32-
product: el
22+
function refreshProducts () {
23+
// initialize products
24+
catalog.getProducts().then(function (data) {
25+
if (data.error != undefined && data.error != "") {
26+
// Notifications.error("Error retrieving products: " + data.error);
27+
return;
3328
}
29+
$scope.products = data
30+
.sort(function (p1, p2) {
31+
return parseInt(p1.itemId) > parseInt(p2.itemId) ? 1 : -1
32+
})
33+
.map(function (el) {
34+
return {
35+
quantity: "1",
36+
product: el
37+
}
38+
})
39+
40+
}, function (err) {
41+
// Notifications.error("Error retrieving products: " + err.statusText);
3442
})
35-
}, function (err) {
36-
// Notifications.error("Error retrieving products: " + err.statusText);
37-
});
38-
43+
.finally(function () {
44+
$timeout(function () {
45+
refreshProducts()
46+
}, COOLSTORE_CONFIG.PRODUCT_REFRESH_INTERVAL)
47+
});
48+
}
3949

50+
refreshProducts()
4051
}])
4152

4253
.controller("HeaderController",

app/coolstore.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
angular.module("app")
2-
.constant("COOLSTORE_CONFIG", {"API_ENDPOINT":"http://localhost:8090","SECURE_API_ENDPOINT":"secure-gateway-coolstore-undefined","SSO_ENABLED":false});
2+
.constant("COOLSTORE_CONFIG", {"API_ENDPOINT":"gateway-vertx-undefined","SECURE_API_ENDPOINT":"gateway-vertx-undefined","SSO_ENABLED":false,"PRODUCT_REFRESH_INTERVAL":5000});

app/services/catalog.js

+9-14
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
angular.module("app")
44

55
.factory('catalog', ['$http', '$q', 'COOLSTORE_CONFIG', 'Auth', '$location', function($http, $q, COOLSTORE_CONFIG, $auth, $location) {
6-
var factory = {}, products, baseUrl;
6+
var factory = {}, baseUrl;
77

88
if ($location.protocol() === 'https') {
99
baseUrl = (COOLSTORE_CONFIG.SECURE_API_ENDPOINT.startsWith("https://") ? COOLSTORE_CONFIG.SECURE_API_ENDPOINT : "https://" + COOLSTORE_CONFIG.SECURE_API_ENDPOINT + '.' + $location.host().replace(/^.*?\.(.*)/g,"$1")) + '/api/products';
@@ -13,19 +13,14 @@ angular.module("app")
1313

1414
factory.getProducts = function() {
1515
var deferred = $q.defer();
16-
if (products) {
17-
deferred.resolve(products);
18-
} else {
19-
$http({
20-
method: 'GET',
21-
url: baseUrl
22-
}).then(function(resp) {
23-
products = resp.data;
24-
deferred.resolve(resp.data);
25-
}, function(err) {
26-
deferred.reject(err);
27-
});
28-
}
16+
$http({
17+
method: 'GET',
18+
url: baseUrl
19+
}).then(function(resp) {
20+
deferred.resolve(resp.data);
21+
}, function(err) {
22+
deferred.reject(err);
23+
});
2924
return deferred.promise;
3025
};
3126

config/coolstore.config.js

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const { get } = require('env-var')
2+
13
var config = {
24
API_ENDPOINT: 'gateway-vertx-' + process.env.OPENSHIFT_BUILD_NAMESPACE,
35
SECURE_API_ENDPOINT: 'gateway-vertx-' + process.env.OPENSHIFT_BUILD_NAMESPACE,
@@ -17,4 +19,6 @@ if (process.env.SECURE_COOLSTORE_GW_ENDPOINT != null) {
1719
config.SECURE_API_ENDPOINT = process.env.SECURE_COOLSTORE_GW_SERVICE + '-' + process.env.OPENSHIFT_BUILD_NAMESPACE;
1820
}
1921

22+
config.PRODUCT_REFRESH_INTERVAL = get('PRODUCT_REFRESH_INTERVAL').default(5000).asIntPositive()
23+
2024
module.exports = config;

config/coolstore.config.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"COOLSTORE_CONFIG": {
33
"API_ENDPOINT": "localhost:8090",
44
"SECURE_API_ENDPOINT": "localhost:8443",
5-
"SSO_ENABLED": "false"
5+
"SSO_ENABLED": "false",
6+
"PRODUCT_REFRESH_INTERVAL": 5000
67
}
78
}

licenses/ENV-VAR_MIT.TXT

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2016, 2020 Evan Shortiss
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

licenses/licenses.html

+8-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<link rel="stylesheet" type="text/css" href="licenses.css">
55
</head>
66
<body>
7-
<h2>web</h2>
7+
<h2>web-nodejs</h2>
88
<table>
99
<tr>
1010
<th>Package Group</th>
@@ -36,6 +36,13 @@ <h2>web</h2>
3636
</tr>
3737
<tr>
3838
<td>N/A</td>
39+
<td>env-var</td>
40+
<td>7.3.0</td>
41+
<td>http:&#x2F;&#x2F;www.opensource.org&#x2F;licenses&#x2F;MIT</td>
42+
<td><a href=ENV-VAR_MIT.TXT>ENV-VAR_MIT.TXT</a></td>
43+
</tr>
44+
<tr>
45+
<td>N/A</td>
3946
<td>express</td>
4047
<td>4.16.4</td>
4148
<td>http:&#x2F;&#x2F;www.opensource.org&#x2F;licenses&#x2F;MIT</td>

licenses/licenses.xml

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version='1.0'?>
22
<licenseSummary>
3-
<project>web</project>
3+
<project>web-nodejs</project>
44
<version>1.0.0</version>
55
<license>Apache-2.0</license>
66
<dependencies>
@@ -34,6 +34,16 @@
3434
</license>
3535
</licenses>
3636
</dependency>
37+
<dependency>
38+
<packageName>env-var</packageName>
39+
<version>7.3.0</version>
40+
<licenses>
41+
<license>
42+
<name>MIT License</name>
43+
<url>http://www.opensource.org/licenses/MIT</url>
44+
</license>
45+
</licenses>
46+
</dependency>
3747
<dependency>
3848
<packageName>express</packageName>
3949
<version>4.16.4</version>

0 commit comments

Comments
 (0)