Skip to content

Commit

Permalink
feat: my kiva first login for guest claim flow (#5686)
Browse files Browse the repository at this point in the history
* feat: my kiva first login for guest claim flow

* fix: ensure scroll if passing hash only

* feat: unit tests added for first login function at my kiva utils
  • Loading branch information
roger-in-kiva authored Nov 18, 2024
1 parent bf575d4 commit 0e3dff4
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/graphql/query/myKiva.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ query myKivaQuery {
id
url
}
memberSince
}
userAccount {
id
Expand Down
49 changes: 44 additions & 5 deletions src/pages/Portfolio/MyKiva/MyKivaPage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ import useBadgeData, { MY_IMPACT_JOURNEYS_ID, MY_ACHIEVEMENTS_ID } from '#src/co
import EarnedBadgesSection from '#src/components/MyKiva/EarnedBadgesSection';
import { STATE_JOURNEY, STATE_EARNED, STATE_IN_PROGRESS } from '#src/composables/useBadgeModal';
import useUserPreferences from '#src/composables/useUserPreferences';
import { hasLoanFunFactFootnote } from '#src/util/myKivaUtils';
import { hasLoanFunFactFootnote, isFirstLogin } from '#src/util/myKivaUtils';
import {
ref,
computed,
Expand All @@ -170,6 +170,7 @@ const MY_KIVA_EXP_KEY = 'my_kiva_page';
const apollo = inject('apollo');
const $kvTrackEvent = inject('$kvTrackEvent');
const kvAuth0 = inject('kvAuth0');
const {
fetchAchievementData,
Expand Down Expand Up @@ -301,6 +302,46 @@ const saveMyKivaToUserPreferences = () => {
}
};
const badgesAchieved = computed(() => {
const completedBadgesArr = [];
badgeData.value.forEach(badge => {
if (badge.achievementData?.tiers?.length) {
const { tiers } = badge.achievementData;
tiers.forEach(tierObj => {
if (tierObj.completedDate) {
completedBadgesArr.push(badge);
}
});
}
if (badge.achievementData?.milestoneProgress?.length) {
const earnedAtDate = badge.achievementData?.milestoneProgress?.[0]?.earnedAtDate;
if (earnedAtDate) {
completedBadgesArr.push(badge);
}
}
});
return completedBadgesArr;
});
const scrollToTarget = target => {
const targetElement = document.getElementById(target);
if (targetElement) {
targetElement.scrollIntoView({ behavior: 'smooth' });
}
};
const checkGuestAchievementsToScroll = () => {
const lastLogin = kvAuth0.user?.exp;
const memberSince = lender.value?.memberSince;
if (isFirstLogin(lastLogin, memberSince)) {
const numberOfBadges = badgesAchieved.value.length;
const sectionToScrollTo = numberOfBadges.value === 1 ? MY_IMPACT_JOURNEYS_ID : MY_ACHIEVEMENTS_ID;
scrollToTarget(sectionToScrollTo);
}
};
onMounted(async () => {
trackExperimentVersion(
apollo,
Expand All @@ -323,10 +364,8 @@ watch(isAchievementDataLoaded, () => {
nextTick(() => {
// Scroll to section once async data is loaded
const targetId = window?.location?.hash?.replace('#', '');
const targetElement = document?.getElementById(targetId);
if (targetElement) {
targetElement.scrollIntoView({ behavior: 'smooth' });
}
if (targetId) scrollToTarget(targetId);
checkGuestAchievementsToScroll();
});
}
});
Expand Down
23 changes: 23 additions & 0 deletions src/util/myKivaUtils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import { differenceInMinutes, fromUnixTime } from 'date-fns';

const FIRST_LOGIN_THRESHOLD = 5;

/**
* Determines whether the provided loan needs a footnote
*
Expand All @@ -19,3 +23,22 @@ export const hasLoanFunFactFootnote = loan => {
return false;
}
};

/**
* Determines whether is first login for the user
*
* @param lastLogin last login time from token access
* @param memberSince member since time from user data
* @returns Whether the user is logging in for the first time
*/
export const isFirstLogin = (lastLogin, memberSince) => {
const lastLoginDate = fromUnixTime(lastLogin);
lastLoginDate.setHours(lastLoginDate.getHours() - 1);

const minutesDiff = differenceInMinutes(
lastLoginDate,
new Date(memberSince),
);

return minutesDiff < FIRST_LOGIN_THRESHOLD;
};
27 changes: 26 additions & 1 deletion test/unit/specs/util/myKivaUtils.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { hasLoanFunFactFootnote } from '#src/util/myKivaUtils';
import { getUnixTime } from 'date-fns';
import { hasLoanFunFactFootnote, isFirstLogin } from '#src/util/myKivaUtils';

describe('myKivaUtils.js', () => {
describe('hasLoanFunFactFootnote', () => {
Expand Down Expand Up @@ -88,4 +89,28 @@ describe('myKivaUtils.js', () => {
expect(result).toBe(false);
});
});

describe('isFirstLogin', () => {
it('should return true for first login user', () => {
const memberSince = new Date();
let lastLogin = new Date(memberSince);
lastLogin.setMinutes(lastLogin.getMinutes() + 62);
lastLogin = getUnixTime(lastLogin);

const result = isFirstLogin(lastLogin, memberSince);

expect(result).toBe(true);
});

it('should return false if not first login user', () => {
const memberSince = new Date();
let lastLogin = new Date(memberSince);
lastLogin.setMinutes(lastLogin.getMinutes() + 82);
lastLogin = getUnixTime(lastLogin);

const result = isFirstLogin(lastLogin, memberSince);

expect(result).toBe(false);
});
});
});

0 comments on commit 0e3dff4

Please sign in to comment.