From afba8f466210a9f5e9a0c44c149c6e66161c7dbe Mon Sep 17 00:00:00 2001 From: Ebenezer Date: Sat, 10 Nov 2018 16:57:08 +0100 Subject: [PATCH 1/4] Get products from api using fetch --- UI/assets/js/products.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 UI/assets/js/products.js diff --git a/UI/assets/js/products.js b/UI/assets/js/products.js new file mode 100644 index 0000000..61f48b3 --- /dev/null +++ b/UI/assets/js/products.js @@ -0,0 +1,15 @@ +getProducts = () => { + const options = { + headers: new Headers({ + 'Content-Type': 'application/json', + 'accessstoken': localStorage.accesstoken, + }), + }; + fetch('https://newstoremanager.herokuapp.com/api/v1/products', options) + .then(res => res.json()) + .then((data) => { + let output = '

Products

'; + console.log(data); + }); + console.log(localStorage.accesstoken); +}; From fdb9781f3f5cf420f165572b51903bf491b26e94 Mon Sep 17 00:00:00 2001 From: Ebenezer Date: Sat, 10 Nov 2018 18:07:14 +0100 Subject: [PATCH 2/4] ch[#161861252] Connect products to api --- UI/assets/js/products.js | 37 +++++++++++++++++++++++++++---------- UI/pages/products.html | 8 +++++--- 2 files changed, 32 insertions(+), 13 deletions(-) diff --git a/UI/assets/js/products.js b/UI/assets/js/products.js index 61f48b3..149bb21 100644 --- a/UI/assets/js/products.js +++ b/UI/assets/js/products.js @@ -1,15 +1,32 @@ -getProducts = () => { - const options = { - headers: new Headers({ +const getProducts = () => { + fetch('https://newstoremanager.herokuapp.com/api/v1/products', { + headers: { 'Content-Type': 'application/json', - 'accessstoken': localStorage.accesstoken, - }), - }; - fetch('https://newstoremanager.herokuapp.com/api/v1/products', options) + accesstoken: localStorage.accesstoken, + }, + }) .then(res => res.json()) .then((data) => { - let output = '

Products

'; - console.log(data); + let output = null; + data.forEach((user) => { + console.log(user.productimage); + output += ` +
+
+ + + + +

${user.productname}

+

${user.price}

+
+ +
+
+ `; + }); + document.getElementById('products-list').innerHTML = output; }); - console.log(localStorage.accesstoken); }; + +/* document.getElementById('products-list').addEventListener('mouseover', getProducts); */ diff --git a/UI/pages/products.html b/UI/pages/products.html index 1ec0bef..f9d4a8d 100644 --- a/UI/pages/products.html +++ b/UI/pages/products.html @@ -13,7 +13,7 @@ - +
-
+ +
-
+ + From 0e465b97b146925786085c250da15c17c09d5a3c Mon Sep 17 00:00:00 2001 From: Ebenezer Date: Sat, 10 Nov 2018 18:18:51 +0100 Subject: [PATCH 3/4] move onload event to js file --- UI/assets/js/products.js | 2 +- UI/pages/products.html | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/UI/assets/js/products.js b/UI/assets/js/products.js index 149bb21..af108c3 100644 --- a/UI/assets/js/products.js +++ b/UI/assets/js/products.js @@ -29,4 +29,4 @@ const getProducts = () => { }); }; -/* document.getElementById('products-list').addEventListener('mouseover', getProducts); */ +window.onload = getProducts(); diff --git a/UI/pages/products.html b/UI/pages/products.html index f9d4a8d..885fd95 100644 --- a/UI/pages/products.html +++ b/UI/pages/products.html @@ -13,7 +13,7 @@ - +
- +