forked from snapshot-labs/snapshot-v1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuseProposalVotes.ts
137 lines (121 loc) · 3.38 KB
/
useProposalVotes.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import { Proposal, Vote, VoteFilters } from '@/helpers/interfaces';
import { VOTES_QUERY } from '@/helpers/queries';
type QueryParams = {
voter?: string;
} & Partial<VoteFilters>;
export function useProposalVotes(proposal: Proposal, loadBy = 6) {
const { profiles, loadProfiles } = useProfiles();
const { apolloQuery } = useApolloQuery();
const { resolveName } = useResolveName();
const loadingVotes = ref(false);
const loadingMoreVotes = ref(false);
const loadingUserVote = ref(false);
const votes = ref<Vote[]>([]);
const userVote = ref<Vote | null>(null);
async function _fetchVotes(queryParams: QueryParams, skip = 0) {
const response = await apolloQuery(
{
query: VOTES_QUERY,
variables: {
id: proposal.id,
first: loadBy,
skip,
orderBy: 'vp',
orderDirection: queryParams.orderDirection || 'desc',
reason_not: queryParams.onlyWithReason ? '' : undefined,
voter: queryParams.voter || undefined
}
},
'votes'
);
loadProfiles(response.map(vote => vote.voter));
return response;
}
async function _fetchVote(queryParams: QueryParams) {
const response = await apolloQuery(
{
query: VOTES_QUERY,
variables: {
id: proposal.id,
voter: queryParams.voter
}
},
'votes'
);
loadProfiles(response.map(vote => vote.voter));
return response;
}
function formatProposalVotes(votes: Vote[]) {
if (!votes?.length) return [];
return votes.map(vote => {
vote.balance = vote.vp;
vote.scores = vote.vp_by_strategy;
return vote;
});
}
async function loadVotes(filter: Partial<VoteFilters> = {}) {
if (loadingVotes.value) return;
loadingVotes.value = true;
try {
const response = await _fetchVotes(filter);
votes.value = formatProposalVotes(response);
} catch (e) {
console.log(e);
} finally {
loadingVotes.value = false;
}
}
async function loadSingleVote(search: string) {
loadingVotes.value = true;
const response = await resolveName(search);
const voter = response || search;
try {
const response = await _fetchVote({ voter });
votes.value = formatProposalVotes(response);
} catch (e) {
console.log(e);
} finally {
loadingVotes.value = false;
}
}
async function loadMoreVotes(filter: Partial<VoteFilters> = {}) {
if (loadingMoreVotes.value || loadingVotes.value) return;
loadingMoreVotes.value = true;
try {
const response = await _fetchVotes(filter, votes.value.length);
votes.value = votes.value.concat(formatProposalVotes(response));
} catch (e) {
console.log(e);
} finally {
loadingMoreVotes.value = false;
}
}
async function loadUserVote(voter: string) {
if (!voter) {
userVote.value = null;
return;
}
try {
loadingUserVote.value = true;
const response = await _fetchVote({ voter });
userVote.value = formatProposalVotes(response)[0];
} catch (e) {
console.log(e);
} finally {
loadingUserVote.value = false;
}
}
return {
votes,
profiles,
loadingVotes,
loadingMoreVotes,
loadingUserVote: computed(() => loadingUserVote.value),
userVote,
formatProposalVotes,
loadVotes,
loadMoreVotes,
loadSingleVote,
loadUserVote
};
}