forked from MergeFi/frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMilestoneActions.tsx
More file actions
88 lines (81 loc) · 2.87 KB
/
Copy pathMilestoneActions.tsx
File metadata and controls
88 lines (81 loc) · 2.87 KB
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
"use client";
import { useState } from "react";
import { useRouter } from "next/navigation";
import { Button } from "@/components/ui/Button";
import { useWallet } from "@/context/WalletContext";
import { apiPost, ApiRequestError } from "@/lib/api";
export function MilestoneFundButton({ milestoneId }: { milestoneId: string }) {
const router = useRouter();
const { address, connect, connecting } = useWallet();
const [pending, setPending] = useState(false);
const [error, setError] = useState<string | null>(null);
async function handleFund() {
setError(null);
setPending(true);
try {
const walletAddress = address ?? (await connect());
if (!walletAddress) {
setError("Connect a Stellar wallet to fund this milestone.");
return;
}
await apiPost(`/milestones/${milestoneId}/fund`, {
funderAddress: walletAddress,
});
router.refresh();
} catch (err) {
setError(err instanceof ApiRequestError ? err.message : "Something went wrong.");
} finally {
setPending(false);
}
}
return (
<div className="mt-4">
<Button size="sm" variant="outline" onClick={handleFund} disabled={pending || connecting}>
{pending || connecting ? "Confirming in wallet..." : "Fund milestone"}
</Button>
{error && <p className="mt-2 text-xs text-rose-600">{error}</p>}
</div>
);
}
export function PoolDepositButton({ poolId }: { poolId: string }) {
const router = useRouter();
const { address, connect, connecting } = useWallet();
const [amount, setAmount] = useState("100");
const [pending, setPending] = useState(false);
const [error, setError] = useState<string | null>(null);
async function handleDeposit() {
setError(null);
setPending(true);
try {
const walletAddress = address ?? (await connect());
if (!walletAddress) {
setError("Connect a Stellar wallet to deposit.");
return;
}
await apiPost(`/maintenance-pools/${poolId}/deposit`, {
amount,
funderAddress: walletAddress,
});
router.refresh();
} catch (err) {
setError(err instanceof ApiRequestError ? err.message : "Something went wrong.");
} finally {
setPending(false);
}
}
return (
<div className="mt-4 flex items-center gap-2">
<input
type="number"
min="1"
value={amount}
onChange={(e) => setAmount(e.target.value)}
className="w-24 rounded-lg border border-slate-200 bg-white px-3 py-1.5 text-sm text-slate-900 focus:border-indigo-400 focus:outline-none dark:border-slate-800 dark:bg-slate-900 dark:text-white"
/>
<Button size="sm" variant="outline" onClick={handleDeposit} disabled={pending || connecting}>
{pending || connecting ? "Confirming..." : "Deposit"}
</Button>
{error && <p className="text-xs text-rose-600">{error}</p>}
</div>
);
}