Skip to content

Commit c0b3308

Browse files
committed
fix: improve loading state management and ensure mounted check in Listings and Markets components
1 parent 033d7ea commit c0b3308

File tree

3 files changed

+31
-8
lines changed

3 files changed

+31
-8
lines changed

src/components/Dashboard/Listings.tsx

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,18 @@ interface ListingsProps {
1111

1212
const Listings: React.FC<ListingsProps> = ({ filterParams }) => {
1313
const [listings, setListings] = useState<AuctionDto[]>([]);
14+
const [loading, setLoading] = useState<boolean>(true);
1415
const [currentPage, setCurrentPage] = useState(1);
1516
const [totalListings, setTotalListings] = useState(0);
1617
const itemsPerPage = 12;
18+
const [mounted, setMounted] = useState(false);
1719

1820
useEffect(() => {
21+
setMounted(true);
22+
}, []);
23+
24+
useEffect(() => {
25+
setLoading(true);
1926
const fetchListings = async () => {
2027
try {
2128
const params: Record<string, unknown> = {
@@ -52,13 +59,18 @@ const Listings: React.FC<ListingsProps> = ({ filterParams }) => {
5259
} catch (error) {
5360
console.error("Error fetching Listings:", error);
5461
}
62+
setLoading(false);
5563
};
5664

5765
fetchListings();
5866
}, [currentPage, filterParams]);
5967

6068
const canAddToCart = false;
6169

70+
if (!mounted) {
71+
return null;
72+
}
73+
6274
return (
6375
<div>
6476
<div
@@ -70,7 +82,7 @@ const Listings: React.FC<ListingsProps> = ({ filterParams }) => {
7082
justifyContent: "center"
7183
}}
7284
>
73-
{listings.length === 0 ? (
85+
{listings.length === 0 && !loading ? (
7486
<div style={{ textAlign: "center", width: "100%" }}>
7587
<h2>No Listings Found</h2>
7688
<p>Please adjust your filters or try again later.</p>

src/components/Dashboard/Markets.tsx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,19 @@ interface MarketsProps {
1111

1212
const Markets: React.FC<MarketsProps> = ({ filterParams }) => {
1313
const [markets, setMarkets] = useState<AuctionDto[]>([]);
14+
const [loading, setLoading] = useState<boolean>(true);
1415
const [currentPage, setCurrentPage] = useState(1);
1516
const [totalMarkets, setTotalMarkets] = useState(0);
1617
const itemsPerPage = 12;
18+
const [mounted, setMounted] = useState(false);
19+
20+
useEffect(() => {
21+
setMounted(true);
22+
}, []);
1723

1824
useEffect(() => {
1925
const fetchMarkets = async () => {
26+
setLoading(true);
2027
try {
2128
const response = await axios.get(`/api/markets`, {
2229
params: {
@@ -31,13 +38,18 @@ const Markets: React.FC<MarketsProps> = ({ filterParams }) => {
3138
} catch (error) {
3239
console.error("Error fetching markets:", error);
3340
}
41+
setLoading(false);
3442
};
3543

3644
fetchMarkets();
3745
}, [currentPage, filterParams]);
3846

3947
const canAddToCart = true;
4048

49+
if (!mounted) {
50+
return null;
51+
}
52+
4153
return (
4254
<div>
4355
<div
@@ -49,9 +61,9 @@ const Markets: React.FC<MarketsProps> = ({ filterParams }) => {
4961
justifyContent: "center"
5062
}}
5163
>
52-
{markets.length === 0 ? (
64+
{markets.length === 0 && !loading ? (
5365
<div style={{ textAlign: "center", width: "100%" }}>
54-
<h2>No Listings Found</h2>
66+
<h2>No Listings Found {`${loading}`}</h2>
5567
<p>Please adjust your filters or try again later.</p>
5668
</div>
5769
) : (

src/services/erc20Contract.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
import { readContract } from "@wagmi/core";
22
import { abiERC20 } from "@/abi/abiERC20";
3-
import { wagmiConfig } from "@/app/wagmi";
3+
import { getWagmiConfig } from "@/app/wagmi";
44
import { USDT_ADDRESS } from "@/constants/constants";
55
import { ethers } from "ethers";
66
import { shortenNumber } from "@/utils/shorten";
77

88
const getBalance = async (address: string): Promise<number> => {
9-
if (!wagmiConfig) {
10-
throw new Error("wagmiConfig is null");
9+
if (!getWagmiConfig()) {
10+
throw new Error("getWagmiConfig() is null");
1111
}
12-
// check if the user has enough balance
13-
const balance = await readContract(wagmiConfig, {
12+
const balance = await readContract(getWagmiConfig(), {
1413
abi: abiERC20,
1514
address: USDT_ADDRESS,
1615
functionName: "balanceOf",

0 commit comments

Comments
 (0)