Skip to content

Commit

Permalink
feat(referral): update meganet referral to multilevel
Browse files Browse the repository at this point in the history
  • Loading branch information
shrpne committed Jul 7, 2023
1 parent d213c24 commit 9a8ffd7
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 12 deletions.
26 changes: 21 additions & 5 deletions api/referral.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,33 +71,49 @@ export function getReferralList(address) {

const fastCache = new Cache({ttl: 2 * 1000, max: 100});

/**
* @typedev
*/

/**
* @param {Array<string>} refIdList
* @return {Promise<Record<string, number>>}
* @return {Promise<{
* data: Record<string, Record<number, Array<string>>>,
* launchers: Record<string, Record<number|'total', number>>
* }>}
*/
export function getReferralBonusList(refIdList) {
if (!refIdList?.length) {
throw new Error('refId needed to get statistics');
}
return instance.get(`statistic/meganet`, {
return instance.get(`statistic/launcher`, {
params: {
users: refIdList.join(','),
},
cache: fastCache,
})
.then((response) => response.data.data);
.then((response) => response.data);
}

/**
* @param {string} refId
* @return {Promise<number>}
* @return {Promise<{inviteCount: number, reward: number}>}
*/
export function getReferralBonus(refId) {
if (!refId) {
throw new Error('refId needed to get statistics');
}
return getReferralBonusList([refId])
.then((bonusMap) => Object.values(bonusMap)[0]);
.then((result) => {
const inviteLevels = Object.values(result.data)[0] || {};
// count of invites from all levels
const inviteCount = Object.values(inviteLevels).reduce((sum, level) => sum + level.length, 0);
const launchers = Object.values(result.launchers)[0];
return {
inviteCount,
reward: launchers?.total || 0,
};
});
}


Expand Down
20 changes: 17 additions & 3 deletions components/ReferralCard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ export default {
},
},
async fetch() {
if (IS_SUBAPP_MEGACHAIN) {
if (!this.isModalButton) {
this.fetchReferralList();
}
if (IS_SUBAPP_MEGACHAIN) {
if (!this.$store.state.referral.refId) {
await this.setup(true);
}
Expand All @@ -47,6 +49,12 @@ export default {
refUrl() {
return window.location.origin + this.$route.fullPath;
},
referralCount() {
// megachain program is multilevel and invites from all levels needed
return IS_SUBAPP_MEGACHAIN
? this.$store.state.referral.referralInviteCount
: (this.referralList?.length || 0);
},
},
watch: {
isConfirmModalVisible: {
Expand Down Expand Up @@ -90,6 +98,9 @@ export default {
});
},
fetchReferralList() {
if (IS_SUBAPP_MEGACHAIN) {
return;
}
return getReferralList(this.$store.getters.address)
.then((list) => {
this.referralList = list;
Expand Down Expand Up @@ -170,12 +181,15 @@ export default {
</div>
</div>
<div class="u-flex u-flex--justify-between u-flex--align-center u-mt-10" v-if="$store.state.referral.refId && referralList && $options.IS_SUBAPP_MEGACHAIN">
<p class="u-text-medium u-mt-10" v-if="$store.state.referral.refId && !$options.IS_SUBAPP_MEGACHAIN">
{{ $td(`You currently invited ${referralCount} friends`, 'referral.invited-list') }}
</p>
<div class="u-flex u-flex--justify-between u-flex--align-center u-mt-10" v-if="$store.state.referral.refId && $options.IS_SUBAPP_MEGACHAIN">
<div class="u-flex u-flex--align-center u-mr-10">
<div class="u-h--uppercase u-text-mega-muted">{{ $td('Total invites accepted', 'meganet.referral-program-total-invites') }}</div>
</div>
<div class="u-h u-h4">{{ referralList.length }}</div>
<div class="u-h u-h4">{{ referralCount }}</div>
</div>
<div class="u-flex u-flex--justify-between u-flex--align-center u-mt-10" v-if="$options.IS_SUBAPP_MEGACHAIN">
<div class="u-flex u-flex--align-center u-mr-10">
Expand Down
10 changes: 6 additions & 4 deletions store/referral.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export const state = () => ({
refId: undefined,
// ref to follow
foreignRefId: undefined,
referralInviteCount: 0,
referralBonus: 0,
});

Expand All @@ -18,8 +19,9 @@ export const mutations = {
setRefId(state, value) {
state.refId = value;
},
setReferralBonus(state, value) {
state.referralBonus = value;
setReferralBonus(state, data) {
state.referralInviteCount = data.inviteCount;
state.referralBonus = data.reward;
},
initForeignRefId(state, value) {
if (value) {
Expand Down Expand Up @@ -72,8 +74,8 @@ export const actions = {
},
fetchReferralBonus({state, commit}) {
return getReferralBonus(state.refId)
.then((bonus) => {
commit('setReferralBonus', bonus);
.then((data) => {
commit('setReferralBonus', data);
})
.catch((error) => {
console.log(error);
Expand Down

0 comments on commit 9a8ffd7

Please sign in to comment.