Skip to content

Commit

Permalink
Merge pull request #51 from neu-fi/45-improve-the-performance-of-my-c…
Browse files Browse the repository at this point in the history
…ards-page

Performance improved
  • Loading branch information
hantuzun committed Jan 17, 2023
2 parents 4b2d0de + e806eab commit ac8f4f5
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 19 deletions.
4 changes: 2 additions & 2 deletions packages/frontend/components/CardList.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Card, { ICard } from "@/components/Card";
import { useBingoContract } from "@/hooks/useBingoContract";
import { checkIfNetworkIsCorrect, errorSlicing, getToken } from "@/utils/utils";
import { isNetworkCorrect, errorSlicing, getToken, svg } from "@/utils/utils";
import { Contract } from "ethers";
import { PropsWithChildren, useEffect, useState } from "react";
import { useSigner, useAccount } from "wagmi";
Expand Down Expand Up @@ -44,7 +44,7 @@ export default function CardList(props: PropsWithChildren<CardListProps>) {
const contract: Contract | undefined = useBingoContract(signer.data);

useEffect(() => {
if (!checkIfNetworkIsCorrect()) {
if (!isNetworkCorrect()) {
return;
}

Expand Down
4 changes: 2 additions & 2 deletions packages/frontend/components/contract/DrawnCountDown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { useEffect, useState } from "react";
import { useProvider } from "wagmi";
import { useBingoContract } from "@/hooks/useBingoContract";
import {
checkIfNetworkIsCorrect,
isNetworkCorrect,
timestampToCountdown,
toastOptions,
} from "@/utils/utils";
Expand All @@ -28,7 +28,7 @@ export const DrawnCountDown = () => {
}

useEffect(() => {
if (!checkIfNetworkIsCorrect()) {
if (!isNetworkCorrect()) {
return;
}
async function getDrawTime() {
Expand Down
7 changes: 3 additions & 4 deletions packages/frontend/components/contract/GetDrawnNumbers.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import React, { useEffect, useState } from "react";
import { useEffect, useState } from "react";
import { useProvider } from "wagmi";
import DrawnNumbersTable from "@/components/DrawnNumbersTable";
import { useBingoContract } from "@/hooks/useBingoContract";
import { BigNumber, Event, Contract } from "ethers";
import { toast } from "react-toastify";
import { errorSlicing, toastOptions } from "@/utils/utils";
import { checkIfNetworkIsCorrect } from "../../utils/utils";
import { errorSlicing, toastOptions, isNetworkCorrect } from "@/utils/utils";

type GetDrawnNumbersProps = {};

Expand All @@ -18,7 +17,7 @@ export const GetDrawnNumbers = (props: GetDrawnNumbersProps) => {
const contract: Contract | undefined = useBingoContract(provider);

useEffect(() => {
if (!checkIfNetworkIsCorrect()) {
if (!isNetworkCorrect()) {
return;
}

Expand Down
21 changes: 10 additions & 11 deletions packages/frontend/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,18 @@ export async function getToken(
): Promise<ICard> {
try {
const tokenURIBase64 = await contract.tokenURI(tokenId);
const tokenURI = await getTokenDataJSON(tokenURIBase64);
let tokenURI = JSON.parse(
Buffer.from(tokenURIBase64.split(",")[1], "base64").toString()
);
tokenURI.image = Buffer.from(
tokenURI.image.split(",")[1],
"base64"
).toString();

const coveredNumbersCount = Number(
(await contract.coveredNumbers(tokenId)).toString()
);

const card: ICard = {
id: tokenId,
coveredNumbersCount,
Expand All @@ -100,16 +108,7 @@ export async function getToken(
}
}

export async function getTokenDataJSON(tokenURI: URL) {
return await fetch(tokenURI).then(async (res) => {
let data = await res.json();
let image = await fetch(data.image).then((res) => res.text());
data.image = image;
return data;
});
}

export function checkIfNetworkIsCorrect() {
export function isNetworkCorrect() {
const network = getNetwork();
if (network.chain && network.chain.id !== NETWORK_ID) {
toast.error(`Please switch to ${NETWORK_NAME}`, toastOptions);
Expand Down

1 comment on commit ac8f4f5

@vercel
Copy link

@vercel vercel bot commented on ac8f4f5 Jan 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.