Skip to content

Commit

Permalink
feat: disable Dispute Modal button and Manage Support Button in Propo…
Browse files Browse the repository at this point in the history
…salId page when conn address not reg in comm
  • Loading branch information
Mati0x committed Nov 24, 2024
1 parent 5c4982d commit d1e4bd5
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
"use client";
import { useEffect, useState } from "react";
import { useEffect, useMemo, useState } from "react";
import { Hashicon } from "@emeraldpay/hashicon-react";
import {
AdjustmentsHorizontalIcon,
InformationCircleIcon,
UserIcon,
} from "@heroicons/react/24/outline";
import { ArrowPathIcon } from "@heroicons/react/24/solid";
import { usePathname, useRouter } from "next/navigation";
import { toast } from "react-toastify";
import { Address, encodeAbiParameters, formatUnits } from "viem";
import { useAccount, useToken } from "wagmi";
import {
getProposalDataDocument,
getProposalDataQuery,
isMemberDocument,
isMemberQuery,
} from "#/subgraph/.graphclient";
import {
Badge,
Expand All @@ -33,7 +34,7 @@ import { usePubSubContext } from "@/contexts/pubsub.context";
import { useChainIdFromPath } from "@/hooks/useChainIdFromPath";
import { useContractWriteWithConfirmations } from "@/hooks/useContractWriteWithConfirmations";
import { useConvictionRead } from "@/hooks/useConvictionRead";
import { useDisableButtons } from "@/hooks/useDisableButtons";
import { ConditionObject, useDisableButtons } from "@/hooks/useDisableButtons";
import { useMetadataIpfsFetch } from "@/hooks/useIpfsFetch";
import { useSubgraphQuery } from "@/hooks/useSubgraphQuery";
import { alloABI } from "@/src/generated";
Expand All @@ -43,10 +44,11 @@ import { useErrorDetails } from "@/utils/getErrorName";
import { prettyTimestamp } from "@/utils/text";

export default function Page({
params: { proposalId, garden, poolId },
params: { proposalId, garden, community: communityAddr, poolId },
}: {
params: {
proposalId: string;
community: string;
poolId: string;
chain: string;
garden: string;
Expand All @@ -56,6 +58,7 @@ export default function Page({
const router = useRouter();

const { address } = useAccount();

const [, proposalNumber] = proposalId.split("-");
const { data } = useSubgraphQuery<getProposalDataQuery>({
query: getProposalDataDocument,
Expand All @@ -71,11 +74,26 @@ export default function Page({
},
});

//query to get member registry in community
const { data: memberData } = useSubgraphQuery<isMemberQuery>({
query: isMemberDocument,
variables: {
me: address?.toLowerCase(),
comm: communityAddr?.toLowerCase(),
},
enabled: !!address,
});

const isMemberCommunity =
!!memberData?.member?.memberCommunity?.[0]?.isRegistered;
//

const proposalData = data?.cvproposal;
const proposalIdNumber =
proposalData?.proposalNumber ?
BigInt(proposalData.proposalNumber)
: undefined;

const poolTokenAddr = proposalData?.strategy.token as Address;

const { publish } = usePubSubContext();
Expand All @@ -102,7 +120,18 @@ export default function Page({
chainId,
});

const { tooltipMessage, isConnected, missmatchUrl } = useDisableButtons();
const disableManSupportBtn = useMemo<ConditionObject[]>(
() => [
{
condition: !isMemberCommunity,
message: "Join community to dispute",
},
],
[address],
);

const { tooltipMessage, isConnected, missmatchUrl } =
useDisableButtons(disableManSupportBtn);

const {
currentConvictionPct,
Expand Down Expand Up @@ -262,6 +291,7 @@ export default function Page({
proposalData={{ ...proposalData, ...metadata }}
/>
: <DisputeButton
isMemberCommunity={isMemberCommunity}
proposalData={{ ...proposalData, ...metadata }}
/>
}
Expand All @@ -285,7 +315,7 @@ export default function Page({
<Button
icon={<AdjustmentsHorizontalIcon height={24} width={24} />}
onClick={() => manageSupportClicked()}
disabled={!isConnected || missmatchUrl}
disabled={!isConnected || missmatchUrl || !isMemberCommunity}
tooltip={tooltipMessage}
>
Manage support
Expand Down
25 changes: 19 additions & 6 deletions apps/web/components/DisputeButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,17 @@ type Props = {
}
> &
MetadataV1;
isMemberCommunity: boolean;
};

const ABSTAINED_RULING = 0;
const APPROVED_RULING = 1;
const REJECTED_RULING = 2;

export const DisputeButton: FC<Props> = ({ proposalData }) => {
export const DisputeButton: FC<Props> = ({
proposalData,
isMemberCommunity,
}) => {
const [isModalOpened, setIsModalOpened] = useState(false);
const [reason, setReason] = useState("");
const [isEnoughBalance, setIsEnoughBalance] = useState(true);
Expand Down Expand Up @@ -249,8 +253,12 @@ export const DisputeButton: FC<Props> = ({ proposalData }) => {
}
};

const disableSubmitBtn = useMemo<ConditionObject[]>(
const disableDisputeSubmitBtn = useMemo<ConditionObject[]>(
() => [
{
condition: !isMemberCommunity,
message: "Join community to dispute",
},
{
condition: !isEnoughBalance,
message: "Insufficient balance",
Expand All @@ -267,8 +275,9 @@ export const DisputeButton: FC<Props> = ({ proposalData }) => {
[isEnoughBalance, isCooldown],
);

const { isConnected, missmatchUrl, tooltipMessage } =
useDisableButtons(disableSubmitBtn);
const { isConnected, missmatchUrl, tooltipMessage } = useDisableButtons(
disableDisputeSubmitBtn,
);

const rulingTimeout = convertSecondsToReadableTime(
arbitrationConfig.defaultRulingTimeout,
Expand Down Expand Up @@ -437,7 +446,11 @@ export const DisputeButton: FC<Props> = ({ proposalData }) => {
onClick={handleSubmit}
color="danger"
disabled={
!isConnected || missmatchUrl || !isEnoughBalance || isCooldown
!isConnected ||
missmatchUrl ||
!isMemberCommunity ||
!isEnoughBalance ||
isCooldown
}
tooltip={tooltipMessage}
tooltipSide="tooltip-left"
Expand All @@ -460,7 +473,7 @@ export const DisputeButton: FC<Props> = ({ proposalData }) => {
btnStyle="outline"
onClick={() => setIsModalOpened(true)}
>
{(isDisputed ?? isProposalEnded) ? "Open dispute" : "Dispute"}
{isDisputed ?? isProposalEnded ? "Open dispute" : "Dispute"}
</Button>
<Modal
title={`Disputed Proposal: ${proposalData.title} #${proposalData.proposalNumber}`}
Expand Down

0 comments on commit d1e4bd5

Please sign in to comment.