mirror of
https://github.com/Permissionless-Software-Foundation/bch-dex-taker-v2.git
synced 2026-09-21 16:52:01 -07:00
feat(nft): Load NFT card from bch-dex DB
This commit is contained in:
@@ -11,7 +11,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
// Global npm libraries
|
// Global npm libraries
|
||||||
import React, { useState, useEffect, useCallback } from 'react'
|
import React, { useState, useEffect, useCallback, useRef } from 'react'
|
||||||
import { Container, Row, Col, Button, Spinner } from 'react-bootstrap'
|
import { Container, Row, Col, Button, Spinner } from 'react-bootstrap'
|
||||||
import axios from 'axios'
|
import axios from 'axios'
|
||||||
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
|
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
|
||||||
@@ -35,9 +35,13 @@ function NftsForSale (props) {
|
|||||||
const [iconsAreLoaded, setIconsAreLoaded] = useState(false)
|
const [iconsAreLoaded, setIconsAreLoaded] = useState(false)
|
||||||
const [dataAreLoaded, setDataAreLoaded] = useState(false)
|
const [dataAreLoaded, setDataAreLoaded] = useState(false)
|
||||||
const [currentPage, setCurrentPage] = useState(0)
|
const [currentPage, setCurrentPage] = useState(0)
|
||||||
|
const [lastLoadedPage, setLastLoadedPage] = useState(0)
|
||||||
const [totalPages, setTotalPages] = useState(0)
|
const [totalPages, setTotalPages] = useState(0)
|
||||||
const [selectedFilter, setSelectedFilter] = useState('Misc')
|
const [selectedFilter, setSelectedFilter] = useState('Misc')
|
||||||
|
|
||||||
|
// Flag to prevent load data multiple times on component mount!
|
||||||
|
const componentMountRef = useRef(false)
|
||||||
|
|
||||||
// Handler for previous page
|
// Handler for previous page
|
||||||
const handlePreviousPage = () => {
|
const handlePreviousPage = () => {
|
||||||
if (currentPage > 0) {
|
if (currentPage > 0) {
|
||||||
@@ -70,6 +74,20 @@ function NftsForSale (props) {
|
|||||||
return offer
|
return offer
|
||||||
}
|
}
|
||||||
}, [appData])
|
}, [appData])
|
||||||
|
// Function to process token metadata (iconUrl , userData).
|
||||||
|
const processOfferMetadata = useCallback(async (offer) => {
|
||||||
|
try {
|
||||||
|
// Token icon
|
||||||
|
if (offer.tokenIconUrl) {
|
||||||
|
offer.icon = offer.tokenIconUrl
|
||||||
|
offer.iconAlreadyDownloaded = true
|
||||||
|
offer.userData = JSON.parse(offer.userDataStr)
|
||||||
|
}
|
||||||
|
return offer
|
||||||
|
} catch (error) {
|
||||||
|
return offer
|
||||||
|
}
|
||||||
|
}, [])
|
||||||
|
|
||||||
// Fetch offers
|
// Fetch offers
|
||||||
const getNftOffers = useCallback(async (page = 0) => {
|
const getNftOffers = useCallback(async (page = 0) => {
|
||||||
@@ -86,7 +104,8 @@ function NftsForSale (props) {
|
|||||||
for (let i = 0; i < rawOffers.length; i++) {
|
for (let i = 0; i < rawOffers.length; i++) {
|
||||||
const offer = rawOffers[i]
|
const offer = rawOffers[i]
|
||||||
const processedOffer = await processTokenData(offer)
|
const processedOffer = await processTokenData(offer)
|
||||||
processedOffers.push(processedOffer)
|
const processedOfferMetadata = await processOfferMetadata(processedOffer)
|
||||||
|
processedOffers.push(processedOfferMetadata)
|
||||||
}
|
}
|
||||||
|
|
||||||
setOffersAreLoaded(true)
|
setOffersAreLoaded(true)
|
||||||
@@ -97,7 +116,7 @@ function NftsForSale (props) {
|
|||||||
setOffersAreLoaded(true)
|
setOffersAreLoaded(true)
|
||||||
throw err
|
throw err
|
||||||
}
|
}
|
||||||
}, [processTokenData])
|
}, [processTokenData, processOfferMetadata])
|
||||||
|
|
||||||
// This function loads the token data .
|
// This function loads the token data .
|
||||||
const lazyLoadTokenData = useCallback(async (tokens) => {
|
const lazyLoadTokenData = useCallback(async (tokens) => {
|
||||||
@@ -166,6 +185,7 @@ function NftsForSale (props) {
|
|||||||
const thisToken = tokens[i]
|
const thisToken = tokens[i]
|
||||||
|
|
||||||
// Incon does not need to be downloaded, so continue with the next one
|
// Incon does not need to be downloaded, so continue with the next one
|
||||||
|
console.log('Icon already downloaded ', thisToken.iconAlreadyDownloaded)
|
||||||
if (thisToken.iconAlreadyDownloaded) continue
|
if (thisToken.iconAlreadyDownloaded) continue
|
||||||
|
|
||||||
// Try to get token icon url from mutable data.
|
// Try to get token icon url from mutable data.
|
||||||
@@ -174,7 +194,7 @@ function NftsForSale (props) {
|
|||||||
if (iconUrl) {
|
if (iconUrl) {
|
||||||
// Set the icon url to the token , this can be used to display the icon in the token card component.
|
// Set the icon url to the token , this can be used to display the icon in the token card component.
|
||||||
thisToken.icon = iconUrl
|
thisToken.icon = iconUrl
|
||||||
thisToken.tokenData.userData = userData
|
thisToken.userData = userData
|
||||||
}
|
}
|
||||||
|
|
||||||
// Mark token to prevent fetch token icon again.
|
// Mark token to prevent fetch token icon again.
|
||||||
@@ -246,17 +266,23 @@ function NftsForSale (props) {
|
|||||||
|
|
||||||
// Effect to load NFTs on component mount
|
// Effect to load NFTs on component mount
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
console.log('loading nfts for sale')
|
// Peevent to run multiple times
|
||||||
loadNftOffers()
|
if (!componentMountRef.current) {
|
||||||
|
console.log('loading nfts for sale')
|
||||||
|
loadNftOffers()
|
||||||
|
componentMountRef.current = true
|
||||||
|
}
|
||||||
}, [loadNftOffers])
|
}, [loadNftOffers])
|
||||||
|
|
||||||
// Effect to reload data when page changes
|
// Effect to reload data when page changes
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (currentPage >= 0) {
|
// Validate to prevent load same page again
|
||||||
|
if (currentPage >= 0 && currentPage !== lastLoadedPage) {
|
||||||
console.log('page changed, reloading nfts for sale')
|
console.log('page changed, reloading nfts for sale')
|
||||||
loadNftOffers(currentPage)
|
loadNftOffers(currentPage)
|
||||||
|
setLastLoadedPage(currentPage)
|
||||||
}
|
}
|
||||||
}, [currentPage, loadNftOffers])
|
}, [currentPage, loadNftOffers, lastLoadedPage])
|
||||||
|
|
||||||
// Handler for refresh button
|
// Handler for refresh button
|
||||||
const handleRefresh = useCallback(() => {
|
const handleRefresh = useCallback(() => {
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ function InfoButton (props) {
|
|||||||
// Get token user data if it exists and verify if it contains media or markdown
|
// Get token user data if it exists and verify if it contains media or markdown
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
try {
|
try {
|
||||||
const userDataStr = props.token.tokenData.userData
|
const userDataStr = props.token.userData
|
||||||
if (userDataStr) {
|
if (userDataStr) {
|
||||||
const userData = JSON.parse(userDataStr)
|
const userData = JSON.parse(userDataStr)
|
||||||
|
|
||||||
|
|||||||
@@ -145,7 +145,7 @@ function NFTForSale (props) {
|
|||||||
if (iconUrl) {
|
if (iconUrl) {
|
||||||
// Set the icon url to the token , this can be used to display the icon in the token card component.
|
// Set the icon url to the token , this can be used to display the icon in the token card component.
|
||||||
thisToken.icon = iconUrl
|
thisToken.icon = iconUrl
|
||||||
thisToken.tokenData.userData = userData
|
thisToken.userData = userData
|
||||||
}
|
}
|
||||||
|
|
||||||
// Mark token to prevent fetch token icon again.
|
// Mark token to prevent fetch token icon again.
|
||||||
|
|||||||
@@ -124,7 +124,7 @@ const SlpTokens = (props) => {
|
|||||||
if (iconUrl) {
|
if (iconUrl) {
|
||||||
// Set the icon url to the token , this can be used to display the icon in the token card component.
|
// Set the icon url to the token , this can be used to display the icon in the token card component.
|
||||||
thisToken.icon = iconUrl
|
thisToken.icon = iconUrl
|
||||||
thisToken.tokenData.userData = userData
|
thisToken.userData = userData
|
||||||
}
|
}
|
||||||
|
|
||||||
// Mark token to prevent fetch token icon again.
|
// Mark token to prevent fetch token icon again.
|
||||||
|
|||||||
@@ -59,7 +59,7 @@ function InfoButton (props) {
|
|||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
try {
|
try {
|
||||||
console.log('props token', props.token)
|
console.log('props token', props.token)
|
||||||
const userDataStr = props.token.tokenData.userData
|
const userDataStr = props.token.userData
|
||||||
if (userDataStr) {
|
if (userDataStr) {
|
||||||
const userData = JSON.parse(userDataStr)
|
const userData = JSON.parse(userDataStr)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user