Skip to content

Commit

Permalink
toast for unavailable metadata and missing translations
Browse files Browse the repository at this point in the history
  • Loading branch information
nomadbitcoin authored and danicuki committed Jul 26, 2024
1 parent 5504829 commit 295b18a
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 12 deletions.
9 changes: 3 additions & 6 deletions components/RenderField/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@ import { useTranslation } from 'react-i18next'

export default function RenderField({ object, field, isHtml, maxSize }) {
const { i18n } = useTranslation()
let content
const defaultLanguage = 'en'
const language = i18n.resolvedLanguage || defaultLanguage

if (object?.metadata) {
content = object.metadata[i18n.resolvedLanguage || 'en']?.[field]
} else {
content = object?.[field]
}
let content = object?.metadata?.[language]?.[field] ?? object?.[field]

if (maxSize && content?.length > maxSize) {
content = `${content.substring(0, maxSize)}...`
Expand Down
9 changes: 5 additions & 4 deletions lib/course.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,12 @@ export async function getHomeCourse() {
}

export function getFieldContent(object, field, i18n) {
let content
const defaultLanguage = 'en'
const language = i18n.resolvedLanguage || defaultLanguage

if (object?.metadata) {
content = object.metadata[i18n.resolvedLanguage || 'en']?.[field]
} else {
let content = object?.metadata?.[language]?.[field]

if (content === undefined) {
content = object?.[field]
}

Expand Down
7 changes: 7 additions & 0 deletions pages/courses/[id].js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import Loading from '../../components/Loading'
import { dateFormat } from '../../lib/dateFormat'
import { useTranslation } from 'react-i18next'
import RenderField from '../../components/RenderField'
import { toast } from 'react-toastify'

function Course({ course, currentDate }) {
if (!course.active) return <NotFound />
Expand All @@ -35,6 +36,12 @@ function Course({ course, currentDate }) {
const { t, i18n } = useTranslation()
const language = i18n.resolvedLanguage

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

let counter = 0
useEffect(async () => {
setCohorts(await getAllCohorts())
Expand Down
6 changes: 6 additions & 0 deletions pages/courses/[id]/[section]/[lesson].js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ function Lessons({ course, section, lesson, content, currentDate }) {
const { t, i18n } = useTranslation()
const language = i18n.resolvedLanguage

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

useEffect(async () => {
if (auth.currentUser) {
const userSession = await getUserFromFirestore(auth.currentUser)
Expand Down
3 changes: 2 additions & 1 deletion public/locales/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@
"exercise_not_submitted": "You have not submitted the exercise for this lesson yet",
"already_on_first_lesson": "You are already on the first lesson.",
"data_updated_success": "Data updated successfully!",
"error_updating_data": "Error updating data"
"error_updating_data": "Error updating data",
"language_not_available": "Content not available for the selected language. Using the default language."
},
"twitter-share": "Networking is everything, how about sharing your progress with your friends on Twitter?",
"buttons": {
Expand Down
3 changes: 2 additions & 1 deletion public/locales/pt-BR/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@
"exercise_not_submitted": "Você ainda não enviou o exercício desta lição",
"already_on_first_lesson": "Você já está na primeira lição.",
"data_updated_success": "Dados atualizados com sucesso!",
"error_updating_data": "Erro ao atualizar dados"
"error_updating_data": "Erro ao atualizar dados",
"language_not_available": "Conteúdo não disponível para o idioma selecionado. Usando o idioma padrão."
},
"twitter-share": "Networking é tudo, que tal compartilhar seu progresso com os seus amigos no Twitter?",
"link_github_account": "Deseja vincular o github à sua conta já cadastrada?",
Expand Down

0 comments on commit 295b18a

Please sign in to comment.