Update
This commit is contained in:
parent
6ddc031188
commit
c58b9ef1d8
@ -160,7 +160,32 @@ app.post('/purchase-api', function(request, responce){
|
||||
let product = request.body.item
|
||||
var productRef = database.ref("/products/"+product)
|
||||
productRef.update({owner:uid, status:"sold"})
|
||||
responce.send({success:true, error:null})
|
||||
.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' })
|
||||
}
|
||||
})
|
||||
|
||||
app.post('/place-on-sale-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:"on-sale"})
|
||||
.then(function(){
|
||||
responce.send({success:true, error:null})
|
||||
})
|
||||
.catch(e => {console.log(e)})
|
||||
})
|
||||
.catch(function(error) {
|
||||
responce.send({ error: 'invalid auth token' })
|
||||
|
||||
@ -140,17 +140,17 @@ function purchaseItem() {
|
||||
firebase.auth().currentUser.getIdToken().then(function(idToken) {
|
||||
var item_id_element = document.getElementById("productID")
|
||||
var item_id = item_id_element.getAttribute("product_id")
|
||||
const url = 'purchase-api';
|
||||
const url = 'purchase-api'
|
||||
// The data we are going to send in our request
|
||||
let data = {
|
||||
item: item_id,
|
||||
user: idToken
|
||||
item: item_id,
|
||||
user: idToken
|
||||
}
|
||||
// The parameters we are gonna pass to the fetch function
|
||||
let postData = {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(data),
|
||||
headers: {'Content-Type': 'application/json'}
|
||||
method: 'POST',
|
||||
body: JSON.stringify(data),
|
||||
headers: {'Content-Type': 'application/json'}
|
||||
}
|
||||
fetch(url, postData)
|
||||
.then(response => response.json())
|
||||
@ -167,8 +167,6 @@ function purchaseItem() {
|
||||
document.getElementById("api-response").innerHTML = "Error: " + result.error
|
||||
document.getElementById("api-response").setAttribute("style", "display: block")
|
||||
}
|
||||
//check return value
|
||||
//update element accordingly
|
||||
})
|
||||
})
|
||||
}
|
||||
@ -178,6 +176,9 @@ function purchaseItem() {
|
||||
}
|
||||
|
||||
}
|
||||
else if (purchaseBtn.innerHTML == "Item Purchased, Congratulations!") {
|
||||
window.location.replace("home")
|
||||
}
|
||||
else {
|
||||
purchaseBtn.disabled = true
|
||||
}
|
||||
@ -188,15 +189,48 @@ function edit_item() {
|
||||
firebase.auth().currentUser.getIdToken().then(function(idToken) {
|
||||
var item_id_element = document.getElementById("productID")
|
||||
var item_id = item_id_element.getAttribute("product_id")
|
||||
console.log("edit-item?uToken=" + idToken + '&productid=' + item_id)
|
||||
window.location.replace("edit-item?uToken=" + idToken + '&productid=' + item_id)
|
||||
})
|
||||
.catch(e => {console.log(e)})
|
||||
}
|
||||
|
||||
function place_item_on_sale() {
|
||||
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("sellBtn")
|
||||
const url = 'place-on-sale-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 on sale!"
|
||||
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) {
|
||||
console.log("my-products?uToken=" + idToken + '&productid=' + productID)
|
||||
window.location.replace("my-product?uToken=" + idToken + '&productid=' + productID)
|
||||
})
|
||||
.catch(e => {console.log(e)})
|
||||
|
||||
@ -65,16 +65,16 @@
|
||||
<div class="product-page-img"><img class="dyn-img" src="media/noimage.png" title="<%= item.img %>"></div>
|
||||
<div class="product-page-info">
|
||||
<h2><%= item.name %> </h2>
|
||||
<p>Seller <%= item.owner %></p>
|
||||
<p>Seller: You</p>
|
||||
<p>Location: <%= item.location %></p>
|
||||
<p>In stock: <%= item.stock %></p>
|
||||
<% var publicField %>
|
||||
<% if (item.public == "") {publicField = "On sale"} else {publicField = "Hidden"}%>
|
||||
<p>Status: <%= publicField %></p>
|
||||
<p>Status: <%= item.status %></p>
|
||||
<p>Price: £<%= item.price %></p>
|
||||
<div class="product-mgmt-buttons">
|
||||
<div class="edit-btn"><button onclick={edit_item()} id="editBtn">Edit</button> </div>
|
||||
<div class="sell-btn"><button onclick={y()} id="sellBtn">Sell</button> </div>
|
||||
<% var sellbtnStyle = ""%>
|
||||
<% if (item.status == "sold") {sellbtnStyle = "visibility:all;"} else {sellbtnStyle = "visibility:hidden;"}%>
|
||||
<div class="sell-btn" style=<%= sellbtnStyle%>><button onclick={place_item_on_sale()} id="sellBtn">Sell</button> </div>
|
||||
<div class="ship-btn"><button onclick={z()} id="shipBtn">Ship</button> </div>
|
||||
</div>
|
||||
|
||||
|
||||
27
server.js
27
server.js
@ -166,7 +166,32 @@ app.post('/purchase-api', function(request, responce){
|
||||
let product = request.body.item
|
||||
var productRef = database.ref("/products/"+product)
|
||||
productRef.update({owner:uid, status:"sold"})
|
||||
responce.send({success:true, error:null})
|
||||
.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' })
|
||||
}
|
||||
})
|
||||
|
||||
app.post('/place-on-sale-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:"on-sale"})
|
||||
.then(function(){
|
||||
responce.send({success:true, error:null})
|
||||
})
|
||||
.catch(e => {console.log(e)})
|
||||
})
|
||||
.catch(function(error) {
|
||||
responce.send({ error: 'invalid auth token' })
|
||||
|
||||
Loading…
Reference in New Issue
Block a user