UNPKG

plxdcma_eshop

Version:

Instala todo lo necesario para una tienda virtual

201 lines (179 loc) 6.43 kB
function areAllSizesValid(){ return verifyDiferentSizes() } function verifyDiferentSizes(){ // var inventarioValue=shopInputInventario.value.trim() // var preciovalue=shopInputPrice.value.trim() for(var each in productregistryDiferentSize){ var o=productregistryDiferentSize[each] var inventarioValue=o.inventario var precioValue=o.precio var op=o.texto if(inventarioValue == null || isNaN(inventarioValue)){ shopregproerrorlbl.style.opacity=1 shopregproerrorlbl.innerHTML=`El numero en inventario en [ ${op} ] debe ser un valor numerico` shopInputInventario.style.border="solid 1px red" errorize(shopregproerrorlbl) return false } if(precioValue == null || isNaN(precioValue)){ shopregproerrorlbl.style.opacity=1 shopregproerrorlbl.innerHTML=`El valor insertado en el precio de [ ${op} ] debe ser un valor numerico.` shopInputPrice.style.border="solid 1px red" errorize(shopregproerrorlbl) return false } } return true } function diferentSizeInserts(){ var stnd=[] var moneda=shopCurrencyList.options[shopCurrencyList.selectedIndex].value for(var each in productregistryDiferentSize){ var o=productregistryDiferentSize[each] stnd.push([null,o.precio,o.inventario,o.texto,moneda]) } return stnd } function postNewProduct(){ var nombre =shopproductregistrynameinput.value.trim() var descripcion =shoploadingdescription.value.trim() var inventarioValue=shopInputInventario.value.trim() var preciovalue=shopInputPrice.value.trim() var options=shopCurrencyList.options var selectedIndex=shopCurrencyList.selectedIndex var moneda=options[selectedIndex].value var clearinputs=[ shopproductregistrynameinput, shoploadingdescription, shopInputInventario, shopInputPrice ] for(var each in clearinputs){ clearinputs[each].style.border="solid 1px rgb(0,0,0,0)" clearinputs[each].style.borderBottom="solid 1px black" } if(nombre == null){ shopregproerrorlbl.style.opacity=1 shopregproerrorlbl.innerHTML="Debe haber un nombre valido para este articulo." shopproductregistrynameinput.style.border="solid 1px red" errorize(shopregproerrorlbl) return } if(nombre.length < 5){ shopregproerrorlbl.style.opacity=1 shopregproerrorlbl.innerHTML="El nombre es muy corto, debe haber almenos 6 caracteres." shopproductregistrynameinput.style.border="solid 1px red" errorize(shopregproerrorlbl) return } if(nombre.length > 99){ shopregproerrorlbl.style.opacity=1 shopregproerrorlbl.innerHTML="El nombre es demasiado largo, debe haber como maximo 99 caracteres." shopproductregistrynameinput.style.border="solid 1px red" errorize(shopregproerrorlbl) return } if(descripcion.length > 599){ shopregproerrorlbl.style.opacity=1 shopregproerrorlbl.innerHTML="La descripción es demasiado larga, debe haber almenos 600 caracteres." shoploadingdescription.style.border="solid 1px red" errorize(shopregproerrorlbl) return } if( areAllSizesValid() == false ){ return false } shopregproerrorlbl.style.opacity=0 var cars=[] var crcy=shopCurrencyList.options[shopCurrencyList.selectedIndex].value var imagesdatas=[] for(var each in ProductRegistryImageDisplayerVerticalFunction.children){ var x=ProductRegistryImageDisplayerVerticalFunction.children[each] if(x.data != null){ imagesdatas.push(x.data) } } var images=imagesdatas var sz=diferentSizeInserts() var uploadingdata={ nombre:shopproductregistrynameinput.value.trim(), descripcion:shoploadingdescription.value.trim(), caracteristicas:shRegistryCaracteristicsList, moneda:moneda, standares:sz } if(myShopIsGoingToRegisterAProduct != null){ if(myShopIsGoingToRegisterAProduct(uploadingdata,images) == false){ return false } } let xhr = new XMLHttpRequest(); xhr.open('POST', '/myshop/reg/product') xhr.setRequestHeader('Content-Type', 'application/json') xhr.onload=function(e){ var r=JSON.parse(e.target.responseText) if(DBs != null){ if(DBs.productos == null){ DBs.productos={} } uploadingdata.id=r.id uploadingdata.url=r.url for(var each in productregistryDiferentSize){ productregistryDiferentSize[each].product = r.id } DBs.productos[r.id]=uploadingdata } if(DBs.productos_estandares == null){ DBs.productos_estandares=[] } DBs.productos_estandares.push(... productregistryDiferentSize) var downloadedData=r var promesasDeImagenes=[] var imagesuploaded=0 function finishing(){ function proceedehastaelfinal(){ registerPreFabricStandares(uploadingdata.standares) btnshreg.innerHTML="<i class='fa fa-check'></i>" setTimeout(function(){ ProductRegistryModal.style.display="none " btnshreg.innerHTML="<i class='fa fa-plus'></i> Registrar nuevo producto" },1000) } if(myShopDidRegisterAProduct != null){ myShopDidRegisterAProduct(uploadingdata,images,e,proceedehastaelfinal) }else{ proceedehastaelfinal() } } if(images.length > 0){ btnshreg.innerHTML="50% <i class='fa fa-spinner fa-spin'></i>" for(var each in images){ promesasDeImagenes.push(new Promise(function(accept,reject){ let xhrimagesend = new XMLHttpRequest(); xhrimagesend.open('POST', "/myproduct/add/image") xhrimagesend.setRequestHeader('Content-Type', 'application/json'); xhrimagesend.onload=function(){ imagesuploaded=imagesuploaded+1 var percentage=parseInt((imagesuploaded/promesasDeImagenes.length)*100) var total=parseInt(50+percentage) btnshreg.innerHTML=" "+total+"% <i class='fa fa-spinner fa-spin'></i>" accept() } xhrimagesend.send(JSON.stringify(images[each])) })) } Promise.all(promesasDeImagenes).then(function(images){ if(shopRegisterProductHx != null){ shopRegisterProductHx(uploadingdata,images,e) } finishing() }) }else{ finishing() } } // send rquest with JSON payload xhr.send(JSON.stringify(uploadingdata)) btnshreg.innerHTML="<i class='fa fa-spinner fa-spin'></i>" }