2 lines
7.5 KiB
JavaScript
2 lines
7.5 KiB
JavaScript
|
carritoUsuario=[];window.addEventListener("hashchange",()=>{render(decodeURI(window.location.hash))});const render=e=>{switch(e){case"#registro":abrirRegistro();break;case"#iniciar-sesion":abrirInicioSesion();break;case"#usuarios/":abrirUsuarios();break;case"#usuarios/mi-cuenta":abrirMiCuenta();break;case"#usuarios/pagos":abrirCarrito();break;case"#staff/":abrirStaff();break;default:return void abrirPagPrin()}},sectionP=document.getElementById("sectionP"),contenedorInicioRegistro=document.getElementById("contenedorInicio-Registro"),formInicioRegistro=document.getElementById("inicio_registro"),sectionS=document.getElementById("sectionS"),sectionCarrito=document.getElementById("sectionCarrito"),manipularTopBar=e=>{switch(e){case 1:cambiarProgreso(25);break;case 2:cancelarTransicion(),cambiarProgreso(50);break;case 3:cancelarTransicion(),cambiarProgreso(75);break;default:cancelarTransicion(),reiniciarTopBar()}},abrirUsuarios=()=>{const e=new XMLHttpRequest;reiniciarTopBar(),cerrarElemF(contenedorInicioRegistro),cerrarElemF(formInicioRegistro),cerrarElemF(sectionP),cerrarElemF(sectionCarrito),e.onreadystatechange=(()=>{200===e.status&&4===e.readyState?(cambiarProgreso(100).then(ocultarTopBar),abrirElemF(sectionS,()=>{sectionS.innerHTML=e.responseText,(()=>new Promise((e,t)=>{const r=new XMLHttpRequest;r.onreadystatechange=(()=>{if(200===r.status&&4===r.readyState){const t=JSON.parse(r.responseText),n=document.getElementById("productos");let a=document.createElement("div");a.className="row";let i=0;for(const e in t)if(t.hasOwnProperty(e)){const r=t[e];++i>=4&&(n.appendChild(a),n.appendChild(document.createElement("br")),(a=document.createElement("div")).className="row",i=1);const o=document.createElement("div");o.className="col-4 caja plato";const c=document.createElement("img");c.className="image",c.src=r.imgUrl,o.appendChild(c);const s=document.createElement("div");s.className="plato--Titulo",s.innerText=r.nombre,o.appendChild(s),o.appendChild(document.createElement("br"));const d=document.createElement("div");d.className="plato--Descripcion",d.innerText=r.descripcion,o.appendChild(d),o.appendChild(document.createElement("br"));const l=document.createElement("div");l.className="plato--Precio",l.innerText=`${r.precio}`,o.appendChild(l);const m=document.createElement("div");m.className="plato--Cantidad",m.appendChild(document.createTextNode("Cantidad: "));const p=document.createElement("span"),u=document.createElement("i");u.className="material-icons plato--Cantidad--Icon",u.innerText="remove",u.addEventListener("click",()=>{const e=parseInt(p.innerText);p.innerText=e>1?e-1:"1"}),m.appendChild(u),p.innerText="1",m.appendChild(p);const h=document.createElement("i");h.className="material-icons plato--Cantidad--Icon",h.innerText="add",h.addEventListener("click",()=>{const e=parseInt(p.innerText);p.innerText=e>0?e+1:"1"}),m.appendChild(h),o.appendChild(m),o.appendChild(document.createElement("br"));const E=document.createElement("button");E.className="botonAddCart",E.addEventListener("click",()=>{const r=parseInt(p.innerText);if(r>0){const n=t[e];n.cantidad=r,carritoUsuario.push(n),console.log(`JSON del usuario|>\n${JSON.stringify(carritoUsuario)}`),window.location.replace("./#usuarios/pagos")}else alert("")}),E.innerHTML="<i class='material-icons'>add_shopping_cart</i> Añadir al carrito",o.appendChild(E),a.appendChild(o)}n.appendChild(a),e()}}),r.open("POST","./usuarios/platos.json",!0),r.setRequestHeader("Content-type","application/x-www-form-urlencoded"),r.send()}))()})):manipularTopBar(e.readyState)}),e.open("POST","./usuarios/index.php",!0),e.setRequestHeader("Content-type","application/x-www-form-urlencoded"),e.send()},abrirMiCuenta=()=>{const e=new XMLHttpRequest;reiniciarTopBar(),cerrarElemF(contenedorInicioRegistro),cerrarElemF(formInicioRegistro),cerrarElemF(sectionP),cerrarElemF(sectionCarrito),e.onreadystatechange=(()=>{200===e.status&&4===e.readyState?(cambiarProgreso(100).then(ocultarTopBar),abrirElemF(sectionS,()=>{sectionS.innerHTML=e.responseText})):manipularTopBar(e.readyState)}),e.open("POST","./usuarios/mi-c
|