Skip to content

Commit

Permalink
Merge pull request #111 from partio-scout/fix/migrated-activitygroups
Browse files Browse the repository at this point in the history
Fix/migrated activitygroups
  • Loading branch information
linneamyl authored Sep 7, 2022
2 parents cc3b35f + 6388158 commit 3f67abb
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 26 deletions.
23 changes: 19 additions & 4 deletions src/components/Notifications/TaskGroupNotification.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import styled from 'styled-components'
import { Link } from 'react-router-dom'

import { useSelector } from 'react-redux'
import { getTaskUrl, getTimestamp } from './utils'
import { getTimestamp, getTaskUrl } from './utils'
import { getTermInLanguage } from 'helpers'

const Container = styled.div`
Expand All @@ -25,7 +25,22 @@ const StyledLink = styled(Link)`
const TaskGroupNotification = ({ notification, markRead }) => {
const translations = useSelector((state) => state.translations)
const itemsByGuid = useSelector((state) => state.itemsByGuid)
const taskGroup = itemsByGuid[notification.item_guid]
const activityGroups = useSelector((state) => state.activityGroups)

/* Below: Migrated activitygroup accomplishments comes with item id instead of wp_guid.
* ItemsByGuid keys are wp_guids, so if activitygroup won't be found from
* itemsByGuid -list, they need to be found from activitygroups-list (which contains slightly differend data)
*/

const taskGroup =
itemsByGuid[notification.item_guid] ||
activityGroups[notification.item_guid]

if (taskGroup === undefined) {
return null
}

const taskGroupItem = taskGroup.item ? taskGroup.item : taskGroup
const timestamp = getTimestamp(notification.created_at)

const getStateMessage = (state) => {
Expand All @@ -47,8 +62,8 @@ const TaskGroupNotification = ({ notification, markRead }) => {
{getStateMessage(notification.notification_type)}{' '}
{getTermInLanguage(translations, 'tehtavakokonaisuuden-suoritetuksi')}{' '}
</span>
<StyledLink to={getTaskUrl(taskGroup)} onClick={markRead}>
{taskGroup.item.title}
<StyledLink to={getTaskUrl(taskGroupItem)} onClick={markRead}>
{taskGroupItem.title}
</StyledLink>
</Message>
<Timestamp>{timestamp}</Timestamp>
Expand Down
2 changes: 1 addition & 1 deletion src/components/Notifications/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ export const getTimestamp = (datetime) => {
}

export const getTaskUrl = (task) => {
return `/guid/${getItemId(task.item)}`
return `/guid/${getItemId(task)}`
}
9 changes: 8 additions & 1 deletion src/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ export const getItemId = (item) => {
return item.wp_guid || item.id.toString()
}

export const getActivityGroupItemId = (taskGroup) => {
return taskGroup.id.toString() || taskGroup.wp_guid
}

export const deepFlatten = (items) => {
const flattener = (items) => {
const CHILD_GROUPS = ['activities']
Expand Down Expand Up @@ -75,7 +79,10 @@ export const getCompletedUserActivityGroups = (
activityGroup,
userActivityGroups
) => {
if (userActivityGroups[getItemId(activityGroup)] === 'COMPLETED') {
if (
userActivityGroups[activityGroup.wp_guid] === 'COMPLETED' ||
userActivityGroups[activityGroup.id.toString()] === 'COMPLETED'
) {
return getTermInLanguage(translations, 'kokonaisuus-valmis')
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/views/AcceptTasks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ const AcceptTasks = () => {
updateGroupMemberTask({
task_guid: taskGuid,
user_guid: Number(memberid),
completion_status: TASK_GROUP_STATUS.COMPLETED,
completion_status: COMPLETION_STATUS.COMPLETED,
groupGuid: Number(membergroup),
})
)
Expand All @@ -165,7 +165,7 @@ const AcceptTasks = () => {
updateGroupMemberTaskGroup({
taskgroup_guid: taskGuid,
user_guid: Number(memberid),
completed: COMPLETION_STATUS.COMPLETED,
completed: TASK_GROUP_STATUS.COMPLETED,
groupGuid: Number(membergroup),
})
)
Expand Down
12 changes: 10 additions & 2 deletions src/views/AgeGroup/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
getTermInLanguage,
getActivityGroupIcon,
getItemId,
getActivityGroupItemId,
} from 'helpers'

const Background = styled.div`
Expand Down Expand Up @@ -183,11 +184,16 @@ const AgeGroup = () => {
const completedGroups = []
const unfinishedGroups = []

// TODO: Find a way to simplify this if else
const getCompletedActivityGroups = (categories) => {
user.loggedIn && categories
? categories.map(({ groups }) => {
groups.map((group) => {
if (userActivityGroups[getItemId(group)] === 'COMPLETED') {
if (
userActivityGroups[getActivityGroupItemId(group)] === 'COMPLETED'
) {
completedGroups.push(group)
} else if (userActivityGroups[getItemId(group)] === 'COMPLETED') {
completedGroups.push(group)
} else {
const activities = group.activities
Expand All @@ -200,7 +206,8 @@ const AgeGroup = () => {
if (completedTasks === activities.length) {
if (
activities.length === 0 &&
userActivityGroups[getItemId(group)] !== 'COMPLETED'
userActivityGroups[getActivityGroupItemId(group)] !==
'COMPLETED'
) {
unfinishedGroups.push(group)
} else {
Expand All @@ -214,6 +221,7 @@ const AgeGroup = () => {
})
: null
}

getCompletedActivityGroups(categories)

return (
Expand Down
27 changes: 19 additions & 8 deletions src/views/Member/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,15 +175,24 @@ const Member = () => {

const completedTaskGroups = Object.keys(memberTaskGroups)
.filter((guid) => memberTaskGroups[guid] === TASK_GROUP_STATUS.COMPLETED)
.map((id) => itemsByGuid[id])
.map((id) => itemsByGuid[id] || activityGroups[id])

const completedTaskGroupsGuids = completedTaskGroups.map(
(group) => group && group.id
)

const taskGroupsMarkedCompleted = completedTaskGroupsGuids
.filter((guid) => !parentTaskGroupGuids.includes(guid))
.map((id) => itemsByGuid[id])
.map((id) => itemsByGuid[id] || activityGroups[id])

const filterUndefined = taskGroupsMarkedCompleted.filter(
(taskgroup) => taskgroup !== undefined
)

const filteredTaskGroupsMarkedCompleted = filterUndefined.filter(
(taskgroup) =>
!taskgroup.age_group || Object.keys(taskgroup.age_group).length > 0
)

const Lists = () => {
/* If the user navigates to this page too fast or reloads the page all the data is not available.
Expand Down Expand Up @@ -237,22 +246,24 @@ const Member = () => {
completedTaskGroupsGuids={completedTaskGroupsGuids}
/>
)}
{taskGroupsMarkedCompleted &&
taskGroupsMarkedCompleted.map((taskGroup) => {
if (!taskGroup) return null
{filteredTaskGroupsMarkedCompleted &&
filteredTaskGroupsMarkedCompleted.map((taskGroup) => {
if (taskGroup === undefined) return null
// const group = taskGroup.item || taskGroup
return (
<ListItem
key={taskGroup.id}
guid={getItemId(taskGroup.item)}
guid={getItemId(taskGroup)}
ageGroupGuid={taskGroup.ageGroupGuid}
title={taskGroup.item.title}
title={taskGroup.title || taskGroup.item.title}
subTitle={getTermInLanguage(
translations,
'kokonaisuus-valmis'
)}
icon={getActivityGroupIcon(taskGroup.item)}
icon={getActivityGroupIcon(taskGroup.item || taskGroup)}
language={language}
itemType={ITEM_TYPES.TASK_GROUP}
actionsComponent={actionTypes.openTaskActions}
showActions
/>
)
Expand Down
29 changes: 22 additions & 7 deletions src/views/Profile/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,10 @@ const Profile = () => {
const completedTasks = Object.keys(userTasks).filter(
(guid) => userTasks[guid] === COMPLETION_STATUS.COMPLETED
)

const completedTaskGroups = Object.keys(userTaskGroups)
.filter((guid) => userTaskGroups[guid] === TASK_GROUP_STATUS.COMPLETED)
.map((id) => itemsByGuid[id])
.map((id) => itemsByGuid[id] || activityGroups[id])

const completedTaskGroupsGuids = completedTaskGroups.map(
(group) => group && group.id
Expand All @@ -162,9 +163,22 @@ const Profile = () => {

const parentTaskGroupGuids = Object.keys(taskGroupsWithChildTaskGroups)

/* Migrated data from Kuksa uses id instead of wp_guid, which is used mostly in the application, also some of the migrated activitygroups belongs to "Seikkailijat Vanha"-agegroup,
* which is not published yet - they need to be filtered. This looks messy
*/

const taskGroupsMarkedCompleted = completedTaskGroupsGuids
.filter((guid) => !parentTaskGroupGuids.includes(guid))
.map((id) => itemsByGuid[id])
.map((id) => itemsByGuid[id] || activityGroups[id])

const filterUndefined = taskGroupsMarkedCompleted.filter(
(taskgroup) => taskgroup !== undefined
)

const filteredTaskGroupsMarkedCompleted = filterUndefined.filter(
(taskgroup) =>
!taskgroup.age_group || Object.keys(taskgroup.age_group).length > 0
)

const ongoingTasks = Object.keys(userTasks).filter(
(guid) =>
Expand Down Expand Up @@ -307,22 +321,23 @@ const Profile = () => {
parentTaskGroupGuids={parentTaskGroupGuids}
/>
)}
{taskGroupsMarkedCompleted &&
taskGroupsMarkedCompleted.map((taskGroup) => {
{filteredTaskGroupsMarkedCompleted &&
filteredTaskGroupsMarkedCompleted.map((taskGroup) => {
if (!taskGroup) return null
return (
<ListItem
key={taskGroup.id}
guid={getItemId(taskGroup.item)}
guid={getItemId(taskGroup)}
ageGroupGuid={taskGroup.ageGroupGuid}
title={taskGroup.item.title}
title={taskGroup.title || taskGroup.item.title}
subTitle={getTermInLanguage(
translations,
'kokonaisuus-valmis'
)}
icon={getActivityGroupIcon(taskGroup.item)}
icon={getActivityGroupIcon(taskGroup.item || taskGroup)}
language={language}
itemType={ITEM_TYPES.TASK_GROUP}
actionsComponent={actionTypes.openTaskActions}
showActions
/>
)
Expand Down
4 changes: 3 additions & 1 deletion src/views/TaskGroup/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ const TaskGroup = () => {
<ListItem
key={task.id}
guid={taskId}
ageGroupGuid={getItemId(taskGroup.item.age_group)}
ageGroupGuid={
taskGroup.item.age_group ? getItemId(taskGroup.item.age_group) : null
}
title={task.title}
subTitle={getTermInLanguage(translations, `${task_status}`)}
language={language}
Expand Down

0 comments on commit 3f67abb

Please sign in to comment.