├── README.md ├── api.json ├── app.js └── index.html /README.md: -------------------------------------------------------------------------------- 1 | # carrito-directo-js 2 | Carrito con Js 2 3 | -------------------------------------------------------------------------------- /api.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "precio": 500, 4 | "id": 1, 5 | "title": "Café", 6 | "thumbnailUrl": "https://picsum.photos/id/0/600" 7 | }, 8 | { 9 | "precio": 300, 10 | "id": 2, 11 | "title": "Pizza", 12 | "thumbnailUrl": "https://picsum.photos/id/10/600" 13 | }, 14 | { 15 | "precio": 100, 16 | "id": 3, 17 | "title": "Agua", 18 | "thumbnailUrl": "https://picsum.photos/id/20/600" 19 | }, 20 | { 21 | "precio": 50, 22 | "id": 4, 23 | "title": "Sandía", 24 | "thumbnailUrl": "https://picsum.photos/id/30/600" 25 | }, 26 | { 27 | "precio": 10, 28 | "id": 5, 29 | "title": "Mango", 30 | "thumbnailUrl": "https://picsum.photos/id/40/600" 31 | }, 32 | { 33 | "precio": 150, 34 | "id": 6, 35 | "title": "Chela", 36 | "thumbnailUrl": "https://picsum.photos/id/50/600" 37 | } 38 | ] -------------------------------------------------------------------------------- /app.js: -------------------------------------------------------------------------------- 1 | document.addEventListener("DOMContentLoaded", () => { 2 | fetchData() 3 | }) 4 | 5 | const fetchData = async () => { 6 | try { 7 | const res = await fetch('api.json') 8 | const data = await res.json() 9 | // console.log(data) 10 | pintarProductos(data) 11 | detectarBotones(data) 12 | } catch (error) { 13 | console.log(error) 14 | } 15 | } 16 | 17 | const contendorProductos = document.querySelector('#contenedor-productos') 18 | const pintarProductos = (data) => { 19 | const template = document.querySelector('#template-productos').content 20 | const fragment = document.createDocumentFragment() 21 | // console.log(template) 22 | data.forEach(producto => { 23 | // console.log(producto) 24 | template.querySelector('img').setAttribute('src', producto.thumbnailUrl) 25 | template.querySelector('h5').textContent = producto.title 26 | template.querySelector('p span').textContent = producto.precio 27 | template.querySelector('button').dataset.id = producto.id 28 | const clone = template.cloneNode(true) 29 | fragment.appendChild(clone) 30 | }) 31 | contendorProductos.appendChild(fragment) 32 | } 33 | 34 | let carrito = {} 35 | 36 | const detectarBotones = (data) => { 37 | const botones = document.querySelectorAll('.card button') 38 | 39 | botones.forEach(btn => { 40 | btn.addEventListener('click', () => { 41 | // console.log(btn.dataset.id) 42 | const producto = data.find(item => item.id === parseInt(btn.dataset.id)) 43 | producto.cantidad = 1 44 | if (carrito.hasOwnProperty(producto.id)) { 45 | producto.cantidad = carrito[producto.id].cantidad + 1 46 | } 47 | carrito[producto.id] = { ...producto } 48 | // console.log('carrito', carrito) 49 | pintarCarrito() 50 | }) 51 | }) 52 | } 53 | 54 | const items = document.querySelector('#items') 55 | 56 | const pintarCarrito = () => { 57 | 58 | //pendiente innerHTML 59 | items.innerHTML = '' 60 | 61 | const template = document.querySelector('#template-carrito').content 62 | const fragment = document.createDocumentFragment() 63 | 64 | Object.values(carrito).forEach(producto => { 65 | // console.log('producto', producto) 66 | template.querySelector('th').textContent = producto.id 67 | template.querySelectorAll('td')[0].textContent = producto.title 68 | template.querySelectorAll('td')[1].textContent = producto.cantidad 69 | template.querySelector('span').textContent = producto.precio * producto.cantidad 70 | 71 | //botones 72 | template.querySelector('.btn-info').dataset.id = producto.id 73 | template.querySelector('.btn-danger').dataset.id = producto.id 74 | 75 | const clone = template.cloneNode(true) 76 | fragment.appendChild(clone) 77 | }) 78 | 79 | items.appendChild(fragment) 80 | 81 | pintarFooter() 82 | accionBotones() 83 | 84 | } 85 | 86 | const footer = document.querySelector('#footer-carrito') 87 | const pintarFooter = () => { 88 | 89 | footer.innerHTML = '' 90 | 91 | if (Object.keys(carrito).length === 0) { 92 | footer.innerHTML = ` 93 |
# | 21 |Item | 22 |Cantidad | 23 |Acción | 24 |Total | 25 |
---|---|---|---|---|
Carrito vacío - comience a comprar! | 31 |
$ 5000
78 | 81 |