Skip to content

Commit

Permalink
refactor: use single fetch and get functions
Browse files Browse the repository at this point in the history
  • Loading branch information
wa0x6e committed Nov 29, 2024
1 parent 6323779 commit f0645e0
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 33 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,8 +27,8 @@ const emit = defineEmits<{
const { vote } = useActions();
const { web3 } = useWeb3();
const {
getProposalVp,
fetchProposalVp,
get: getVotingPower,
fetch: fetchVotingPower,
reset: resetVotingPower
} = useVotingPower();
const proposalsStore = useProposalsStore();
Expand All @@ -55,7 +55,9 @@ const formValidator = getValidator({
}
});
const votingPower = computed(() => getProposalVp(props.proposal));
const votingPower = computed(() =>
getVotingPower(props.proposal.space, props.proposal)
);
const formattedVotingPower = computed(() =>
getFormattedVotingPower(votingPower.value)
Expand Down Expand Up @@ -126,7 +128,7 @@ async function handleConfirmed(tx?: string | null) {
}
function handleFetchVotingPower() {
fetchProposalVp(props.proposal);
fetchVotingPower(props.proposal.space, props.proposal);
}
watch(
Expand Down
31 changes: 9 additions & 22 deletions apps/ui/src/composables/useVotingPower.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,35 +18,22 @@ export function useVotingPower() {
);
}

function fetchProposalVp(proposal: Proposal) {
function fetch(space: Space, proposal?: Proposal) {
if (!web3.value.account) return;

votingPowersStore.fetch(
proposal,
proposal || space,
web3.value.account,
proposalSnapshot(proposal)
proposal ? proposalSnapshot(proposal) : latestBlock(space.network)
);
}

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) {
function get(space: Space, proposal?: Proposal) {
return votingPowersStore.votingPowers.get(
getIndex(space, latestBlock(space.network))
getIndex(
proposal ? proposal.space : space,
proposal ? proposalSnapshot(proposal) : latestBlock(space.network)
)
);
}

Expand All @@ -61,5 +48,5 @@ export function useVotingPower() {
}
);

return { getProposalVp, getSpaceVp, fetchProposalVp, fetchSpaceVp, reset };
return { get, fetch, reset };
}
8 changes: 4 additions & 4 deletions apps/ui/src/views/Proposal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ const props = defineProps<{
const route = useRoute();
const proposalsStore = useProposalsStore();
const {
getProposalVp,
fetchProposalVp,
get: getVotingPower,
fetch: fetchVotingPower,
reset: resetVotingPower
} = useVotingPower();
const { setTitle } = useTitle();
Expand Down Expand Up @@ -45,7 +45,7 @@ const discussion = computed(() => {
const votingPower = computed(() => {
if (!proposal.value) return;
return getProposalVp(proposal.value);
return getVotingPower(props.space, proposal.value);
});
const votingPowerDecimals = computed(() => {
Expand Down Expand Up @@ -93,7 +93,7 @@ async function handleVoteSubmitted() {
function handleFetchVotingPower() {
if (!proposal.value) return;
fetchProposalVp(proposal.value);
fetchVotingPower(props.space, proposal.value);
}
watch(
Expand Down
10 changes: 7 additions & 3 deletions apps/ui/src/views/Space/Proposals.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import { Space } from '@/types';
const props = defineProps<{ space: Space }>();
const { setTitle } = useTitle();
const { getSpaceVp, fetchSpaceVp, reset: resetVotingPower } = useVotingPower();
const {
get: getVotingPower,
fetch: fetchVotingPower,
reset: resetVotingPower
} = useVotingPower();
const { web3 } = useWeb3();
const router = useRouter();
const route = useRoute();
Expand All @@ -24,7 +28,7 @@ const proposalsRecord = computed(
() => proposalsStore.proposals[`${props.space.network}:${props.space.id}`]
);
const votingPower = computed(() => getSpaceVp(props.space));
const votingPower = computed(() => getVotingPower(props.space));
const spaceLabels = computed(() => {
if (!props.space.labels) return {};
Expand All @@ -44,7 +48,7 @@ async function handleEndReached() {
}
function handleFetchVotingPower() {
fetchSpaceVp(props.space);
fetchVotingPower(props.space);
}
watchThrottled(
Expand Down

0 comments on commit f0645e0

Please sign in to comment.