From 538de2899a06a3d10bf5dac1cc4ca34cd9b41e41 Mon Sep 17 00:00:00 2001 From: Mika Kattainen Date: Mon, 17 May 2021 12:21:57 +0300 Subject: [PATCH] Hide age groups that do not have translated task groups --- src/components/AgeGroupItem/index.js | 12 +++++ src/helpers/index.js | 76 ++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+) diff --git a/src/components/AgeGroupItem/index.js b/src/components/AgeGroupItem/index.js index 96adfb3..74011f1 100644 --- a/src/components/AgeGroupItem/index.js +++ b/src/components/AgeGroupItem/index.js @@ -5,8 +5,10 @@ import { getAgeGroupTitleWithoutAges, getTermInLanguage, getAgeGroupStatus, + ageGroupHasTranslatedTaskGroups, } from 'helpers' import { getAgeGroupIcon } from 'graphics/ageGroups' +import { useSelector } from 'react-redux' const StyledAgeGroupItem = styled.div` width: 50vw; @@ -57,6 +59,16 @@ const AgeGroupItem = ({ user, userTasks, }) => { + const itemsByGuid = useSelector(state => state.itemsByGuid) + const hasTranslatedTaskGroups = ageGroupHasTranslatedTaskGroups( + ageGroup.item, + itemsByGuid, + language + ) + + // Hide the age group if it does not have any translated task groups + if (!hasTranslatedTaskGroups) return null + const languageInfo = ageGroup.item.languages.find(x => x.lang === language) const status = user.loggedIn diff --git a/src/helpers/index.js b/src/helpers/index.js index 7191f81..dbe7ac2 100644 --- a/src/helpers/index.js +++ b/src/helpers/index.js @@ -128,6 +128,82 @@ export const getAgeGroupTasks = ageGroup => { ) } +/** + * Get a list of the task group guids for the task groups that have their title translated + * and contain at least one translated task from the given language + * @param ageGroup The ageGroup object to get the task groups for + * @param itemsByGuid An object containing all items with guid as the key + * @param language The selected language + * @returns {Array[string]} An array of TaskGroup guids + */ +export const getAgeGroupsTranslatedTaskGroups = ( + ageGroup, + itemsByGuid, + language +) => { + return ageGroup.taskgroups.reduce((acc, taskGroupGuid) => { + const taskGroup = itemsByGuid[taskGroupGuid.guid].item + const taskGroupTranslations = taskGroup.languages.find( + group => group.lang === language + ) + const hasTranslatedTasks = taskGroupHasTranslatedTasks(taskGroup, language) + if ( + taskGroupTranslations && + taskGroupTranslations.title && + hasTranslatedTasks + ) { + acc.push(taskGroupGuid.guid) + } + return acc + }, []) +} + +/** + * Get all the translated tasks for a task group for a specific language + * @param taskGroup The task group to get the tasks for + * @param language The language to filter by + * @returns {Array[Task]} Return an array of task objects + */ +export const getTaskGroupsTranslatedTasks = (taskGroup, language) => { + return taskGroup.tasks.reduce((acc, task) => { + const translations = task.languages.find(lang => lang.lang === language) + if (translations && translations.title) { + acc.push(task) + } + return acc + }, []) +} + +/** + * Check if a task group has 1 or more tasks translated to the given language + * @param taskGroup The task group to check + * @param language The language to check the translations for + * @returns {boolean} True if task group has translated tasks and false otherwise + */ +export const taskGroupHasTranslatedTasks = (taskGroup, language) => { + return getTaskGroupsTranslatedTasks(taskGroup, language).length > 0 +} + +/** + * Checks if The given age group has translated task groups + * @param ageGroup The ageGroup object to check + * @param itemsByGuid An object containing all items with guid as the key + * @param language The selected language + * @returns {boolean} True if age groups has translated task groups and false otherwise + */ +export const ageGroupHasTranslatedTaskGroups = ( + ageGroup, + itemsByGuid, + language +) => { + const taskGroups = getAgeGroupsTranslatedTaskGroups( + ageGroup, + itemsByGuid, + language + ) + return taskGroups.length > 0 +} + export const getAgeGroupStatus = (ageGroup, userTasks) => { const ageGroupTasks = getAgeGroupTasks(ageGroup) const completedMandatory = ageGroupTasks.mandatory.filter(