/
HomeAuth.js
95 lines (75 loc) · 2.19 KB
/
HomeAuth.js
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
89
90
91
92
93
94
95
import { useState, useEffect } from "react";
import "./HomeAuth.css";
import axios from "axios";
import BlogCard from "../components/BlogCard";
import { useMoralisWeb3Api } from "react-moralis";
const HomeAuth = () => {
const [blogs, setBlogs] = useState();
const [blogsContent, setBlogsContent] = useState();
const Web3Api = useMoralisWeb3Api();
const fetchAllNfts = async () => {
const options = {
chain: "mumbai",
address: "0xEA5e2D4CaAeD0520a38EcCBbc175E857AB14bD16",
};
const polygonNFTs = await Web3Api.token.getNFTOwners(options);
const tokenUri = polygonNFTs?.result?.map((data) => {
const { metadata, owner_of } = data;
if (metadata) {
const metadataObj = JSON.parse(metadata);
const { externalUrl } = metadataObj;
return { externalUrl, owner_of };
} else {
return undefined;
}
});
setBlogs(tokenUri);
};
const fetchBlogsContent = async () => {
const limit5 = blogs?.slice(0, 5);
let contentBlog = [];
if (limit5) {
limit5.map(async (blog) => {
if (blog) {
const { externalUrl, owner_of } = blog;
const res = await axios.get(externalUrl);
const text = res.data.text.toString();
const title = res.data.title;
contentBlog.push({ title, text, owner_of, externalUrl });
}
});
}
setBlogsContent(contentBlog);
};
useEffect(() => {
if (blogs && !blogsContent) {
fetchBlogsContent();
}
}, [blogs, blogsContent]);
useEffect(() => {
if (!blogs) {
fetchAllNfts();
}
}, [blogs]);
return (
<div className="homeAuth_container">
<div className="homeAuth_header">Recommended Blogs</div>
<div className="homeAuth_blogs">
{blogsContent &&
blogsContent.map((blog, i) => {
const { title, text, owner_of, externalUrl } = blog;
return (
<BlogCard
key={i}
title={title}
text={text}
ownerOf={owner_of}
externalUrl={externalUrl}
/>
);
})}
</div>
</div>
);
};
export default HomeAuth;