Skip to content

Commit

Permalink
fix(feed): Add loader when Gno react (#1393)
Browse files Browse the repository at this point in the history
* fix(feed): Add loader when Gno react, add toasts error

* chore(feed): Rename react hook

* chore(feed): Rollback renaming hook

* chore(feed): remvoe console.log

* chore(feed): Revert a refacto out of scope

* chore(feed): Revert a refacto out of scope

* chore(feed): remove out of scope code,
FIX BIG MISTAKE in postMutate identifier

* fix(feed): Add try catch when react to post on gno

---------

Co-authored-by: n0izn0iz <[email protected]>
  • Loading branch information
WaDadidou and n0izn0iz authored Nov 23, 2024
1 parent 1ea33f7 commit 13cf4eb
Showing 1 changed file with 43 additions and 28 deletions.
71 changes: 43 additions & 28 deletions packages/hooks/feed/useSocialReactions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { GnoJSONRPCProvider } from "@gnolang/gno-js-client";
import { Dispatch, SetStateAction } from "react";
import { Dispatch, SetStateAction, useEffect, useState } from "react";

import { useFeedPosting } from "./useFeedPosting";
import useSelectedWallet from "../useSelectedWallet";
Expand Down Expand Up @@ -47,11 +47,16 @@ export const useSocialReactions = ({
post.reactions,
variables.msg.icon,
);

setPost({ ...post, reactions });
},
});
const cosmosReaction = async (emoji: string, walletAddress: string) => {
const [isLoading, setLoading] = useState(false);

useEffect(() => {
setLoading(isPostMutationLoading);
}, [isPostMutationLoading]);

const reactOnCosmos = async (emoji: string, walletAddress: string) => {
const client = await signingSocialFeedClient({
networkId: post.networkId,
walletAddress,
Expand All @@ -66,7 +71,8 @@ export const useSocialReactions = ({
},
});
};
const gnoReaction = async (emoji: string, rpcEndpoint: string) => {

const reactOnGno = async (emoji: string, rpcEndpoint: string) => {
const gnoNetwork = mustGetGnoNetwork(post.networkId);
const vmCall = {
caller: selectedWallet?.address || "",
Expand All @@ -75,30 +81,39 @@ export const useSocialReactions = ({
func: "ReactPost",
args: [TERITORI_FEED_ID, post.id.split("-")[1], emoji, "true"],
};
const txHash = await adenaDoContract(
post.networkId,
[{ type: AdenaDoContractMessageType.CALL, value: vmCall }],
{
gasWanted: 2_000_000,
},
);
const provider = new GnoJSONRPCProvider(rpcEndpoint);
// Wait for tx done
await provider.waitForTransaction(txHash);
const reactions = [...post.reactions];
const currentReactionIdx = reactions.findIndex((r) => r.icon === emoji);

if (currentReactionIdx > -1) {
reactions[currentReactionIdx].count++;
} else {
reactions.push({
icon: emoji,
count: 1,
ownState: true,
});
try {
setLoading(true);
const txHash = await adenaDoContract(
post.networkId,
[{ type: AdenaDoContractMessageType.CALL, value: vmCall }],
{
gasWanted: 2_000_000,
},
);
const provider = new GnoJSONRPCProvider(rpcEndpoint);
// Wait for tx done
await provider.waitForTransaction(txHash);
const reactions = [...post.reactions];
const currentReactionIdx = reactions.findIndex((r) => r.icon === emoji);

if (currentReactionIdx > -1) {
reactions[currentReactionIdx].count++;
} else {
reactions.push({
icon: emoji,
count: 1,
ownState: true,
});
}
setPost({ ...post, reactions });
} catch (err) {
console.error(err);
} finally {
setLoading(false);
}
setPost({ ...post, reactions });
};

const handleReaction = async (emoji: string) => {
const action =
emoji === LIKE_EMOJI
Expand Down Expand Up @@ -126,11 +141,11 @@ export const useSocialReactions = ({
}
const network = getNetwork(post.networkId);
if (network?.kind === NetworkKind.Gno) {
gnoReaction(emoji, network?.endpoint || "");
reactOnGno(emoji, network?.endpoint || "");
} else {
cosmosReaction(emoji, selectedWallet.address);
reactOnCosmos(emoji, selectedWallet.address);
}
};

return { handleReaction, isPostMutationLoading };
return { handleReaction, isPostMutationLoading: isLoading };
};

0 comments on commit 13cf4eb

Please sign in to comment.