From 7b276c3f34c56c8b92d7bb47dac0b399f83a9dca Mon Sep 17 00:00:00 2001 From: Yan Luiz Date: Tue, 10 Sep 2024 18:44:25 -0300 Subject: [PATCH 1/4] fix-languages: Study Group creation and content availability --- pages/admin/createGroup.js | 98 +++++++++++++++++++-------- pages/study-groups/[slug].js | 9 ++- public/locales/pt-BR/translation.json | 2 +- 3 files changed, 77 insertions(+), 32 deletions(-) diff --git a/pages/admin/createGroup.js b/pages/admin/createGroup.js index 68033f40..73018ca1 100644 --- a/pages/admin/createGroup.js +++ b/pages/admin/createGroup.js @@ -2,23 +2,27 @@ import React, { useState } from 'react' import { withProtected } from '../../hooks/route' import { useRouter } from 'next/router' import { db, storage } from '../../firebase/initFirebase' -import { collection, addDoc } from 'firebase/firestore' +import { collection, addDoc, Timestamp } from 'firebase/firestore' import { useTranslation } from 'react-i18next' import Head from 'next/head' import { ref, uploadBytes, getDownloadURL } from 'firebase/storage' function CreateStudyGroup({ user }) { const [formData, setFormData] = useState({ - title: '', - description: '', - difficulty: 'Beginner', - language: 'pt', - scheduled_at: '', + language: '', leader_discord_id: '', + scheduled_at: '', image_url: '', + difficulty: 'beginner', active: true, index: 0, }) + + const [tempLanguageData, setTempLanguageData] = useState({ + title: '', + description: '', + }) + const router = useRouter() const { t } = useTranslation() @@ -35,6 +39,11 @@ function CreateStudyGroup({ user }) { })) }) }) + } else if (name === 'title' || name === 'description') { + setTempLanguageData((prevData) => ({ + ...prevData, + [name]: value, + })) } else { setFormData((prevData) => ({ ...prevData, @@ -44,18 +53,42 @@ function CreateStudyGroup({ user }) { } const handleSubmit = async (e) => { - e.preventDefault() - const slug = formData.title.toLowerCase().replace(/ /g, '-') + e.preventDefault() // Garante que isso seja chamado primeiro + + if (!formData.language) { + alert(t('pleaseSelectLanguage')) + return + } + + if (!formData.image_url) { + alert(t('pleaseWaitForImageUpload')) + return + } + + const slug = tempLanguageData.title.toLowerCase().replace(/ /g, '-') + try { - const docRef = await addDoc(collection(db, 'study_groups'), { + const finalFormData = { ...formData, - scheduled_at: new Date(formData.scheduled_at), + scheduled_at: Timestamp.fromDate(new Date(formData.scheduled_at)), slug: slug, - }) + index: parseInt(formData.index, 10), + title: tempLanguageData.title, + description: tempLanguageData.description, + metadata: { + [formData.language]: { + title: tempLanguageData.title, + description: tempLanguageData.description, + }, + }, + } + const docRef = await addDoc(collection(db, 'study_groups'), finalFormData) console.log('Study group added with ID: ', docRef.id) + alert(t('studyGroupCreatedSuccessfully')) // Adiciona feedback de sucesso router.push(`/study-groups/${slug}`) } catch (error) { console.error('Error adding study group: ', error) + alert(t('errorCreatingStudyGroup')) // Adiciona feedback de erro } } @@ -67,6 +100,24 @@ function CreateStudyGroup({ user }) {

{t('createStudyGroup')}

+
+ + +