1import { db, auth } from "./firebase-config.js"; 2import { collection, addDoc, serverTimestamp } from "https://www.gstatic.com/firebasejs/9.6.1/firebase-firestore.js"; 3import { IMGBB_UPLOAD_URL } from "./config.js"; 4 5const recadosForm = document.getElementById('recadosForm'); 6const recadosModal = document.getElementById('recadosModal'); 7const imagemPreview = document.getElementById('imagemPreview'); 8const imagemInput = document.getElementById('recadoMidia'); 9const previewContainer = document.getElementById('previewContainer'); 10 11let imagemUploadada = null; 12 13// ========== PREVIEW DE IMAGEM ========== 14if (imagemInput) { 15 imagemInput.addEventListener('change', (e) => { 16 const file = e.target.files[0]; 17 if (file) { 18 const reader = new FileReader(); 19 reader.onload = (event) => { 20 imagemPreview.src = event.target.result; 21 previewContainer.style.display = 'block'; 22 }; 23 reader.readAsDataURL(file); 24 } 25 }); 26} 27 28// ========== UPLOAD PARA IMGBB ========== 29async function uploadImagemImgBB(file) { 30 const formData = new FormData(); 31 formData.append('image', file); 32 33 try { 34 const response = await fetch(IMGBB_UPLOAD_URL, { 35 method: 'POST', 36 body: formData 37 }); 38 39 if (!response.ok) { 40 throw new Error('Erro ao fazer upload da imagem'); 41 } 42 43 const data = await response.json(); 44 if (data.success) { 45 return data.data.url; 46 } else { 47 throw new Error('Upload falhou: ' + data.error.message); 48 } 49 } catch (error) { 50 console.error('Erro ao fazer upload:', error); 51 throw error; 52 } 53} 54 55// ========== ENVIAR RECADO ========== 56if (recadosForm) { 57 recadosForm.addEventListener('submit', async (e) => { 58 e.preventDefault(); 59 60 const nome = document.getElementById('recadoNome').value.trim(); 61 const cidade = document.getElementById('recadoCidade').value.trim(); 62 const texto = document.getElementById('recadoTexto').value.trim(); 63 const imagemFile = imagemInput.files[0]; 64 65 // Validação 66 if (!nome || !cidade || !texto) { 67 Swal.fire('Erro', 'Por favor, preencha todos os campos obrigatórios.', 'error'); 68 return; 69 } 70 71 if (!imagemFile) { 72 Swal.fire('Erro', 'Por favor, selecione uma imagem.', 'error'); 73 return; 74 } 75 76 // Validar tipo de arquivo 77 if (!imagemFile.type.startsWith('image/')) { 78 Swal.fire('Erro', 'Por favor, selecione apenas imagens.', 'error'); 79 return; 80 } 81 82 // Validar tamanho (máximo 5MB) 83 if (imagemFile.size > 5 * 1024 * 1024) { 84 Swal.fire('Erro', 'A imagem não pode exceder 5MB.', 'error'); 85 return; 86 } 87 88 try { 89 // Mostrar loading 90 Swal.fire({ 91 title: 'Enviando...', 92 text: 'Aguarde enquanto sua imagem é enviada.', 93 icon: 'info', 94 allowOutsideClick: false, 95 didOpen: () => { 96 Swal.showLoading(); 97 } 98 }); 99 100 // 1. Upload da imagem para ImgBB 101 const imagemURL = await uploadImagemImgBB(imagemFile); 102 103 // 2. Salvar recado no Firestore 104 await addDoc(collection(db, "recados"), { 105 nome, 106 cidade, 107 texto, 108 imagemURL, 109 aprovado: false, 110 timestamp: serverTimestamp(), 111 userEmail: auth.currentUser ? auth.currentUser.email : '[email protected]' 112 }); 113 114 // 3. Sucesso 115 Swal.fire('Sucesso!', 'Seu recado foi enviado e está aguardando aprovação.', 'success'); 116 117 // Limpar formulário 118 recadosForm.reset(); 119 previewContainer.style.display = 'none'; 120 imagemUploadada = null; 121 122 // Fechar modal 123 $(recadosModal).modal('hide'); 124 125 } catch (error) { 126 console.error("Erro ao enviar recado: ", error); 127 Swal.fire('Erro', `Ocorreu um erro: ${error.message}`, 'error'); 128 } 129 }); 130} 131 132export { uploadImagemImgBB };
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.