Skip to content

Commit 2228edf

Browse files
committed
refactor: useAccount to get address
1 parent 3fe52f6 commit 2228edf

8 files changed

+56
-76
lines changed

.eslintrc.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"semi": [2, "never"],
55
"space-before-blocks": "error",
66
"space-before-function-paren": [2, "never"],
7-
"object-curly-spacing": ["error", "always"]
7+
"object-curly-spacing": ["error", "always"],
8+
"indent": ["error", 2]
89
}
910
}

pages/all-assets.js

+2-7
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,17 @@ import { nftaddress, nftmarketaddress } from "../config"
77

88
import NFT from "../artifacts/contracts/NFT.sol/NFT.json"
99
import Market from "../artifacts/contracts/Market.sol/NFTMarket.json"
10+
import {useWeb3} from "../hooks/useWeb3";
1011

1112
export default function AllAssets() {
1213
const [nfts, setNfts] = useState([])
1314
const [sold, setSold] = useState([])
1415
const [loadingState, setLoadingState] = useState("not-loaded")
16+
const provider = useWeb3()
1517
useEffect(() => {
1618
loadNFTs()
1719
}, [])
1820
async function loadNFTs() {
19-
const web3Modal = new Web3Modal({
20-
network: "mainnet",
21-
cacheProvider: true,
22-
})
23-
const connection = await web3Modal.connect()
24-
// const provider = new ethers.providers.JsonRpcProvider()
25-
const provider = new ethers.providers.Web3Provider(connection)
2621
const signer = provider.getSigner()
2722

2823
// const marketContract = new ethers.Contract(nftmarketaddress, Market.abi, provider)

pages/article.js

+2-6
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@ import Market from "../artifacts/contracts/Market.sol/NFTMarket.json"
1515

1616
import { providers } from "ethers"
1717
import { init } from "@textile/eth-storage"
18+
import {useWeb3} from "../hooks/useWeb3";
1819

1920
let ethAccount
2021
let myethAccount
2122
let cid
2223
let nft = {}
2324
export default function MyAssets() {
2425
// const [nft, setNft] = useState({})
26+
const provider = useWeb3()
2527
const [loadingState, setLoadingState] = useState("not-loaded")
2628
const router = useRouter()
2729

@@ -45,10 +47,7 @@ export default function MyAssets() {
4547
}
4648

4749
async function storeNFTtoFilecoin() {
48-
await window.ethereum.enable()
49-
const provider = new providers.Web3Provider(window.ethereum)
5050
const wallet = provider.getSigner()
51-
5251
const storage = await init(wallet)
5352
// const blob = new Blob(["Hello, world!"], { type: "text/plain" });
5453
const jsonse = JSON.stringify(nft)
@@ -75,9 +74,6 @@ export default function MyAssets() {
7574
}
7675

7776
async function createSale(url) {
78-
const web3Modal = new Web3Modal()
79-
const connection = await web3Modal.connect()
80-
const provider = new ethers.providers.Web3Provider(connection)
8177
const signer = provider.getSigner()
8278

8379
/* next, create the item */

pages/articles.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,18 @@ import axios from "axios"
44
import { useRouter } from "next/router"
55

66
import { nftmarketaddress, nftaddress } from "../config"
7+
import {useAccount} from "../hooks/useAccount";
78

89
let ethAccount
910
export default function MyAssets() {
11+
const account = useAccount()
1012
const [nfts, setNfts] = useState([])
1113
const [loadingState, setLoadingState] = useState("not-loaded")
1214

1315
// useEffect(function() {
1416
// },[]);
1517
const router = useRouter()
1618

17-
console.log(router.query)
1819
if ("author" in router.query) {
1920
ethAccount = router.query.author
2021
console.log("ethAccount", ethAccount)

pages/create.tsx

+17-21
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import NFT from "../artifacts/contracts/NFT.sol/NFT.json"
1111
import Market from "../artifacts/contracts/Market.sol/NFTMarket.json"
1212
import { useEffect, useState } from "react"
1313
import { InputFieldError } from "../components/InputFieldError"
14+
import { useAccount } from "../hooks/useAccount"
1415

1516
interface IFormInputs {
1617
price: string
@@ -32,19 +33,14 @@ const schema = yup.object({
3233

3334
export default function CreateItem() {
3435
const router = useRouter()
35-
// TODO: create useAccount Hook and listen account change
36-
const [ethAccount, setEthAccount] = useState<string>()
36+
const account = useAccount()
3737
const [preview, setPreview] = useState<string>()
3838

3939
useEffect(() => {
40-
if (typeof window !== "undefined") {
41-
const account = localStorage.getItem("ethAccount")
42-
if (!account) {
43-
alert("No ETH Account, Please login")
44-
router.push("/articles-all")
45-
return
46-
}
47-
setEthAccount(account)
40+
if (!account) {
41+
alert("No ETH Account, Please login")
42+
router.push("/articles-all")
43+
return
4844
}
4945
}, [])
5046

@@ -72,7 +68,7 @@ export default function CreateItem() {
7268
const authors = [{
7369
name: data.author,
7470
wallet: {
75-
eth: ethAccount,
71+
eth: account,
7672
},
7773
}]
7874
const nftData = JSON.stringify({
@@ -99,7 +95,7 @@ export default function CreateItem() {
9995
axios.defaults.headers.common['address'] = aethAccount
10096
const ret = await axios.post(dweb_search_url, {
10197
path: addedNFT.path,
102-
eth: ethAccount,
98+
eth: account,
10399
name: data.name,
104100
image: imageURL,
105101
tags: data.s_tags,
@@ -145,15 +141,15 @@ export default function CreateItem() {
145141
</div>
146142
<div>
147143

148-
<p>-- Markdown Tips: &nbsp;
149-
<a href="https://anwen.cc/share/6">参考1</a>&nbsp;
150-
<a href="https://www.markdown.xyz/basic-syntax/">参考2</a>
151-
</p>
152-
<textarea
153-
placeholder="Content of your article (you can use Markdown format)"
154-
className="mt-2 border rounded p-4 h-80 w-full"
155-
{...register("description")}
156-
/>
144+
<p>-- Markdown Tips: &nbsp;
145+
<a href="https://anwen.cc/share/6">参考1</a>&nbsp;
146+
<a href="https://www.markdown.xyz/basic-syntax/">参考2</a>
147+
</p>
148+
<textarea
149+
placeholder="Content of your article (you can use Markdown format)"
150+
className="mt-2 border rounded p-4 h-80 w-full"
151+
{...register("description")}
152+
/>
157153
<InputFieldError message={errors.description?.message} />
158154
</div>
159155
<div>

pages/edit.tsx

+18-22
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { nftaddress, nftmarketaddress } from "../config"
1313
import NFT from "../artifacts/contracts/NFT.sol/NFT.json"
1414
import Market from "../artifacts/contracts/Market.sol/NFTMarket.json"
1515
import { InputFieldError } from "../components/InputFieldError"
16+
import { useAccount } from "../hooks/useAccount"
1617

1718

1819
let nft // TODO: use useState?
@@ -37,21 +38,16 @@ const schema = yup.object({
3738

3839
export default function EditItem() {
3940
const router = useRouter()
40-
// TODO: create useAccount Hook and listen account change
41-
const [ethAccount, setEthAccount] = useState<string>()
41+
const account = useAccount()
4242
const [preview, setPreview] = useState<string>()
4343
const [cid, setCid] = useState<string>()
4444
const [loadingState, setLoadingState] = useState("not-loaded")
4545

4646
useEffect(() => {
47-
if (typeof window !== "undefined") {
48-
const account = localStorage.getItem("ethAccount")
49-
if (!account) {
50-
alert("No ETH Account, Please login")
51-
router.push("/articles-all")
52-
return
53-
}
54-
setEthAccount(account)
47+
if (!account) {
48+
alert("No ETH Account, Please login")
49+
router.push("/articles-all")
50+
return
5551
}
5652
}, [])
5753

@@ -105,7 +101,7 @@ export default function EditItem() {
105101
const authors = [{
106102
name: data.author,
107103
wallet: {
108-
eth: ethAccount,
104+
eth: account,
109105
},
110106
}]
111107
const nftData = JSON.stringify({
@@ -135,7 +131,7 @@ export default function EditItem() {
135131
// headers: {"Authorization" : `Bearer ${tokenStr}`},
136132
previous_path: cid,
137133
path: addedNFT.path,
138-
eth: ethAccount,
134+
eth: account,
139135
name: data.name,
140136
image: imageURL,
141137
tags: data.s_tags,
@@ -187,16 +183,16 @@ export default function EditItem() {
187183
</div>
188184
<div>
189185

190-
<p>-- Markdown Tips: &nbsp;
191-
<a href="https://anwen.cc/share/6">参考1</a>&nbsp;
192-
<a href="https://www.markdown.xyz/basic-syntax/">参考2</a>
193-
</p>
194-
<textarea
195-
placeholder="Content of your article (you can use Markdown format)"
196-
className="mt-2 border rounded p-4 h-80 w-full"
197-
defaultValue={nft.description}
198-
{...register("description")}
199-
/>
186+
<p>-- Markdown Tips: &nbsp;
187+
<a href="https://anwen.cc/share/6">参考1</a>&nbsp;
188+
<a href="https://www.markdown.xyz/basic-syntax/">参考2</a>
189+
</p>
190+
<textarea
191+
placeholder="Content of your article (you can use Markdown format)"
192+
className="mt-2 border rounded p-4 h-80 w-full"
193+
defaultValue={nft.description}
194+
{...register("description")}
195+
/>
200196
<InputFieldError message={errors.description?.message} />
201197
</div>
202198
<div>

pages/my-collections.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,22 @@ import { nftmarketaddress, nftaddress } from "../config"
66

77
import Market from "../artifacts/contracts/Market.sol/NFTMarket.json"
88
import NFT from "../artifacts/contracts/NFT.sol/NFT.json"
9+
import { useAccount } from "../hooks/useAccount"
10+
import { useWeb3Context } from "../context/web3Context"
11+
import { useWeb3 } from "../hooks/useWeb3"
912

1013
export default function MyAssets() {
1114
const [nfts, setNfts] = useState([])
1215
const [loadingState, setLoadingState] = useState("not-loaded")
16+
const provider = useWeb3()
17+
1318

1419
useEffect(() => {
20+
if (!provider) return
1521
loadNFTs()
16-
}, [])
22+
}, [provider])
23+
1724
async function loadNFTs() {
18-
const web3Modal = new Web3Modal({
19-
// network: "mainnet",
20-
// network: "mumbai",
21-
cacheProvider: true,
22-
})
23-
const connection = await web3Modal.connect()
24-
const provider = new ethers.providers.Web3Provider(connection)
2525
const signer = provider.getSigner()
2626

2727
const marketContract = new ethers.Contract(
@@ -64,7 +64,7 @@ export default function MyAssets() {
6464
<div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-4 pt-4">
6565
{nfts.map((nft, i) => (
6666
<div key={i} className="border shadow rounded-xl overflow-hidden">
67-
<img src={nft.image} className="rounded" />
67+
<img src={nft.image} className="rounded"/>
6868

6969
<div className="p-4">
7070
<a href={"/article?cid=" + nft.path}>

pages/my-nfts.js

+4-9
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,19 @@ import { nftmarketaddress, nftaddress } from "../config"
66

77
import Market from "../artifacts/contracts/Market.sol/NFTMarket.json"
88
import NFT from "../artifacts/contracts/NFT.sol/NFT.json"
9+
import {useWeb3} from "../hooks/useWeb3";
910

1011
export default function CreatorDashboard() {
1112
const [nfts, setNfts] = useState([])
1213
const [sold, setSold] = useState([])
1314
const [loadingState, setLoadingState] = useState("not-loaded")
15+
const provider = useWeb3()
1416

1517
useEffect(() => {
18+
if (!provider) return
1619
loadNFTs()
17-
}, [])
20+
}, [provider])
1821
async function loadNFTs() {
19-
const web3Modal = new Web3Modal({
20-
// network: "mainnet",
21-
// network: "mumbai",
22-
cacheProvider: true,
23-
})
24-
// const connection = await web3Modal.connect("https://rpc-mumbai.matic.today")
25-
const connection = await web3Modal.connect()
26-
const provider = new ethers.providers.Web3Provider(connection)
2722
const signer = provider.getSigner()
2823

2924
const marketContract = new ethers.Contract(

0 commit comments

Comments
 (0)