Skip to content

Commit

Permalink
Merge main into production: language fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
nomadbitcoin committed Sep 10, 2024
1 parent 77e0523 commit 6ed4dd3
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 32 deletions.
98 changes: 68 additions & 30 deletions pages/admin/createGroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -35,6 +39,11 @@ function CreateStudyGroup({ user }) {
}))
})
})
} else if (name === 'title' || name === 'description') {
setTempLanguageData((prevData) => ({
...prevData,
[name]: value,
}))
} else {
setFormData((prevData) => ({
...prevData,
Expand All @@ -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
}
}

Expand All @@ -67,6 +100,24 @@ function CreateStudyGroup({ user }) {
<div className="container mx-auto mt-8 px-4">
<h1 className="mb-4 text-2xl font-bold">{t('createStudyGroup')}</h1>
<form onSubmit={handleSubmit} className="space-y-4">
<div>
<label htmlFor="language" className="block">
{t('language')}
</label>
<select
id="language"
name="language"
value={formData.language}
onChange={handleChange}
required
className="w-full rounded border p-2"
>
<option value="">{t('selectLanguage')}</option>
<option value="en">English</option>
<option value="pt-BR">Português</option>
<option value="es">Español</option>
</select>
</div>
<div>
<label htmlFor="title" className="block">
{t('title')}
Expand All @@ -75,7 +126,7 @@ function CreateStudyGroup({ user }) {
type="text"
id="title"
name="title"
value={formData.title}
value={tempLanguageData.title}
onChange={handleChange}
required
className="w-full rounded border p-2"
Expand All @@ -88,7 +139,7 @@ function CreateStudyGroup({ user }) {
<textarea
id="description"
name="description"
value={formData.description}
value={tempLanguageData.description}
onChange={handleChange}
required
className="w-full rounded border p-2"
Expand All @@ -105,25 +156,11 @@ function CreateStudyGroup({ user }) {
onChange={handleChange}
className="w-full rounded border p-2"
>
<option value="Beginner">{t('beginner')}</option>
<option value="Intermediate">{t('intermediate')}</option>
<option value="Advanced">{t('advanced')}</option>
<option value="beginner">{t('beginner')}</option>
<option value="intermediate">{t('intermediate')}</option>
<option value="advanced">{t('advanced')}</option>
</select>
</div>
<div>
<label htmlFor="language" className="block">
{t('language')}
</label>
<input
type="text"
id="language"
name="language"
value={formData.language}
onChange={handleChange}
required
className="w-full rounded border p-2"
/>
</div>
<div>
<label htmlFor="scheduled_at" className="block">
{t('scheduledAt')}
Expand Down Expand Up @@ -181,6 +218,7 @@ function CreateStudyGroup({ user }) {
<button
type="submit"
className="text-white rounded bg-blue-500 px-4 py-2 hover:bg-blue-600"
disabled={formData.language === ''} // Desabilita o botão se nenhuma linguagem for selecionada
>
{t('createStudyGroup')}
</button>
Expand Down
9 changes: 8 additions & 1 deletion pages/study-groups/[slug].js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import Link from 'next/link'
import Head from 'next/head'
import Image from 'next/image'
import Loading from '../../components/Loading'
import { dateFormat } from '../../lib/dateFormat'
import { toast } from 'react-toastify'
import { useTranslation } from 'react-i18next'
import RenderField from '../../components/RenderField'

Expand All @@ -22,6 +22,13 @@ function StudyGroup({ studyGroup }) {
const [loading, setLoading] = useState(true)
const [userRegisteredInGroup, setUserRegisteredInGroup] = useState(false)
const { t, i18n } = useTranslation()
const language = i18n.resolvedLanguage

useEffect(() => {
if (studyGroup?.metadata && !studyGroup.metadata.hasOwnProperty(language)) {
toast.error(t('messages.language_not_available'))
}
}, [language])

useEffect(() => {
const fetchUser = async () => {
Expand Down
2 changes: 1 addition & 1 deletion public/locales/pt-BR/translation.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"title": "Lista de Builds",
"title": "Title",
"createFirstProject": "Crie seu Primeiro Projeto WEB3",
"indicateAndWin": "Indique e Ganhe",
"profilePicture": "Foto de perfil",
Expand Down

0 comments on commit 6ed4dd3

Please sign in to comment.