Skip to content

Commit

Permalink
feat: update level name util to support badge object used on MyKiva T…
Browse files Browse the repository at this point in the history
…Y page
  • Loading branch information
dyersituations committed Dec 17, 2024
1 parent abbe776 commit d967d57
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 14 deletions.
6 changes: 5 additions & 1 deletion src/components/Thanks/MyKiva/ThanksBadges.vue
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ const {
badgeData,
getHighestPriorityDisplayBadge,
getLastCompletedBadgeLevelData,
getTierBadgeDataByLevel,
} = useBadgeData();
const badgeIdsAchieved = ref(props.badgesAchieved.map(b => b.achievementId));
Expand Down Expand Up @@ -297,7 +298,10 @@ const displayedBadgeData = computed(() => {
const badgeImageUrl = computed(() => displayedBadgeData.value.contentfulData?.imageUrl ?? '');
const badgeLevelName = computed(() => displayedBadgeData.value.levelName ?? '');
const badgeLevelName = computed(() => {
const levelData = getTierBadgeDataByLevel(displayedBadgeData.value, displayedBadgeData.value.level);
return levelData.tierName;
});
const hasBadgeData = computed(() => !!badgeLevelName.value);
Expand Down
30 changes: 19 additions & 11 deletions src/composables/useBadgeData.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,18 @@ export default function useBadgeData() {
*/
const badgeData = computed(() => combineBadgeData(badgeAchievementData.value, badgeContentfulData.value));

/**
* Returns the formatted name for the provided Contentful data
*
* @param contentfulData The Contentful data for the badge and/or badge level
* @returns Formatted badge and/or badge level name
*/
const getLevelName = contentfulData => {
const levelText = contentfulData?.levelName ? ` (level ${contentfulData?.levelName})` : '';
const challengeName = contentfulData?.challengeName ?? '';
return levelText ? `${challengeName}${levelText}` : challengeName;
};

/**
* Gets the badge data with specific contentful and achievement data for the tier
*
Expand All @@ -260,19 +272,15 @@ export default function useBadgeData() {
const getTierBadgeDataByLevel = (badge, level) => {
const tierIndex = badge?.achievementData?.tiers?.findIndex(t => t?.level === level);
const achievementData = badge?.achievementData?.tiers?.[tierIndex];
const contentfulData = badge?.contentfulData?.[tierIndex];
const levelText = typeof contentfulData?.levelName !== 'undefined'
? ` (level ${contentfulData?.levelName})`
: '';
const contentfulData = badge?.contentfulData?.[tierIndex]
// The TY page provides a badge object with a single contentfulData property
|| (typeof badge?.contentfulData?.length === 'undefined' ? badge.contentfulData : undefined);

return {
...badge,
contentfulData,
achievementData,
// Handle both tiered and old badges (use challenge name for non-tiered)
tierName: contentfulData?.challengeName
? `${(contentfulData.challengeName)}${levelText}`
: (badge?.challengeName ?? ''),
tierName: getLevelName(contentfulData) || badge.challengeName,
};
};

Expand Down Expand Up @@ -347,7 +355,7 @@ export default function useBadgeData() {
return {
...badge,
contentfulData,
levelName: contentfulData.challengeName,
levelName: getLevelName(contentfulData),
};
}
} else if (badge?.achievementData?.tiers?.length) {
Expand All @@ -366,8 +374,7 @@ export default function useBadgeData() {
...badge,
contentfulData,
achievementData: tiers[0],
// eslint-disable-next-line max-len
levelName: `${(contentfulData.challengeName ?? '')}${(contentfulData.levelName ? ' ' : '')}${(contentfulData.levelName ?? '')}`
levelName: getLevelName(contentfulData),
};
}
return {};
Expand Down Expand Up @@ -473,5 +480,6 @@ export default function useBadgeData() {
badgeLoanIdData,
isBadgeKeyValid,
completedBadges,
getLevelName,
};
}
33 changes: 31 additions & 2 deletions test/unit/specs/composables/useBadgeData.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,7 @@ describe('useBadgeData.js', () => {
...badge,
contentfulData: badge.contentfulData[1],
achievementData: badge.achievementData.tiers[1],
levelName: 'Basic needs 2'
levelName: 'Basic needs (level 2)'
});
});

Expand All @@ -591,7 +591,7 @@ describe('useBadgeData.js', () => {
...badge,
contentfulData: badge.contentfulData[1],
achievementData: badge.achievementData.tiers[1],
levelName: 'Basic needs 2'
levelName: 'Basic needs (level 2)'
});
});

Expand Down Expand Up @@ -724,4 +724,33 @@ describe('useBadgeData.js', () => {
])).toEqual([]);
});
});

describe('getLevelName', () => {
it('should return the challenge name with level name when level name is present', () => {
const { getLevelName } = useBadgeData();
const data = {
challengeName: 'Basic needs',
levelName: '1'
};
expect(getLevelName(data)).toEqual('Basic needs (level 1)');
});

it('should return only the challenge name when level name is not present', () => {
const { getLevelName } = useBadgeData();
const data = {
challengeName: 'Basic needs'
};
expect(getLevelName(data)).toEqual('Basic needs');
});

it('should return an empty string when contentfulData is undefined', () => {
const { getLevelName } = useBadgeData();
expect(getLevelName(undefined)).toEqual('');
});

it('should return an empty string when contentfulData is null', () => {
const { getLevelName } = useBadgeData();
expect(getLevelName(null)).toEqual('');
});
});
});

0 comments on commit d967d57

Please sign in to comment.