Skip to content

Commit 3811d94

Browse files
committed
WIP
1 parent 6d816c0 commit 3811d94

File tree

1 file changed

+103
-3
lines changed

1 file changed

+103
-3
lines changed

packages/grant-explorer/src/features/round/ViewCartPage/RoundInCart.tsx

Lines changed: 103 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,55 @@ export function RoundInCart(
6767
]);
6868

6969
const estimate = matchingEstimatesToText(matchingEstimates);
70+
const [gitcoinEnabled, setGitcoinEnabled] = React.useState(false);
71+
const [gitcoinAmount, setGitcoinAmount] = React.useState("");
72+
const [gitcoinDonationType, setGitcoinDonationType] = React.useState<
73+
"percentage" | "amount"
74+
>("percentage");
7075

71-
const totalDonationInUSD =
76+
const baseDonationInUSD =
7277
props.roundCart.reduce((acc, proj) => acc + Number(proj.amount), 0) *
7378
props.payoutTokenPrice;
7479

80+
// Helper function to convert between percentage and amount
81+
const convertAmount = (
82+
value: string,
83+
fromType: "percentage" | "amount",
84+
toType: "percentage" | "amount"
85+
) => {
86+
const numValue = Number(value) || 0;
87+
if (fromType === toType) return value;
88+
if (fromType === "percentage" && toType === "amount") {
89+
return (
90+
(baseDonationInUSD * numValue) /
91+
(100 * props.payoutTokenPrice)
92+
).toFixed(6);
93+
} else {
94+
return (
95+
(numValue * props.payoutTokenPrice * 100) /
96+
baseDonationInUSD
97+
).toFixed(2);
98+
}
99+
};
100+
101+
// Update the donation calculation
102+
const gitcoinDonationInUSD = gitcoinEnabled
103+
? gitcoinDonationType === "percentage"
104+
? (baseDonationInUSD * (Number(gitcoinAmount) || 0)) / 100
105+
: (Number(gitcoinAmount) || 0) * props.payoutTokenPrice
106+
: 0;
107+
75108
const showMatchingEstimate =
76109
matchingEstimateError === undefined &&
77110
matchingEstimates !== undefined &&
78111
round?.chainId !== 43114; // Avalanche
79112

113+
React.useEffect(() => {
114+
if (!gitcoinEnabled) {
115+
setGitcoinAmount("");
116+
}
117+
}, [gitcoinEnabled]);
118+
80119
return (
81120
<div className="my-4">
82121
{/* Round In Cart */}
@@ -128,6 +167,61 @@ export function RoundInCart(
128167
</div>
129168
);
130169
})}
170+
171+
{/* Gitcoin Donation Section */}
172+
<div className="mt-4 p-4 border-t border-gray-200">
173+
<div className="flex items-center justify-end space-x-2">
174+
<input
175+
type="checkbox"
176+
id="gitcoinDonation"
177+
className="rounded border-gray-300"
178+
checked={gitcoinEnabled}
179+
onChange={(e) => setGitcoinEnabled(e.target.checked)}
180+
/>
181+
<label htmlFor="gitcoinDonation" className="text-sm font-medium">
182+
Support Gitcoin with an additional donation
183+
</label>
184+
</div>
185+
186+
{gitcoinEnabled && (
187+
<div className="mt-2 flex items-center justify-end space-x-2">
188+
<input
189+
type="number"
190+
min="0"
191+
max={gitcoinDonationType === "percentage" ? 1000 : undefined}
192+
value={gitcoinAmount}
193+
onChange={(e) => {
194+
setGitcoinAmount(e.target.value);
195+
}}
196+
className="w-20 px-2 py-1 border rounded text-right"
197+
placeholder="0"
198+
step="any"
199+
/>
200+
<select
201+
value={gitcoinDonationType}
202+
onChange={(e) => {
203+
const newType = e.target.value as "percentage" | "amount";
204+
const newAmount = convertAmount(
205+
gitcoinAmount,
206+
gitcoinDonationType,
207+
newType
208+
);
209+
setGitcoinDonationType(newType);
210+
setGitcoinAmount(newAmount);
211+
}}
212+
className="px-2 py-1 border rounded text-sm w-40"
213+
>
214+
<option value="percentage">% of donation</option>
215+
<option value="amount">
216+
{props.selectedPayoutToken.code}
217+
</option>
218+
</select>
219+
<span className="text-sm text-gray-500">
220+
(${gitcoinDonationInUSD.toFixed(2)})
221+
</span>
222+
</div>
223+
)}
224+
</div>
131225
</div>
132226
</div>
133227
{/* Total Donations */}
@@ -167,9 +261,15 @@ export function RoundInCart(
167261
<div className="font-semibold">
168262
<p>
169263
<span className="mr-2">Total donation</span>$
170-
{isNaN(totalDonationInUSD)
264+
{isNaN(baseDonationInUSD)
171265
? "0.0"
172-
: totalDonationInUSD.toFixed(2)}
266+
: baseDonationInUSD.toFixed(2)}
267+
{gitcoinEnabled && gitcoinAmount !== "" && (
268+
<span className="text-sm text-gray-500 ml-2">
269+
(plus an additional ${gitcoinDonationInUSD.toFixed(2)}{" "}
270+
Gitcoin donation)
271+
</span>
272+
)}
173273
</p>
174274
</div>
175275
</div>

0 commit comments

Comments
 (0)