From f61aa337da96e2621019d8ee3cf11cca7ee70101 Mon Sep 17 00:00:00 2001 From: Max Hunt Date: Mon, 27 Apr 2020 15:39:53 +0100 Subject: [PATCH] Update --- functions/index.js | 22 +++++++++++++++++++++ functions/views/js/fdb.js | 35 ++++++++++++++++++++++++++++++++++ functions/views/my-product.ejs | 6 ++++-- server.js | 22 +++++++++++++++++++++ 4 files changed, 83 insertions(+), 2 deletions(-) diff --git a/functions/index.js b/functions/index.js index 2f3d673..7101f66 100644 --- a/functions/index.js +++ b/functions/index.js @@ -196,4 +196,26 @@ app.post('/place-on-sale-api', function(request, responce){ } }) +app.post('/ship-api', function(request, responce){ + if (request.body.item && request.body.user) { + admin.auth().verifyIdToken(request.body.user) + .then(function(decodedToken) { + let uid = decodedToken.uid; + let product = request.body.item + var productRef = database.ref("/products/"+product) + productRef.update({status:"shipped"}) + .then(function(){ + responce.send({success:true, error:null}) + }) + .catch(e => {console.log(e)}) + }) + .catch(function(error) { + responce.send({ error: 'invalid auth token' }) + }); + } + else { + responce.send({ error: 'invalid data' }) + } +}) + exports.application = functions.https.onRequest(app) \ No newline at end of file diff --git a/functions/views/js/fdb.js b/functions/views/js/fdb.js index af7d8e5..68496f3 100644 --- a/functions/views/js/fdb.js +++ b/functions/views/js/fdb.js @@ -229,6 +229,41 @@ function place_item_on_sale() { .catch(e => {console.log(e)}) } +function ship_item() { + firebase.auth().currentUser.getIdToken().then(function(idToken) { + var item_id_element = document.getElementById("productID") + var item_id = item_id_element.getAttribute("product_id") + var place_on_sale_button = document.getElementById("shipBtn") + const url = 'ship-api' + let data = { + item: item_id, + user: idToken + } + let postData = { + method: 'POST', + body: JSON.stringify(data), + headers: {'Content-Type': 'application/json'} + } + fetch(url, postData) + .then(response => response.json()) + .then(function(result){ + console.log(result) + if (result.success == true) { + place_on_sale_button.setAttribute("style", "background: #00DD00; width:200px;") + place_on_sale_button.innerHTML = "Item shipped!" + place_on_sale_button.disabled = true + } + else { + place_on_sale_button.setAttribute("style", "background: red;") + place_on_sale_button.disabled = true + place_on_sale_button.innerHTML = "ERROR" + console.log(result.error) + } + }) + }) + .catch(e => {console.log(e)}) +} + function my_product(productID) { firebase.auth().currentUser.getIdToken().then(function(idToken) { window.location.replace("my-product?uToken=" + idToken + '&productid=' + productID) diff --git a/functions/views/my-product.ejs b/functions/views/my-product.ejs index d6b8bf8..cbeeb04 100644 --- a/functions/views/my-product.ejs +++ b/functions/views/my-product.ejs @@ -71,11 +71,13 @@

Status: <%= item.status %>

Price: £<%= item.price %>

-
+ <% var shipbtnStyle = ""%> + <% if (item.status == "shipped") {shipbtnStyle = "visibility:hidden;"} else {shipbtnStyle = "visibility:all;"}%> +
>
<% var sellbtnStyle = ""%> <% if (item.status == "sold") {sellbtnStyle = "visibility:all;"} else {sellbtnStyle = "visibility:hidden;"}%>
>
-
+
>
diff --git a/server.js b/server.js index 261b85b..c4493f1 100644 --- a/server.js +++ b/server.js @@ -202,6 +202,28 @@ app.post('/place-on-sale-api', function(request, responce){ } }) +app.post('/ship-api', function(request, responce){ + if (request.body.item && request.body.user) { + admin.auth().verifyIdToken(request.body.user) + .then(function(decodedToken) { + let uid = decodedToken.uid; + let product = request.body.item + var productRef = database.ref("/products/"+product) + productRef.update({status:"shipped"}) + .then(function(){ + responce.send({success:true, error:null}) + }) + .catch(e => {console.log(e)}) + }) + .catch(function(error) { + responce.send({ error: 'invalid auth token' }) + }); + } + else { + responce.send({ error: 'invalid data' }) + } +}) + var port = 5000 app.listen(port, function() {