1
1
import { useRouter } from "next/router"
2
- import * as yup from "yup"
3
- import { useEffect , useState } from "react"
2
+ import { useEffect } from "react"
4
3
import { loadNFT } from "../services/backend"
5
4
import { useAccount } from "../hooks/useAccount"
6
5
import { Editor } from "../components/Editor"
7
- import { Article } from "../types "
6
+ import { useAsync , useMountEffect } from "@react-hookz/web "
8
7
// todo: move to request center
9
8
const dweb_search_url = `https://dweb-search-api.anwen.cc/edit_meta`
10
9
11
- interface EditArticleProps {
12
- cid : string
13
- nft : Article
14
- }
15
-
16
- export default function EditArticle ( { cid, nft } : EditArticleProps ) {
10
+ export default function EditArticle ( ) {
17
11
const router = useRouter ( )
18
12
const account = useAccount ( )
19
13
14
+
15
+ const [ info , actions ] = useAsync ( async ( ) => {
16
+ const cid = router . query . cid as string || ( new URLSearchParams ( router . asPath . match ( / c i d = ( .* ) / g) [ 0 ] ) ) . get ( 'cid' )
17
+ if ( ! cid ) return { cid : null , nft : null }
18
+ const nft = await loadNFT ( cid )
19
+ return { cid, nft }
20
+ } )
21
+
22
+ useMountEffect ( actions . execute )
23
+
20
24
useEffect ( ( ) => {
21
25
if ( ! account ) {
22
26
alert ( "No ETH Account, Please login" )
@@ -26,23 +30,19 @@ export default function EditArticle({ cid, nft }: EditArticleProps) {
26
30
} , [ ] )
27
31
28
32
29
- if ( ! nft ?. name )
33
+ if ( info . status === 'loading' ) {
34
+ return < h1 className = "py-10 px-20 text-3xl" > Loading</ h1 >
35
+ }
36
+
37
+ if ( ! info . result ?. nft ?. name || ! info . result ?. cid )
30
38
return < h1 className = "py-10 px-20 text-3xl" > Article not found, cannot edit</ h1 >
31
39
32
40
return (
33
41
< div className = "flex justify-center py-12" >
34
42
< Editor account = { account }
35
- article = { nft }
43
+ article = { info . result ?. nft }
36
44
publishLink = { dweb_search_url }
37
- cid = { cid } />
45
+ cid = { info . result ?. cid } />
38
46
</ div >
39
47
)
40
48
}
41
-
42
- export async function getServerSideProps ( context ) {
43
- const cid = context . query . cid
44
- const nft = await loadNFT ( cid )
45
- return {
46
- props : { cid, nft }
47
- }
48
- }
0 commit comments