Skip to content

Commit

Permalink
refactor: refactor: create dedicated composable for vote voting power
Browse files Browse the repository at this point in the history
  • Loading branch information
wa0x6e committed Oct 24, 2024
1 parent 1b269b6 commit a20a5af
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 12 deletions.
10 changes: 6 additions & 4 deletions apps/ui/src/components/Modal/Vote.vue
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ const emit = defineEmits<{
const { vote } = useActions();
const { web3 } = useWeb3();
const {
votingPower,
fetch: fetchVotingPower,
getProposalVp,
fetchProposalVp,
reset: resetVotingPower
} = useVotingPower();
} = useVoteVotingPower();
const proposalsStore = useProposalsStore();
const { loadVotes, votes } = useAccount();
const route = useRoute();
Expand All @@ -55,6 +55,8 @@ const formValidator = getValidator({
}
});
const votingPower = computed(() => getProposalVp(props.proposal));
const formattedVotingPower = computed(() =>
getFormattedVotingPower(votingPower.value)
);
Expand Down Expand Up @@ -124,7 +126,7 @@ async function handleConfirmed(tx?: string | null) {
}
function handleFetchVotingPower() {
fetchVotingPower(props.proposal);
fetchProposalVp(props.proposal);
}
watch(
Expand Down
65 changes: 65 additions & 0 deletions apps/ui/src/composables/useVoteVotingPower.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { supportsNullCurrent } from '@/networks';
import { getIndex } from '@/stores/votingPowers';
import { NetworkID, Proposal, Space } from '@/types';

export function useVoteVotingPower() {
const votingPowersStore = useVotingPowersStore();
const { web3 } = useWeb3();
const { getCurrent } = useMetaStore();

function latestBlock(network: NetworkID) {
return supportsNullCurrent(network) ? null : getCurrent(network) || 0;
}

function proposalSnapshot(proposal: Proposal) {
return (
(proposal.state === 'pending' ? null : proposal.snapshot) ||
latestBlock(proposal.network)
);
}

function fetchProposalVp(proposal: Proposal) {
if (!web3.value.account) return;

votingPowersStore.fetch(
proposal,
web3.value.account,
proposalSnapshot(proposal)
);
}

function fetchSpaceVp(space: Space) {
if (!web3.value.account) return;

votingPowersStore.fetch(
space,
web3.value.account,
latestBlock(space.network)
);
}

function getProposalVp(proposal: Proposal) {
return votingPowersStore.votingPowers.get(
getIndex(proposal.space, proposalSnapshot(proposal))
);
}

function getSpaceVp(space: Space) {
return votingPowersStore.votingPowers.get(
getIndex(space, latestBlock(space.network))
);
}

function reset() {
votingPowersStore.reset();
}

watch(
() => web3.value.account,
account => {
if (!account) reset();
}
);

return { getProposalVp, getSpaceVp, fetchProposalVp, fetchSpaceVp, reset };
}
14 changes: 10 additions & 4 deletions apps/ui/src/views/Proposal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ const props = defineProps<{
const route = useRoute();
const proposalsStore = useProposalsStore();
const {
votingPower,
fetch: fetchVotingPower,
getProposalVp,
fetchProposalVp,
reset: resetVotingPower
} = useVotingPower();
} = useVoteVotingPower();
const { setTitle } = useTitle();
const { web3 } = useWeb3();
const { modalAccountOpen } = useModal();
Expand All @@ -39,6 +39,12 @@ const discussion = computed(() => {
return sanitizeUrl(proposal.value.discussion);
});
const votingPower = computed(() => {
if (!proposal.value) return;
return getProposalVp(proposal.value);
});
const votingPowerDecimals = computed(() => {
if (!proposal.value) return 0;
return Math.max(
Expand Down Expand Up @@ -73,7 +79,7 @@ async function handleVoteSubmitted() {
function handleFetchVotingPower() {
if (!proposal.value) return;
fetchVotingPower(proposal.value);
fetchProposalVp(proposal.value);
}
watch(
Expand Down
10 changes: 6 additions & 4 deletions apps/ui/src/views/Space/Proposals.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ const props = defineProps<{ space: Space }>();
const { setTitle } = useTitle();
const {
votingPower,
fetch: fetchVotingPower,
getSpaceVp,
fetchSpaceVp,
reset: resetVotingPower
} = useVotingPower();
} = useVoteVotingPower();
const { web3 } = useWeb3();
const router = useRouter();
const route = useRoute();
Expand All @@ -27,14 +27,16 @@ const proposalsRecord = computed(
() => proposalsStore.proposals[`${props.space.network}:${props.space.id}`]
);
const votingPower = computed(() => getSpaceVp(props.space));
async function handleEndReached() {
if (!proposalsRecord.value?.hasMoreProposals) return;
proposalsStore.fetchMore(props.space.id, props.space.network);
}
function handleFetchVotingPower() {
fetchVotingPower(props.space);
fetchSpaceVp(props.space);
}
watch(
Expand Down

0 comments on commit a20a5af

Please sign in to comment.