Skip to content

Commit

Permalink
Dapps loading optimizations (#927)
Browse files Browse the repository at this point in the history
* Dapps loading optimizations

* Fetch dApp from the proper network
  • Loading branch information
bobo-k2 committed Sep 19, 2023
1 parent b7a7741 commit 4f5b7f7
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 4 deletions.
38 changes: 36 additions & 2 deletions src/components/dapp-staking/dapp/Dapp.vue
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,15 @@ import DappStatistics from 'src/components/dapp-staking/dapp/DappStatistics.vue'
import DappStatsCharts from 'src/components/dapp-staking/dapp/DappStatsCharts.vue';
import ProjectDetails from 'src/components/dapp-staking/dapp/ProjectDetails.vue';
import ProjectOverview from 'src/components/dapp-staking/dapp/ProjectOverview.vue';
import { useDappRedirect, useDispatchGetDapps, useStakingList } from 'src/hooks';
import { useDappRedirect, useDispatchGetDapps, useNetworkInfo, useStakingList } from 'src/hooks';
import { Path } from 'src/router';
import { networkParam } from 'src/router/routes';
import { useStore } from 'src/store';
import { container } from 'src/v2/common';
import { DappCombinedInfo } from 'src/v2/models';
import { computed, defineComponent } from 'vue';
import { IDappStakingService } from 'src/v2/services';
import { Symbols } from 'src/v2/symbols';
import { computed, defineComponent, watch } from 'vue';
import { useRoute } from 'vue-router';
export default defineComponent({
Expand All @@ -50,6 +53,7 @@ export default defineComponent({
},
setup() {
const route = useRoute();
const { currentNetworkName } = useNetworkInfo();
useDappRedirect();
useDispatchGetDapps();
const store = useStore();
Expand Down Expand Up @@ -81,6 +85,36 @@ export default defineComponent({
return null;
});
// Fetch full dApp model from API. Initially, store contains dapp with props required for the main page.
const getDapp = async () => {
if (dapp.value?.dapp?.description) {
// Full dapp model is already loaded to the store. No need to fetch dapp from API.
return;
}
try {
store.commit('general/setLoading', true, { root: true });
const service = container.get<IDappStakingService>(Symbols.DappStakingService);
const loadedDapp = await service.getDapp(
dappAddress.value,
currentNetworkName.value.toLowerCase()
);
if (loadedDapp) {
store.commit('dapps/updateDapp', loadedDapp);
}
} finally {
store.commit('general/setLoading', false, { root: true });
}
};
watch(
[dapps],
() => {
if (dapps.value.length > 0) {
getDapp();
}
},
{ immediate: true }
);
return {
Path,
dapp,
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/dapps-staking/useDispatchGetDapps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export function useDispatchGetDapps() {

const getDapps = async (): Promise<void> => {
const isConnectedWallet = currentNetworkName.value && currentAccount.value;
if (isConnectedWallet) {
if (isConnectedWallet && dapps.value.length === 0) {
const address = !currentAccount.value ? '' : currentAccount.value;
store.dispatch('dapps/getDapps', {
network: currentNetworkName.value.toLowerCase(),
Expand Down
2 changes: 1 addition & 1 deletion src/store/dapp-staking/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ const actions: ActionTree<State, StateInterface> = {

try {
// Fetch dapps
const dappsUrl = `${TOKEN_API_URL}/v1/${network.toLowerCase()}/dapps-staking/dapps`;
const dappsUrl = `${TOKEN_API_URL}/v1/${network.toLowerCase()}/dapps-staking/dappssimple`;
const service = container.get<IDappStakingService>(Symbols.DappStakingService);
const address = isValidEvmAddress(currentAccount)
? toSS58Address(currentAccount)
Expand Down
10 changes: 10 additions & 0 deletions src/store/dapp-staking/mutations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { DappItem } from '@astar-network/astar-sdk-core';

export interface ContractsMutations<S = State> {
addDapp(state: S, payload: DappItem): void;
updateDapp(state: S, payload: DappItem): void;
addDappCombinedInfos(state: S, payload: DappCombinedInfo[]): void;
setMinimumStakingAmount(state: S, payload: string): void;
setMaxNumberOfStakersPerContract(state: S, payload: number): void;
Expand All @@ -29,6 +30,15 @@ const mutation: MutationTree<State> & ContractsMutations = {
state.dapps = [...state.dapps];
},

updateDapp(state: State, payload: DappItem) {
let dappIndex = state.dappsCombinedInfo.findIndex(
(x) => x.dapp?.address.toLowerCase() === payload.address.toLocaleLowerCase()
);
if (dappIndex !== -1) {
state.dappsCombinedInfo[dappIndex].dapp = payload;
}
},

setMinimumStakingAmount(state: State, payload: string) {
state.minimumStakingAmount = payload;
},
Expand Down

0 comments on commit 4f5b7f7

Please sign in to comment.