|
| 1 | +/* |
| 2 | +
|
| 3 | +Componentes HTML/Javascript |
| 4 | +
|
| 5 | +1 - Una parte de un elemento grande o pequeño |
| 6 | +2 - Pieza modular e individual |
| 7 | +3 - Usualmente consiste de HTML, CSS y JS |
| 8 | +4 - Reusable |
| 9 | +
|
| 10 | +*/ |
| 11 | +const botonDePublicar = document.getElementById('publicar-twit-btn'); |
| 12 | +const formulario = document.getElementById('formulario'); |
| 13 | +// Declarar el event listener |
| 14 | +// botonDePublicar.addEventListener('click', publicarTwit); |
| 15 | +formulario.addEventListener('submit', publicarTwit); |
| 16 | +// Seleccionar contenedor de publicaciones |
| 17 | +const contenedorDePublicaciones = document.getElementById('post-container'); |
| 18 | + |
| 19 | +function publicarTwit(evento){ |
| 20 | + evento.preventDefault() |
| 21 | + const titleInputValue = document.getElementById('title-input'); |
| 22 | + const postContentValue = document.getElementById('post-content'); |
| 23 | + const twit = { |
| 24 | + titulo: titleInputValue.value, |
| 25 | + contenido: postContentValue.value, |
| 26 | + } |
| 27 | + |
| 28 | + titleInputValue.value = ''; |
| 29 | + postContentValue.value = ''; |
| 30 | + |
| 31 | + |
| 32 | + // CrearNuevoTwit(twit); |
| 33 | + // CrearNuevoTwit2(twit); |
| 34 | + |
| 35 | + new Twit(contenedorDePublicaciones, twit); |
| 36 | + |
| 37 | +} |
| 38 | + |
| 39 | + |
| 40 | +function CrearNuevoTwit({titulo, contenido}){ |
| 41 | + // Crear el contenedor |
| 42 | + const contenedor = document.createElement('div'); |
| 43 | + contenedor.classList.add('card', 'post'); |
| 44 | + |
| 45 | + // Crear el elemento de la imágen |
| 46 | + const imagen = document.createElement('img'); |
| 47 | + imagen.classList.add('post-img'); |
| 48 | + imagen.src = 'https://lh3.googleusercontent.com/proxy/5ct9e37RtBJXlTDkJ-d0eix218oShdLKHQ7ctMgNM6nyJY26KimCOqYvt1qChHYNyG4S9z1JPZEqA3XVlj52TDz-LZ_7fYehOvbhCL5aakwZ'; |
| 49 | + imagen.alt = 'profile pic'; |
| 50 | + |
| 51 | + // crear el titulo |
| 52 | + const contenedorDeTitulo = document.createElement('div'); |
| 53 | + contenedorDeTitulo.classList.add('post-title'); |
| 54 | + contenedorDeTitulo.textContent = titulo; |
| 55 | + |
| 56 | + // Creamos el contenido |
| 57 | + const contenedorDeContenido = document.createElement('div'); |
| 58 | + contenedorDeContenido.classList.add('post-content'); |
| 59 | + contenedorDeContenido.textContent = contenido; |
| 60 | + |
| 61 | + contenedor.appendChild(imagen); |
| 62 | + contenedor.appendChild(contenedorDeTitulo); |
| 63 | + contenedor.appendChild(contenedorDeContenido); |
| 64 | + |
| 65 | + return contenedor; |
| 66 | +} |
| 67 | + |
| 68 | +// JSX: JavaScript XML |
| 69 | +// JSON: JavaScript Object Notation |
| 70 | +function CrearNuevoTwit2({titulo, contenido}){ |
| 71 | + |
| 72 | + const nuevoTwit = ` |
| 73 | + <div class="card post"> |
| 74 | + <img class="post-img" src="https://lh3.googleusercontent.com/proxy/5ct9e37RtBJXlTDkJ-d0eix218oShdLKHQ7ctMgNM6nyJY26KimCOqYvt1qChHYNyG4S9z1JPZEqA3XVlj52TDz-LZ_7fYehOvbhCL5aakwZ" alt="profile pic"> |
| 75 | + <div class="post-title">${titulo}</div> |
| 76 | + <div class="post-content">${contenido}</div> |
| 77 | + </div> |
| 78 | + `; |
| 79 | + |
| 80 | + contenedorDePublicaciones.innerHTML += nuevoTwit; |
| 81 | +} |
| 82 | + |
| 83 | +class Twit { |
| 84 | + constructor(contenedor, {titulo, contenido}){ |
| 85 | + this.titulo = titulo; |
| 86 | + this.contenido = contenido; |
| 87 | + this.contenedor = contenedor; |
| 88 | + |
| 89 | + if(this.validarEntrada()){ |
| 90 | + this.agregarAlContenedor(); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + generarElementos(){ |
| 95 | + const nuevoTwit = ` |
| 96 | + <div class="card post"> |
| 97 | + <img class="post-img" src="https://lh3.googleusercontent.com/proxy/5ct9e37RtBJXlTDkJ-d0eix218oShdLKHQ7ctMgNM6nyJY26KimCOqYvt1qChHYNyG4S9z1JPZEqA3XVlj52TDz-LZ_7fYehOvbhCL5aakwZ" alt="profile pic"> |
| 98 | + <div class="post-title">${this.titulo}</div> |
| 99 | + <div class="post-content">${this.contenido}</div> |
| 100 | + </div> |
| 101 | + `; |
| 102 | + |
| 103 | + return nuevoTwit; |
| 104 | + } |
| 105 | + |
| 106 | + validarEntrada(){ |
| 107 | + if(!this.titulo){ |
| 108 | + alert('Debes dar un título a tu twit!'); |
| 109 | + return false; |
| 110 | + } |
| 111 | + |
| 112 | + if(!this.contenido){ |
| 113 | + alert('Debes dar contenido a tu twit!'); |
| 114 | + return false; |
| 115 | + } |
| 116 | + return true; |
| 117 | + } |
| 118 | + |
| 119 | + agregarAlContenedor(){ |
| 120 | + this.contenedor.innerHTML += this.generarElementos(); |
| 121 | + } |
| 122 | + |
| 123 | +} |
| 124 | + |
| 125 | + |
| 126 | +{/* <div class="card post"> |
| 127 | + <img class="post-img" src="https://lh3.googleusercontent.com/proxy/5ct9e37RtBJXlTDkJ-d0eix218oShdLKHQ7ctMgNM6nyJY26KimCOqYvt1qChHYNyG4S9z1JPZEqA3XVlj52TDz-LZ_7fYehOvbhCL5aakwZ" alt="profile pic"> |
| 128 | + <div class="post-title">Este es el twitter faik</div> |
| 129 | + <div class="post-content">Hola, les escribo desde el twitter de titiri mundati</div> |
| 130 | +</div> */} |
0 commit comments