This repository has been archived by the owner on Jun 28, 2024. It is now read-only.
forked from picsoritdidnthappen/poidh-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVoting.tsx
131 lines (117 loc) · 3.65 KB
/
Voting.tsx
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
import { useDynamicContext } from '@dynamic-labs/sdk-react-core';
import { PieChart } from '@mui/x-charts/PieChart';
import React, { useEffect, useState } from 'react';
import {
bountyVotingTracker,
resolveVote,
voteClaim,
} from '@/app/context/web3';
import { VotingTracker } from '@/types/web3';
interface VotingProps {
bountyId: string;
}
const Voting: React.FC<VotingProps> = ({ bountyId }) => {
const [votingData, setVotingData] = useState<VotingTracker | null>(null);
const { primaryWallet } = useDynamicContext();
const weiToEth = (weiValue: string) => parseFloat(weiValue) / 10 ** 18;
useEffect(() => {
if (bountyId) {
(async () => {
const voting = await bountyVotingTracker(bountyId);
setVotingData({
...voting,
yes: weiToEth(voting.yes).toString(),
no: weiToEth(voting.no).toString(),
deadline: new Date(parseInt(voting.deadline) * 1000).toLocaleString(),
});
})();
}
}, [bountyId]);
const voteYes = async () => {
if (!bountyId || !primaryWallet) {
alert('Please connect wallet');
return;
}
try {
await voteClaim(primaryWallet, bountyId, true);
alert('Vote made successfully!');
} catch (error) {
console.error('Error vote:', error);
alert('Failed to vote');
}
};
const voteNo = async () => {
if (!bountyId || !primaryWallet) {
alert('Please connect wallet');
return;
}
try {
await voteClaim(primaryWallet, bountyId, false);
alert('Vote made successfully!');
} catch (error) {
console.error('Error vote:', error);
alert('Failed to vote');
}
};
const resolveVoteHandle = async () => {
if (!bountyId || !primaryWallet) {
alert('Please connect wallet');
return;
}
try {
await resolveVote(primaryWallet, bountyId);
alert('Vote resolve successfully!');
} catch (error) {
console.error('Error resolve vote:', error);
alert('Failed to resolve vote');
}
};
return (
<div className='col-span-12 lg:col-span-3 p-5 lg:p-0 '>
{votingData ? (
<>
<div className='flex items-center mb-5'>
<PieChart
series={[
{
data: [
{ id: 0, value: weiToEth(votingData.yes), label: 'Yes' },
{ id: 1, value: weiToEth(votingData.no), label: 'No' },
],
},
]}
width={400}
height={200}
/>
</div>
<div>Yes votes: {votingData.yes} degen</div>
<div>No votes: {votingData.no} degen</div>
<div className='flex flex-row gap-x-5 '>
<button
className='border mt-5 border-white rounded-full px-5 py-2 flex justify-between items-center backdrop-blur-sm bg-[#D1ECFF]/20 w-fit'
onClick={voteYes}
>
yes
</button>
<button
className='border mt-5 border-white rounded-full px-5 py-2 flex justify-between items-center backdrop-blur-sm bg-[#D1ECFF]/20 w-fit'
onClick={voteNo}
>
no
</button>
<button
className='border mt-5 border-white rounded-full px-5 py-2 flex justify-between items-center backdrop-blur-sm bg-[#D1ECFF]/20 w-fit'
onClick={resolveVoteHandle}
>
resolve vote
</button>
</div>
<div className='mt-5 '>Deadline: {votingData.deadline}</div>
</>
) : (
<div>Loading voting data...</div>
)}
</div>
);
};
export default Voting;