Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: mykiva stacked badges #5825

Merged
merged 2 commits into from
Feb 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
169 changes: 40 additions & 129 deletions src/components/MyKiva/EarnedBadgesSection.vue
Original file line number Diff line number Diff line change
@@ -1,150 +1,75 @@
<template>
<section
v-if="completedBadges.length"
class="tw-bg-white tw-py-2"
class="tw-py-2"
>
<MyKivaContainer>
<div class="tw-my-3">
<h3
class="tw-text-center tw-mb-2"
>
My achievements
</h3>
<div>
<div class="tw-w-full tw-inline-flex tw-flex-wrap tw-justify-center tw-gap-2.5">
<div
v-for="badge in visibleBadges"
:key="badge.id"
class="badge-container tw-flex tw-flex-col tw-justify-between tw-p-1.5 tw-rounded"
>
<div
class="tw-p-1 tw-cursor-pointer"
style="height: 148px;"
@click="clickBadge(badge)"
>
<img
:src="getBadgeImgUrl(badge)"
class="tw-h-full tw-mx-auto"
>
</div>
<div class="tw-flex tw-flex-col tw-gap-0.5 tw-font-medium">
<span class="tw-text-base !tw-font-medium tw-text-center">
{{ getBadgeTitle(badge) }}
</span>
<span class="tw-mx-auto tw-text-small">
{{ getBadgeDate(badge) }}
</span>
</div>
</div>
</div>
<div
v-if="completedBadges.length > visibleLimit && visibleBadges.length < completedBadges.length"
class="tw-flex tw-justify-center"
>
<kv-button
class="tw-mt-2 tw-mx-auto"
variant="secondary"
v-kv-track-event="['portfolio', 'click', 'load-more-badges']"
@click="loadMoreBadges"
>
Load more
</kv-button>
</div>
</div>
<div class="tw-my-3">
<h3
class="tw-text-center tw-mb-2"
>
My achievements
</h3>
<!-- eslint-disable-next-line max-len -->
<div class="tw-w-full tw-inline-flex tw-flex-wrap tw-justify-center lg:tw-justify-start tw-gap-6 md:tw-gap-10 md:tw-px-2 tw-mt-2">
<BadgeCard
v-for="badge in showedBadges"
:key="badge.id"
:badge="badge"
@click="clickBadge"
/>
</div>
</MyKivaContainer>
</div>
</section>
</template>

<script setup>
import {
computed,
toRefs,
ref,
inject,
} from 'vue';
import { format } from 'date-fns';
import { KvButton } from '@kiva/kv-components';
import MyKivaContainer from '#src/components/MyKiva/MyKivaContainer';
import useBadgeData from '#src/composables/useBadgeData';
import { defaultBadges } from '#src/util/achievementUtils';
import BadgeCard from '#src/components/LenderProfile/BadgeCard';

const $kvTrackEvent = inject('$kvTrackEvent');

const emit = defineEmits(['badge-clicked']);

const props = defineProps({
badgesData: {
completedBadges: {
type: Array,
default: () => ([])
},
});

const visibleLimit = ref(6);
const visibleOffset = ref(1);
const { completedBadges } = toRefs(props);

const { badgesData } = toRefs(props);
const sortedTieredBadges = computed(() => {
const tieredBadges = [];
defaultBadges.forEach(element => {
const filteredBadges = completedBadges.value
.filter(badge => badge.id === element)
.sort((a, b) => b.level - a.level);

const { getTierBadgeDataByLevel } = useBadgeData();

const completedBadges = computed(() => {
const completedBadgesArr = [];

badgesData.value.forEach(badge => {
if (badge.achievementData?.tiers?.length) {
const { tiers } = badge.achievementData;
tiers.forEach(tier => {
if (tier.completedDate) {
completedBadgesArr.push({
...badge,
earnedAtDate: tier.completedDate,
level: tier.level,
});
}
});
}
if (badge.achievementData?.milestoneProgress?.length) {
const earnedAtDate = badge.achievementData?.milestoneProgress?.[0]?.earnedAtDate;
if (earnedAtDate) {
completedBadgesArr.push({
...badge,
earnedAtDate,
level: 0,
});
}
if (filteredBadges.length > 0) {
tieredBadges.push(filteredBadges[0]);
}
});

// ascending chronological order
completedBadgesArr.sort((a, b) => {
return new Date(a.earnedAtDate) - new Date(b.earnedAtDate);
});

return completedBadgesArr;
return tieredBadges;
});

const visibleBadges = computed(() => completedBadges.value.slice(0, visibleLimit.value * visibleOffset.value));

const getBadgeTitle = badge => {
const levelData = getTierBadgeDataByLevel(badge, badge.level);
return levelData.tierName;
};

const getBadgeImgUrl = badge => {
if (badge.level === 0) {
return badge?.contentfulData?.[0]?.imageUrl ?? '';
}
const badgeData = badge?.contentfulData?.find(data => data.level === badge.level);
return badgeData?.imageUrl ?? '';
};

const getBadgeDate = badge => {
const earnedAtDate = badge.earnedAtDate ? Date.parse(badge.earnedAtDate) : new Date();
return format(earnedAtDate, 'MMM yyyy');
};
const sortedEventBadges = computed(() => {
return completedBadges.value
.filter(b => b.achievementData?.tiers?.length === 0)
.sort((a, b) => new Date(a.earnedAtDate) - new Date(b.earnedAtDate));
});

const loadMoreBadges = () => {
visibleOffset.value += 1;
};
const showedBadges = computed(() => {
return [
...sortedTieredBadges.value,
...sortedEventBadges.value
];
});

const clickBadge = badge => {
$kvTrackEvent(
Expand All @@ -157,17 +82,3 @@ const clickBadge = badge => {
emit('badge-clicked', badge);
};
</script>

<style lang="postcss" scoped>
.badge-container {
width: 157px;

@media (width >= 410px) {
width: 175px;
}

@screen md {
width: 220px;
}
}
</style>
11 changes: 6 additions & 5 deletions src/pages/Portfolio/MyKiva/MyKivaPage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,12 @@
/>
</div>
</section>
<EarnedBadgesSection
:id="MY_ACHIEVEMENTS_ID"
:completed-badges="completedBadges"
@badge-clicked="handleEarnedBadgeClicked"
/>
</MyKivaContainer>
<EarnedBadgesSection
:id="MY_ACHIEVEMENTS_ID"
:badges-data="badgeData"
@badge-clicked="handleEarnedBadgeClicked"
/>
</template>
<div v-if="showLoanFootnote" class="tw-bg-white tw-text-small tw-py-4 md:tw-py-2.5">
<MyKivaContainer>
Expand Down Expand Up @@ -176,6 +176,7 @@ const {
fetchContentfulData,
badgeAchievementData,
badgeData,
completedBadges,
} = useBadgeData(apollo);

const { saveUserPreferences } = useUserPreferences(apollo);
Expand Down
Loading