From bef62a5c0ad45abb1b0169bf99136661505cbb72 Mon Sep 17 00:00:00 2001 From: amandascam03 Date: Fri, 28 Jul 2023 20:07:28 -0300 Subject: [PATCH] postagens sendo exibidas com printar post --- src/lib/firebase.js | 32 ++++++++++++++++------------ src/pages/cadastro/cadastro.js | 4 ++-- src/pages/feed/feed.css | 1 + src/pages/feed/feed.js | 38 ++++++++++++++++++++++++++++------ src/pages/login/login.js | 3 ++- 5 files changed, 56 insertions(+), 22 deletions(-) diff --git a/src/lib/firebase.js b/src/lib/firebase.js index 15ab7a05..fc2c7ded 100644 --- a/src/lib/firebase.js +++ b/src/lib/firebase.js @@ -38,6 +38,7 @@ export function redefinirSenha(email) { export async function adicionarPost(username, conteudo) { try { + const docRef = await addDoc(collection(db, "posts"), { username: auth.currentUser.displayName, uid: auth.currentUser.uid, @@ -52,17 +53,22 @@ export async function adicionarPost(username, conteudo) { export async function exibirPosts() { try { - const feedElement = document.querySelector('.post'); - const querySnapshot = await getDocs(collection(db, "posts")); - querySnapshot.forEach((doc) => { - const post = doc.data(); - const postElement= document.createElement('div'); - postElement.innerHTML = `

${post.username}

-

${post.conteudo}

`; - feedElement.appendChild(postElement); - console.log(`teste1 ${doc.id} => ${doc.data()}`); - }); -} catch (error) { - console.error('erro ao obter os posts', error); -} + const querySnapshot = await getDocs(collection(db, "posts")); + const feedElement = document.querySelector('.post'); + feedElement.innerHTML = ''; + const array = []; + querySnapshot.forEach((doc) => { + // const post = doc.data(); + // const postElement = document.createElement('div'); + // postElement.innerHTML = `

${post.username}

+ //

${post.conteudo}

+ //
`; + // feedElement.appendChild(postElement); + array.push(doc.data()); + // console.log(`${doc.id} => ${doc.data()}`); + }); + return array; + } catch (error) { + console.error('erro ao obter os posts', error); + } }; diff --git a/src/pages/cadastro/cadastro.js b/src/pages/cadastro/cadastro.js index fd25db97..e5b2c89b 100644 --- a/src/pages/cadastro/cadastro.js +++ b/src/pages/cadastro/cadastro.js @@ -48,6 +48,8 @@ export default () => { checkBtn.addEventListener('change', mostrarSenha); + const mensagemFormatada = document.createElement('p'); + botaoCadastrar.addEventListener('click', (event) => { event.preventDefault(); const emailDoUsuario = inputEmail.value; @@ -55,8 +57,6 @@ export default () => { const nomeDoUsuario = inputNome.value; const caixaDeTexto = container.querySelector('#caixaDeTexto'); - const mensagemFormatada = document.createElement('p'); - function exibirErro(errorCode) { const mensagemErroDiv = document.getElementById('mensagemErro'); mensagemFormatada.textContent = `${errorCode}`; diff --git a/src/pages/feed/feed.css b/src/pages/feed/feed.css index 8cffd65b..1d2ac40a 100644 --- a/src/pages/feed/feed.css +++ b/src/pages/feed/feed.css @@ -75,6 +75,7 @@ z-index: 2; position: fixed; width: 100vw; + display: none; } .lista-menu-mobile > li { diff --git a/src/pages/feed/feed.js b/src/pages/feed/feed.js index 2d063a6c..dca76cfc 100644 --- a/src/pages/feed/feed.js +++ b/src/pages/feed/feed.js @@ -92,15 +92,41 @@ export default () => { const btnPublicar = container.querySelector('#btn-publicar'); const textarea = container.querySelector('#story'); - const post = container.querySelector('.post'); + const textareaVal = textarea.value; + console.log(textareaVal); + + // document.addEventListener('load', exibirPosts); + exibirPosts() // pega todos os docs ja criados e exibe na tela, como uma array de objetos // se os dados do post forem pegos, printa o post na tela. + .then((array) => { + printarPost(username, textareaVal); + console.log(array); + return array; + }); + + function printarPost(username, conteudo) { // cria um elemento div, adiciona no elemento div um h2 e p, adiciona esse conjunto + const postElement = document.createElement('div'); // dentro da div post e exibe na tela + postElement.innerHTML = ` +

${username}

+

${conteudo}

+
+ `; + const feedElement = document.querySelector('.post'); + feedElement.appendChild(postElement); + } btnPublicar.addEventListener('click', () => { - const conteudoDoPost = document.createElement('p'); - conteudoDoPost.textContent = textarea.value; - post.appendChild(conteudoDoPost); - adicionarPost(username, conteudoDoPost.textContent); + // const conteudoDoPost = document.createElement('p'); + // conteudoDoPost.textContent = textarea.value; + // post.appendChild(conteudoDoPost); + const texto = textarea.value; + adicionarPost(username, texto) // cria uma collection com username e texto. se o doc for criado com sucesso, printa o post na tela + + .then((banana) => { + printarPost(username, texto); + console.log(banana); + }) + // salvarPost(username, conteudoDoPost.textContent); textarea.value = ''; - window.onload = exibirPosts(); }) return container; diff --git a/src/pages/login/login.js b/src/pages/login/login.js index e5c21f24..87ae0c85 100644 --- a/src/pages/login/login.js +++ b/src/pages/login/login.js @@ -52,12 +52,13 @@ export default () => { window.location.hash = '#redefinir-senha'; }); + const mensagemFormatada = document.createElement('p'); + botaoEntrar.addEventListener('click', (event) => { event.preventDefault(); const emailDoUsuario = inputEmail.value; const senhaDoUsuario = inputSenha.value; - const mensagemFormatada = document.createElement('p'); function exibirErro(errorCode) { const mensagemErroDiv = document.getElementById('mensagemErro');