forked from thirdweb-example/play-to-earn-game
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOwnedGear.tsx
79 lines (69 loc) · 2.08 KB
/
OwnedGear.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
import {
ThirdwebNftMedia,
useAddress,
useOwnedNFTs,
Web3Button,
} from "@thirdweb-dev/react";
import { EditionDrop, SmartContract } from "@thirdweb-dev/sdk";
import React from "react";
import LoadingSection from "./LoadingSection";
import styles from "../styles/Home.module.css";
import { MINING_CONTRACT_ADDRESS } from "../const/contractAddresses";
type Props = {
pickaxeContract: EditionDrop;
miningContract: SmartContract<any>;
};
/**
* This component shows the:
* - Pickaxes the connected wallet has
* - A stake button underneath each of them to equip it
*/
export default function OwnedGear({ pickaxeContract, miningContract }: Props) {
const address = useAddress();
const { data: ownedPickaxes, isLoading } = useOwnedNFTs(
pickaxeContract,
address
);
if (isLoading) {
return <LoadingSection />;
}
async function equip(id: string) {
if (!address) return;
// The contract requires approval to be able to transfer the pickaxe
const hasApproval = await pickaxeContract.isApproved(
address,
MINING_CONTRACT_ADDRESS
);
if (!hasApproval) {
await pickaxeContract.setApprovalForAll(MINING_CONTRACT_ADDRESS, true);
}
await miningContract.call("stake", id);
// Refresh the page
window.location.reload();
}
return (
<>
<div className={styles.nftBoxGrid}>
{ownedPickaxes?.map((p) => (
<div className={styles.nftBox} key={p.metadata.id.toString()}>
<ThirdwebNftMedia
metadata={p.metadata}
className={`${styles.nftMedia} ${styles.spacerTop}`}
height={"64"}
/>
<h3>{p.metadata.name}</h3>
<div className={styles.smallMargin}>
<Web3Button
colorMode="dark"
contractAddress={MINING_CONTRACT_ADDRESS}
action={() => equip(p.metadata.id)}
>
Equip
</Web3Button>
</div>
</div>
))}
</div>
</>
);
}