Skip to content

Commit

Permalink
Fix wrong network toast and claim history bug (#59)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Jovells authored Jan 10, 2024
1 parent 9dffccd commit b025c3f
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 30 deletions.
2 changes: 1 addition & 1 deletion frontend/nextjs/emt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,4 @@ export const BOOKINGS_COLLECTION =
"bookings"
);

export const exptLevelKeys = [1, 2, 3];
export const EXPT_LEVEL_KEYS = [1, 2, 3];
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ const CreatePostForm = () => {


<Separator />

{/* TODO: @od41 error message doesnt show when image file type is wrong */}
<FormField
control={form.control}
name="coverPhoto"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ const ClaimExptCard = ({profile}: {profile: UserProfile}) => {
.custom<File>((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, {
Expand Down
60 changes: 40 additions & 20 deletions frontend/nextjs/src/lib/hooks/useBackend.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ import {
NOTIFICATIONS_COLLECTION,
USERS_COLLECTION,
ADMIN_COLLECTION,
exptLevelKeys,
EXPT_LEVEL_KEYS,
chain,
} from "../../../emt.config";
import { firestore, storage } from "../firebase";
Expand Down Expand Up @@ -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]) };
});
Expand Down Expand Up @@ -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<ClaimHistoryItem, "id" | "timestamp"> = {
type: "ment",
Expand All @@ -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,
Expand All @@ -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);
}
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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[]) {
Expand Down Expand Up @@ -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);
}
}

/**
Expand All @@ -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);
}
}

/**
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
12 changes: 6 additions & 6 deletions frontend/nextjs/src/lib/hooks/useContracts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -150,15 +150,15 @@ 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);
} else {
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);
Expand Down
1 change: 0 additions & 1 deletion frontend/nextjs/src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ export type ProfileFilters = {
}

export type ExptFilters = {
//TODO: @Jovells refine this
tags?: string[],
author?: string,
mentee?: string,
Expand Down

0 comments on commit b025c3f

Please sign in to comment.