1 line
15 KiB
JavaScript
1 line
15 KiB
JavaScript
function checkFields(){return!!(document.getElementById("pName").value&&document.getElementById("pDesc").value&&document.getElementById("pLoc").value&&document.getElementById("pPrice").value&&document.getElementById("imgInput").files[0])}async function productSubmitEventHandler(){const e=document.getElementById("pSend");if("Submit!"==e.innerHTML)if(checkFields()){e.innerHTML="Uploading...",e.disabled=!0;let t={id:1,name:"",desc:"",img:"default.png",location:"",stock:1,status:"pending",price:0,owner:"",holder:""};const n=document.getElementById("pName"),r=document.getElementById("pDesc"),d=document.getElementById("pLoc"),o=document.getElementById("pStock"),s=document.getElementById("pPrice");let i=document.getElementById("imgInput"),l=firebase.auth().currentUser.uid;t.id=999,t.name=n.value,t.desc=r.value,t.location=d.value,o.value&&(t.stock=o.value),t.price=s.value,t.owner=l,t.holder=l;let u=i.files[0],a={name:i.files[0].name,b64:null};firebase.auth().currentUser.getIdToken().then(function(n){let r={authToken:n,product:t,image:a},d={method:"POST",body:JSON.stringify(r),headers:{"Content-Type":"application/json"}};fetch("new-product-api",d).then(e=>e.json()).then(function(t){if(t.success){firebase.storage().ref(t.imgRef).put(u).then(function(){e.innerHTML="Done! Go Home...",e.disabled=!1,e.setAttribute("style","background:#00FF55; transition:0.5s; width:162%;")}).catch(t=>{e.innerHTML="Error...",e.setAttribute("style","color:red;"),document.getElementById("api-response").innerHTML="Error: "+t,document.getElementById("api-response").setAttribute("style","display: block")})}else e.innerHTML="Error...",e.setAttribute("style","color:red;"),document.getElementById("api-response").innerHTML="Error: "+t.error,document.getElementById("api-response").setAttribute("style","display: block")})})}else document.getElementById("api-response").innerHTML="Fill in all required fields! (try readding the image)",document.getElementById("api-response").setAttribute("style","display: block");else window.location.replace("home")}function productUpdateEventHandler(){const e=document.getElementById("pSend");if("Update"==e.innerHTML){const t=document.getElementById("pName"),n=document.getElementById("pDesc"),r=document.getElementById("pLoc"),d=document.getElementById("pStock"),o=document.getElementById("pPrice"),s=document.getElementById("productID").getAttribute("product_id");e.innerHTML="Updating...",e.disabled=!0,updates={name:t.value,desc:n.value,location:r.value,stock:d.value,price:o.value},firebase.auth().currentUser.getIdToken().then(function(t){let n={authToken:t,product:updates,productID:s},r={method:"POST",body:JSON.stringify(n),headers:{"Content-Type":"application/json"}};fetch("update-product-api",r).then(e=>e.json()).then(function(t){t.success?(e.setAttribute("style","width: 310px; background: #00DD00;cursor: pointer;"),e.innerHTML="Done!, Go Home...",e.disabled=!1):(e.innerHTML="Error...",e.setAttribute("style","color:red;"),document.getElementById("api-response").innerHTML="Error: "+t.error,document.getElementById("api-response").setAttribute("style","display: block"))})})}else window.location.replace("home")}function updateImgs(){const e=firebase.storage().ref();var t=document.getElementsByClassName("dyn-img");Array.from(t).forEach(t=>{e.child(t.title).getDownloadURL().then(function(e){t.src=e}).catch(e=>{console.log(e.message)})})}function requestItem(){const e=document.getElementById("req-btn"),t=document.getElementById("bMsg");"Request!"==e.innerHTML?firebase.auth().currentUser?(e.setAttribute("style","background: #87e5ff; width:410px;"),e.innerHTML="Send?",t.setAttribute("style","display: block;")):(e.innerHTML="Please sign in",e.setAttribute("style","background: #FFa5af; width:300px;")):"Send?"==e.innerHTML?firebase.auth().currentUser?firebase.auth().currentUser.getIdToken().then(function(n){e.innerHTML="Sending...",e.disabled=!0;let r=t.value;let d={prodID:document.getElementById("productID").getAttribute("product_id"),authToken:n,usrMsg:r},o={method:"POST",body:JSON.stringify(d),headers:{"Content-Type":"application/json"}};fetch("request-api",o).then(e=>e.json()).then(function(t){console.log(t),1==t.success?(e.setAttribute("style","background: #00DD00; width:410px; transition: 0.7s;"),e.innerHTML="Done! Go Home",e.disabled=!1):(e.setAttribute("style","background: red;"),e.disabled=!0,e.innerHTML="ERROR",document.getElementById("api-response").innerHTML="Error: "+t.error,document.getElementById("api-response").setAttribute("style","display: block"))})}):e.innerHTML="Please sign in":window.location.replace("home")}function edit_item(){firebase.auth().currentUser.getIdToken().then(function(e){var t=document.getElementById("productID").getAttribute("product_id");window.location.replace("edit-item?uToken="+e+"&productid="+t)}).catch(e=>{console.log(e)})}function toggle_sale(){firebase.auth().currentUser.getIdToken().then(function(e){let t=document.getElementById("productID").getAttribute("product_id"),n=document.getElementById("productID").getAttribute("product_status"),r=document.getElementById("toggleSaleBtn");r.disabled=!0;var d=!0;"on-sale"==n&&(d=!1);let o={prodID:t,authToken:e,targetState:d},s={method:"POST",body:JSON.stringify(o),headers:{"Content-Type":"application/json"}};fetch("toggle-sale-api",s).then(e=>e.json()).then(function(e){if(console.log(e),1==e.success){r.setAttribute("style","background: #00DD00; transition: 0.5s;"),r.innerHTML="Done!";let e=document.getElementById("soldBtn"),t=document.getElementById("editBtn");e.disabled=!0,t.disabled=!0}else r.setAttribute("style","background: red;"),r.innerHTML="ERROR",document.getElementById("api-response").innerHTML="Error: "+e.error,document.getElementById("api-response").setAttribute("style","display: block")}).catch(e=>{r.setAttribute("style","background: red;"),r.innerHTML="ERROR",document.getElementById("api-response").innerHTML="Error: "+e,document.getElementById("api-response").setAttribute("style","display: block")})})}function mark_as_sold(){let t=document.getElementById("soldBtn");"Mark Sold"==t.innerHTML?(t.innerHTML="Confirm?",t.setAttribute("style","background:white;")):"Confirm?"==t.innerHTML?firebase.auth().currentUser.getIdToken().then(function(n){let r={prodID:document.getElementById("productID").getAttribute("product_id"),authToken:n},d={method:"POST",body:JSON.stringify(r),headers:{"Content-Type":"application/json"}};fetch("sold-api",d).then(e=>e.json()).then(function(n){if(1==n.success){t.setAttribute("style","background: #00DD00; width:200px;"),t.innerHTML="Sold!",t.disabled=!1;let e=document.getElementById("toggleSaleBtn"),n=document.getElementById("editBtn");e.disabled=!0,n.disabled=!0}else t.setAttribute("style","background: red;"),t.innerHTML="ERROR",document.getElementById("api-response").innerHTML="Error: "+e,document.getElementById("api-response").setAttribute("style","display: block")}).catch(e=>{t.setAttribute("style","background: red;"),t.innerHTML="ERROR",document.getElementById("api-response").innerHTML="Error: "+e,document.getElementById("api-response").setAttribute("style","display: block")})}):window.location.replace("home")}function my_product(e){firebase.auth().currentUser.getIdToken().then(function(t){window.location.replace("my-product?uToken="+t+"&productid="+e)}).catch(e=>{console.log(e)})}function my_products(){firebase.auth().currentUser.getIdToken().then(function(e){window.location.replace("my-products?uToken="+e)}).catch(e=>{console.log(e)})}function showShipField(){document.getElementById("shippingaddressbutton").setAttribute("style","display:none;"),document.getElementById("shippingaddressinput").setAttribute("style","display:block;"),showUserShippingAddress()}function showUserShippingAddress(){firebase.auth().currentUser.getIdToken().then(function(e){let t={authToken:e},n={method:"POST",body:JSON.stringify(t),headers:{"Content-Type":"application/json"}};fetch("shipping-adress-get-api",n).then(e=>e.json()).then(function(e){e.success?document.getElementById("shippingaddressinput").value=e.address:(document.getElementById("api-response").innerHTML="Error: "+e.error,document.getElementById("api-response").setAttribute("style","display: block"),document.getElementById("shippingaddressinput").value="",document.getElementById("shippingaddressinput").placeholder="Can't retrieve current shipping address")})})}function addShippingAddress(){let e=document.getElementById("shippingaddressinput").value;firebase.auth().currentUser.getIdToken().then(function(t){let n={authToken:t,address:e},r={method:"POST",body:JSON.stringify(n),headers:{"Content-Type":"application/json"}};fetch("shipping-adress-edit-api",r).then(e=>e.json()).then(function(e){e.success?(document.getElementById("shippingaddressinput").value="",document.getElementById("shippingaddressinput").placeholder="Success!"):(document.getElementById("api-response").innerHTML="Error: "+e.error,document.getElementById("api-response").setAttribute("style","display: block"),document.getElementById("shippingaddressinput").value="",document.getElementById("shippingaddressinput").placeholder="ERROR!")})})}function search(e){window.location.replace("search?search="+e)}function u_r_submit(){let e=document.getElementById("u-r-btn");"Submit!"==e.innerHTML?(e.disabled=!0,firebase.auth().currentUser?firebase.auth().currentUser.getIdToken().then(function(t){let n=document.getElementById("f-name"),r=document.getElementById("l-name"),d=document.getElementById("u-address"),o=n.value,s=r.value,i=d.value,l=document.getElementById("imgInput"),u=l.files[0].name,a=l.files[0],c={authToken:t,data:{fName:o,lName:s,address:i,pPic:u}},p={method:"POST",body:JSON.stringify(c),headers:{"Content-Type":"application/json"}};fetch("register-new-user-api",p).then(e=>e.json()).then(function(t){if(t.success){let n=firebase.storage().ref(t.imgRef);e.innerHTML="Uploading...",e.disabled=!0,n.put(a).then(function(){e.setAttribute("style","background: #00FF00; width: 100%; transition: 0.5s;"),e.innerHTML="Success! Go Home...",e.disabled=!1}).catch(t=>{e.setAttribute("style","background: red;"),e.innerHTML="ERROR",e.disabled=!1,document.getElementById("api-response").innerHTML="Error: "+t,document.getElementById("api-response").setAttribute("style","display: block")})}else e.setAttribute("style","background: red;"),e.innerHTML="ERROR",e.disabled=!1,document.getElementById("api-response").innerHTML="Error: "+t.error,document.getElementById("api-response").setAttribute("style","display: block")})}).catch(t=>{e.setAttribute("style","background: red;"),e.innerHTML="AUTH!!!",document.getElementById("api-response").innerHTML="Error: "+t,document.getElementById("api-response").setAttribute("style","display: block")}):(e.setAttribute("style","background: red;"),e.disabled=!1,e.innerHTML="ERROR!",document.getElementById("api-response").innerHTML="Error: Please log in",document.getElementById("api-response").setAttribute("style","display: block"))):window.location.replace("home")}function sendApprove(e){sendAR(!0,e)}function sendReject(e){sendAR(!1,e)}function sendAR(e,t){let n=document.getElementById(t);firebase.auth().currentUser.getIdToken().then(function(r){let d={authToken:r,prodID:t,approved:e},o={method:"POST",body:JSON.stringify(d),headers:{"Content-Type":"application/json"}};fetch("product-ar-api",o).then(e=>e.json()).then(function(e){e.success?n.setAttribute("style","display:none;"):(n.setAttribute("style","background:red; transition:0.3s;"),console.log(e.error))})})}function initFileDropListner(){let e=document.getElementById("dropzone");if(e){let t=document.getElementById("imgInput");e.ondragover=function(){return this.className="dropzone dragover",!1},e.ondragleave=function(){return this.className="dropzone",!1},e.onclick=function(){t.click()},t.onchange=function(e){fileAddSequence(e.target.files[0])},e.ondrop=function(e){e.preventDefault(),this.className="dropzone",fileAddSequence(e.dataTransfer.files[0])}}}function fileAddSequence(e){let t=document.getElementById("imgInput");if(["image/jpeg","image/png","image/gif"].includes(e.type)){if(e.size/1024/1024<10){t.files[0]=e;let n=document.getElementById("imgPreview"),r=document.getElementById("svgElement");n.src=URL.createObjectURL(e),n.setAttribute("style","display: block;"),r.setAttribute("style","display: none;")}else document.getElementById("api-response").innerHTML="Files under 10MB please...",document.getElementById("api-response").setAttribute("style","display: block")}else document.getElementById("api-response").innerHTML="Accepted formats: jpeg/jpg, png, gif",document.getElementById("api-response").setAttribute("style","display: block")}function contactSeller(){const e=document.getElementById("contact-btn"),t=document.getElementById("sMsg");"Contact"==e.innerHTML?firebase.auth().currentUser?(e.setAttribute("style","background: #87e5ff; width:410px;"),e.innerHTML="Send?",t.setAttribute("style","display: block;")):(e.innerHTML="Please sign in",e.setAttribute("style","background: #FFa5af; width:300px;")):"Send?"==e.innerHTML?firebase.auth().currentUser?firebase.auth().currentUser.getIdToken().then(function(n){e.innerHTML="Sending...",e.disabled=!0;let r=t.value;let d={userDBID:document.getElementById("userDBID").getAttribute("user_dbid"),authToken:n,usrMsg:r},o={method:"POST",body:JSON.stringify(d),headers:{"Content-Type":"application/json"}};fetch("contact-api",o).then(e=>e.json()).then(function(t){console.log(t),1==t.success?(e.setAttribute("style","background: #00DD00; width:410px; transition: 0.7s;"),e.innerHTML="Done! Go Home",e.disabled=!1):(e.setAttribute("style","background: red;"),e.disabled=!0,e.innerHTML="ERROR",document.getElementById("api-response").innerHTML="Error: "+t.error,document.getElementById("api-response").setAttribute("style","display: block"))})}):reqBtn.innerHTML="Please sign in":window.location.replace("home")}updateImgs(),initFileDropListner();try{var addressField=document.getElementById("shippingaddressinput");addressField.addEventListener("keyup",function(e){13===e.keyCode&&(e.preventDefault(),"Success!"==addressField.value||""==addressField.value?(addressField.value="",addressField.blur()):(addShippingAddress(),addressField.blur()))})}catch(e){}function bSearch(){let e=document.getElementById("sfield");""==e.value?e.blur():search(e.value)}try{let e=document.getElementById("sfield");e.addEventListener("keyup",function(t){13===t.keyCode&&(t.preventDefault(),""==e.value?e.blur():search(e.value))})}catch(e){}try{document.getElementById("u-address").addEventListener("keyup",function(e){13===e.keyCode&&(e.preventDefault(),document.getElementById("u-r-btn").click())})}catch(e){}window.addEventListener("dragover",function(e){"dropzone"!=e.target.id&&(e.preventDefault(),e.dataTransfer.effectAllowed="none",e.dataTransfer.dropEffect="none")}),window.addEventListener("drop",function(e){"dropzone"!=e.target.id&&(e.preventDefault(),e.dataTransfer.effectAllowed="none",e.dataTransfer.dropEffect="none")}); |