Skip to content

Commit

Permalink
feat(index): rearrange index, add premium card to earn-with-bee section
Browse files Browse the repository at this point in the history
  • Loading branch information
shrpne committed Apr 13, 2023
1 parent f75b565 commit 9993a3a
Show file tree
Hide file tree
Showing 9 changed files with 264 additions and 111 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/assets/css/onsen.css
/static/css/
/static/img/
/types
/tmp/
/.env
sw.*
Expand Down
44 changes: 44 additions & 0 deletions components/CardPremium.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<script>
import {getCurrentInstance} from 'vue';
import useStakeByLockList from '~/composables/use-stake-by-lock-list.js';
import Card from '~/components/Card.vue';
export default {
components: {
Card,
},
setup() {
const vm = getCurrentInstance()?.proxy;
const {
lockListPromise,
premiumCard,
prepareStakeCardList,
} = useStakeByLockList({
$td: vm.$td,
initFetchAddress: vm.$store.getters.address,
});
return {
lockListPromise,
premiumCard,
prepareStakeCardList,
};
},
fetch() {
return this.lockListPromise;
},
data() {
return {
};
},
computed: {
},
methods: {
},
};
</script>
<template>
<Card :card="premiumCard"/>
</template>
108 changes: 27 additions & 81 deletions components/InvestmentList.vue
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
<script>
import {getCurrentInstance} from 'vue';
import {getAddressLockList, getPremiumLevel} from '~/api/staking.js';
import {fillCardWithCoin, flatCardList} from '~/data/cards.js';
import {getErrorText} from '~/assets/server-error.js';
import {PREMIUM_STAKE_PROGRAM_ID} from '~/assets/variables.js';
import {pretty} from '~/assets/utils.js';
import {deepMerge} from '~/assets/utils/collection.js';
import useStakeByLockList from '~/composables/use-stake-by-lock-list.js';
import BaseLoader from '~/components/base/BaseLoader.vue';
import BaseTabs from '~/components/base/BaseTabs.vue';
import Card from '~/components/Card.vue';
Expand All @@ -25,48 +27,41 @@ export default {
Card,
PortfolioListItem,
},
setup() {
const vm = getCurrentInstance()?.proxy;
const {
lockListPromise,
lockList,
coinLockList,
prepareStakeCardList,
} = useStakeByLockList({
$td: vm.$td,
initFetchAddress: vm.$store.getters.address,
});
return {
lockListPromise,
lockList,
coinLockList,
prepareStakeCardList,
};
},
fetch() {
const portfolioListPromise = this.$store.dispatch('portfolio/fetchConsumerPortfolioList')
.then((portfolioInfo) => {
this.portfolioList = Object.freeze(portfolioInfo.list) || [];
});
const lockListPromise = getAddressLockList(this.$store.getters.address)
.then((lockList) => {
this.lockList = Object.freeze(lockList);
});
return Promise.all([portfolioListPromise, lockListPromise]);
return Promise.all([portfolioListPromise, this.lockListPromise]);
},
data() {
return {
selectedFilter: FILTERS.ALL,
/** @type {Array<ConsumerPortfolio>} */
portfolioList: [],
/** @type {Array<StakingProgramAddressLock>} */
lockList: [],
};
},
computed: {
coinLockList() {
const result = {};
this.lockList.forEach((lockItem) => {
const isPremium = lockItem.program.id === PREMIUM_STAKE_PROGRAM_ID;
const coinSymbol = isPremium ? PREMIUM_STAKE_PROGRAM_ID : lockItem.program.lockCoin.symbol;
if (!result[coinSymbol]) {
result[coinSymbol] = isPremium ? this.getEmptyPremiumCard(coinSymbol, lockItem) : this.getEmptyStakeCard(coinSymbol, lockItem);
}
result[coinSymbol].amount += Number(lockItem.amount);
if (isPremium) {
result[coinSymbol].title = 'LEVEL ' + getPremiumLevel(result[coinSymbol].amount);
} else {
// use latest program to ensure it is actual (it is fallback if nothing found in card-data)
result[coinSymbol].programId = Math.max(lockItem.program.id, result[coinSymbol].programId);
result[coinSymbol].action = `/stake/${result[coinSymbol].programId}`;
}
});
return Object.values(result);
},
coinDelegationList() {
const result = {};
this.$store.state.stakeList.forEach((delegationItem) => {
Expand All @@ -79,20 +74,7 @@ export default {
return Object.values(result);
},
stakeCardList() {
return [].concat(this.coinLockList, this.coinDelegationList)
.map((item) => {
item = JSON.parse(JSON.stringify(item));
item.stats.value = pretty(item.amount);
// get latest actual staking program from card-data
const cardData = flatCardList.find((data) => data.coin === item.coin && data.actionType === item.actionType);
if (cardData) {
// overwrite action to ensure latest actual
item.action = cardData.action;
item.description = cardData.description;
item.ru = deepMerge(item.ru, cardData.ru);
}
return item;
});
return this.prepareStakeCardList([].concat(this.coinLockList, this.coinDelegationList));
},
filteredListLength() {
if (this.selectedFilter === FILTERS.PORTFOLIO) {
Expand All @@ -116,46 +98,10 @@ export default {
},
methods: {
getErrorText,
getEmptyStakeCard(coinSymbol, lockItem) {
return fillCardWithCoin({
amount: 0,
coin: coinSymbol,
// store previous item programId to compare it later
programId: lockItem.program.id,
// dummy action to fill correct actionType
action: `/stake/${lockItem.program.id}`,
caption: 'Stake & Earn',
stats: {
caption: 'Total staked',
value: 0,
},
ru: {
caption: 'Стейкинг',
stats: {
caption: 'Общий стейк',
},
},
buttonLabel: this.$td('Stake more', 'index.stake-more'),
});
},
getEmptyPremiumCard(coinSymbol, lockItem) {
return {
...this.getEmptyStakeCard(coinSymbol, lockItem),
caption: 'Premium',
title: 'LEVEL 0',
description: 'Premium is extended account that allows you to get extra rewards without lifting a finger and enjoy additional features.',
icon: '/img/icon-premium.svg',
action: `/premium`,
ru: {
description: 'Premium – это расширенный аккаунт, который позволит получать дополнительный доход, а также добавит новые функции.',
caption: 'Premium',
stats: {
caption: 'Общий стейк',
},
},
buttonLabel: this.$td('Upgrade your level', 'premium.card-update-button'),
};
},
/**
* @param {string} coinSymbol
* @return {CardListItem}
*/
getEmptyDelegationCard(coinSymbol) {
return fillCardWithCoin({
amount: 0,
Expand Down
130 changes: 130 additions & 0 deletions composables/use-stake-by-lock-list.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
import {ref, computed} from 'vue';
import {getAddressLockList, getPremiumLevel} from '~/api/staking.js';
import {PREMIUM_STAKE_PROGRAM_ID} from '~/assets/variables.js';
import {fillCardWithCoin, flatCardList} from '~/data/cards.js';
import {pretty} from '~/assets/utils.js';
import {deepMerge} from '~/assets/utils/collection.js';

/**
* @template T
* @typedef {import('vue').Ref} Ref
*/

/** @type {Ref<Array<StakingProgramAddressLock>>} */
const lockList = ref([]);

export default function useStakeByLockList({$td, initFetchAddress}) {
$td = typeof $td === 'function' ? $td : (val) => val;

// reduce lock list to on item per coin
/** @type {ComputedRef<Array<CardListItem>>} */
const coinLockList = computed(() => {
const result = {};
lockList.value.forEach((lockItem) => {
const isPremium = lockItem.program.id === PREMIUM_STAKE_PROGRAM_ID;
const coinSymbol = isPremium ? PREMIUM_STAKE_PROGRAM_ID : lockItem.program.lockCoin.symbol;
if (!result[coinSymbol]) {
result[coinSymbol] = isPremium ? getEmptyPremiumCard() : getEmptyStakeCard(coinSymbol, lockItem.program.id);
}
result[coinSymbol].amount += Number(lockItem.amount);
if (isPremium) {
result[coinSymbol].title = 'LEVEL ' + getPremiumLevel(result[coinSymbol].amount);
} else {
// use latest program to ensure it is actual (it is fallback if nothing found in card-data)
result[coinSymbol].programId = Math.max(lockItem.program.id, result[coinSymbol].programId);
result[coinSymbol].action = `/stake/${result[coinSymbol].programId}`;
}
});
return Object.values(result);
});

const premiumCard = computed(() => {
const found = coinLockList.value.find((item) => item.programId === PREMIUM_STAKE_PROGRAM_ID);
return prepareStakeCardList([found || getEmptyPremiumCard()])[0];
});

function prepareStakeCardList(list) {
return list
.map((item) => {
item = JSON.parse(JSON.stringify(item));
item.stats.value = pretty(item.amount);
// get latest actual staking program from card-data
const cardData = flatCardList.find((data) => data.coin === item.coin && data.actionType === item.actionType);
if (cardData) {
// overwrite action to ensure latest actual
item.action = cardData.action;
item.description = cardData.description;
item.ru = deepMerge(item.ru, cardData.ru);
}
return item;
});
}

const fetchLockList = (address) => getAddressLockList(address)
.then((lockListResult) => {
lockList.value = Object.freeze(lockListResult);
});

/**
* @param {string|number} coinSymbol
* @param {number} programId
* @return {CardListItem}
*/
function getEmptyStakeCard(coinSymbol, programId) {
return fillCardWithCoin({
amount: 0,
coin: coinSymbol,
// store previous item programId to compare it later
programId,
// dummy action to fill correct actionType
action: `/stake/${programId}`,
caption: 'Stake & Earn',
stats: {
caption: 'Total staked',
value: 0,
},
ru: {
caption: 'Стейкинг',
stats: {
caption: 'Общий стейк',
},
},
buttonLabel: $td('Stake more', 'index.stake-more'),
});
}
/**
* @return {CardListItem}
*/
function getEmptyPremiumCard() {
return {
...getEmptyStakeCard(PREMIUM_STAKE_PROGRAM_ID, PREMIUM_STAKE_PROGRAM_ID),
caption: 'Premium',
title: 'LEVEL 0',
description: 'Premium is extended account that allows you to get extra rewards without lifting a finger and enjoy additional features.',
icon: '/img/icon-premium.svg',
action: `/premium`,
ru: {
description: 'Premium – это расширенный аккаунт, который позволит получать дополнительный доход, а также добавит новые функции.',
caption: 'Premium',
stats: {
caption: 'Общий стейк',
},
},
buttonLabel: $td('Upgrade your level', 'premium.card-update-button'),
};
}

let lockListPromise;
if (initFetchAddress) {
lockListPromise = fetchLockList(initFetchAddress);
}

return {
lockListPromise,
lockList,
coinLockList,
premiumCard,
prepareStakeCardList,
fetchLockList,
};
}
4 changes: 3 additions & 1 deletion jsdoc.config.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
{
"help": "https://jsdoc.app/about-configuring-jsdoc.html",
"plugins": [],
"plugins": [
],
"recurseDepth": 10,
"source": {
"include": [
"api",
"data",
"assets/variables.js"
],
"exclude": [
Expand Down
13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@
"imagemin-svgo": "^10.0.1",
"jest": "^29.5.0",
"jsdoc": "^3.6.11",
"jsdoc-plugin-intersection": "^1.0.4",
"less-loader": "^7",
"minter-js-hub": "^0.0.6",
"node-jq": "^2.3.5",
Expand Down
Loading

0 comments on commit 9993a3a

Please sign in to comment.