From b025c3f3b8719112cd939b456ce2561a375c79a0 Mon Sep 17 00:00:00 2001 From: Jovells <35954298+Jovells@users.noreply.github.com> Date: Wed, 10 Jan 2024 21:56:59 +0000 Subject: [PATCH] Fix wrong network toast and claim history bug (#59) * Jovells workflow auto sync after pr merge (#4) * Update main.yml * Update main.yml * fixed wrong network toast to show correct network to connect to * fix claim history bug and claim expt error --- frontend/nextjs/emt.config.ts | 2 +- .../p/create/_components/create-post-form.tsx | 2 +- .../[uid]/_components/claim-expt-card.tsx | 2 +- frontend/nextjs/src/lib/hooks/useBackend.tsx | 60 ++++++++++++------- .../nextjs/src/lib/hooks/useContracts.tsx | 12 ++-- frontend/nextjs/src/lib/types.ts | 1 - 6 files changed, 49 insertions(+), 30 deletions(-) diff --git a/frontend/nextjs/emt.config.ts b/frontend/nextjs/emt.config.ts index b06ba74..52e04ce 100644 --- a/frontend/nextjs/emt.config.ts +++ b/frontend/nextjs/emt.config.ts @@ -115,4 +115,4 @@ export const BOOKINGS_COLLECTION = "bookings" ); -export const exptLevelKeys = [1, 2, 3]; +export const EXPT_LEVEL_KEYS = [1, 2, 3]; diff --git a/frontend/nextjs/src/app/(with wallet)/dapp/p/create/_components/create-post-form.tsx b/frontend/nextjs/src/app/(with wallet)/dapp/p/create/_components/create-post-form.tsx index 9efd949..1282d4c 100644 --- a/frontend/nextjs/src/app/(with wallet)/dapp/p/create/_components/create-post-form.tsx +++ b/frontend/nextjs/src/app/(with wallet)/dapp/p/create/_components/create-post-form.tsx @@ -169,7 +169,7 @@ const CreatePostForm = () => { - + {/* TODO: @od41 error message doesnt show when image file type is wrong */} { .custom((v) => v instanceof File, { message: 'Only images allowed e.g JPG, JPEG or PNG are allowed.', }), - //TODO: @od41 Error meesage doesn't show up in form + //TODO: @od41 Error message doesn't show up in form collectionSize: z.coerce.number().max(profile.ownedExptIds?.length || 0, "Number exceeds owned expts"), // the tokenIds that are meant to be minted collectionName: z.string(), price: z.coerce.number().gte(1, { diff --git a/frontend/nextjs/src/lib/hooks/useBackend.tsx b/frontend/nextjs/src/lib/hooks/useBackend.tsx index c1dc0b9..db14f44 100644 --- a/frontend/nextjs/src/lib/hooks/useBackend.tsx +++ b/frontend/nextjs/src/lib/hooks/useBackend.tsx @@ -64,7 +64,7 @@ import { NOTIFICATIONS_COLLECTION, USERS_COLLECTION, ADMIN_COLLECTION, - exptLevelKeys, + EXPT_LEVEL_KEYS, chain, } from "../../../emt.config"; import { firestore, storage } from "../firebase"; @@ -162,7 +162,7 @@ export default function useBackend() { const { data: exptLevels } = useQuery({ queryKey: ["exptlevels"], queryFn: async () => { - const levelsPromises = exptLevelKeys.map(async (key) => { + const levelsPromises = EXPT_LEVEL_KEYS.map(async (key) => { const l = await emtMarketplace.exptLevels(key); return { requiredMent: Number(l[0]), receivableExpt: Number(l[1]) }; }); @@ -307,10 +307,13 @@ export default function useBackend() { if (!user?.uid) { throw new Error("User not logged in"); } + const t = loadingToast("Claiming ments", 1); try { const tx = await emtMarketplace.claimMent(); + t("mining transaction", 50); const receipt = await tx!.wait(); console.log("claimed ment"); + t("almost there", 80); const tokenIds = getTokenIdsClaimed(receipt!); const historyItem: Omit = { type: "ment", @@ -321,6 +324,7 @@ export default function useBackend() { const claimHistoryItem = await saveClaimHistoryItemToFirestore(historyItem); const newMent = await updateUserMentInFirestore(); + t("Ment claimed", 100); return { mentClaimed: tokenIds.length, tokenIds, @@ -329,6 +333,11 @@ export default function useBackend() { }; } catch (err: any) { console.log(err); + if (err.message.includes("No MENT to claim")) { + t("No ment to claim", undefined, true); + }else{ + t("Error claiming ment", undefined, true); + } throw new Error("Error claiming ment. Message: " + err.message); } } @@ -380,9 +389,9 @@ export default function useBackend() { }); throw new Error("User not logged in"); } - for (const i of exptLevelKeys) { - const level = i + 1; - const t = loadingToast("Claiming expt", 1); + const t = loadingToast("Claiming expt", 1); + for (const i of EXPT_LEVEL_KEYS) { + const level = i ; try { const tx = await emtMarketplace.claimExpt(level); t("mining transaction", 50); @@ -416,6 +425,8 @@ export default function useBackend() { } } } + t("No Expts To Claim", undefined, true); + throw new Error("Error claiming expt. Message: " + "Level not found"); } async function fetchVotesAndUsernames(notifications: BuiltNotification[]) { @@ -590,11 +601,16 @@ export default function useBackend() { * @returns The number of followers. */ async function fetchNumFollowers(id: string) { - const userFollowersRef = collection(USERS_COLLECTION, id, "followers"); - const querySnapshot = await getCountFromServer(query(userFollowersRef)); - const count = querySnapshot.data().count; - console.log("fetchNumFollowers", count); - return count; + try { + const userFollowersRef = collection(USERS_COLLECTION, id, "followers"); + const querySnapshot = await getCountFromServer(query(userFollowersRef)); + const count = querySnapshot.data().count; + console.log("fetchNumFollowers", count); + return count; + } catch (err) { + console.log("Error fetching num followers", err); + throw new Error('Error fetching num followers'+ err); + } } /** @@ -604,14 +620,19 @@ export default function useBackend() { * @returns The number of followers. */ async function fetchNumFollowing(id: string) { - const q = query( - collectionGroup(firestore, "followers"), - where("uid", "==", id) - ); - const querySnapshot = await getCountFromServer(q); - const count = querySnapshot.data().count; - console.log("fetchNumFollowing", count); - return count; + try { + const q = query( + collectionGroup(firestore, "followers"), + where("uid", "==", id) + ); + const querySnapshot = await getCountFromServer(q); + const count = querySnapshot.data().count; + console.log("fetchNumFollowing", count); + return count; + } catch (err) { + console.log("Error fetching num following", err); + throw new Error('Error fetching num following'+ err); + } } /** @@ -681,7 +702,7 @@ export default function useBackend() { try { console.log("fetching unclaimed expt"); let unclaimedExpt = 0; - for (let i = 0; i < exptLevelKeys.length; i++) { + for (let i = 0; i < EXPT_LEVEL_KEYS.length; i++) { try { const val = await emtMarketplace.unclaimedExpt(user.uid, i + 1); unclaimedExpt += Number(val); @@ -946,7 +967,6 @@ export default function useBackend() { } const t = loadingToast("Submitting your request", 1); try { - // TODO @od41 error: missing permissions const docRef = doc(ADMIN_COLLECTION); await saveRequestToFirestore(docRef); console.log("Document written with ID: ", docRef.id); diff --git a/frontend/nextjs/src/lib/hooks/useContracts.tsx b/frontend/nextjs/src/lib/hooks/useContracts.tsx index bbeed66..44ddfd8 100644 --- a/frontend/nextjs/src/lib/hooks/useContracts.tsx +++ b/frontend/nextjs/src/lib/hooks/useContracts.tsx @@ -121,13 +121,13 @@ export function ContractProvider({ children }: { children: React.ReactNode }) { //@ts-ignore window.signer = _signer; //@ts-ignore - window.stableCoin = stableCoin; + window.stableCoin = stableCoin.connect(_signer); //@ts-ignore - window.emtMarketplace = emtMarketplace; + window.emtMarketplace = emtMarketplace.connect(_signer); //@ts-ignore - window.expertToken = expertToken; + window.expertToken = expertToken.connect(_signer); //@ts-ignore - window.mentorToken = mentorToken; + window.mentorToken = mentorToken.connect(_signer); setContracts({ emtMarketplace: emtMarketplace.connect(_signer), @@ -150,7 +150,7 @@ export function ContractProvider({ children }: { children: React.ReactNode }) { if (network.chain && network.chain.id !== chain.id) { toast({ title: "Wrong Network", - description: "Please change to the Topos network", + description: `Please change to the ${chain.name} network`, variant: "destructive", }) setWrongNetwork(true); @@ -158,7 +158,7 @@ export function ContractProvider({ children }: { children: React.ReactNode }) { if (network.chain && wrongNetwork) { toast({ title: "Network Changed", - description: "You have successfully changed to the Topos network", + description: `You have successfully changed to the ${chain.name} network. Please reload the Page`, variant: "success", }) setWrongNetwork(false); diff --git a/frontend/nextjs/src/lib/types.ts b/frontend/nextjs/src/lib/types.ts index 9eda398..cb567ec 100644 --- a/frontend/nextjs/src/lib/types.ts +++ b/frontend/nextjs/src/lib/types.ts @@ -105,7 +105,6 @@ export type ProfileFilters = { } export type ExptFilters = { - //TODO: @Jovells refine this tags?: string[], author?: string, mentee?: string,