From 4b80b250b4c708b32ede244d09c26bf6a6e43aa8 Mon Sep 17 00:00:00 2001 From: kien-ngo Date: Thu, 3 Oct 2024 17:11:43 +0000 Subject: [PATCH] [Dashboard] Vote contract proposal drawer (#4899) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Problem solved Short description of the bug fixed or feature added --- ## PR-Codex overview This PR focuses on refactoring the proposal creation UI components, replacing a `Drawer` with a `Sheet` for better user experience, and enhancing the proposal submission process with improved form handling and error management. ### Detailed summary - Deleted `proposal-form.tsx`. - Modified `metadata-header.tsx` className for better styling. - Replaced `Flex` components with `div` for layout in `page.tsx`. - Refactored `ProposalButton` to use `Sheet` instead of `Drawer`. - Added form handling with `react-hook-form` in `ProposalButton`. - Implemented error handling and user feedback with `toast` in the proposal submission process. > ✨ Ask PR-Codex anything about this PR by commenting with `/codex {your question}` --- .../contract-header/metadata-header.tsx | 2 +- .../proposals/components/proposal-button.tsx | 151 ++++++++++++------ .../proposals/components/proposal-form.tsx | 75 --------- .../src/contract-ui/tabs/proposals/page.tsx | 8 +- 4 files changed, 105 insertions(+), 131 deletions(-) delete mode 100644 apps/dashboard/src/contract-ui/tabs/proposals/components/proposal-form.tsx diff --git a/apps/dashboard/src/components/custom-contract/contract-header/metadata-header.tsx b/apps/dashboard/src/components/custom-contract/contract-header/metadata-header.tsx index b87a01fdcca..7b94eb1a545 100644 --- a/apps/dashboard/src/components/custom-contract/contract-header/metadata-header.tsx +++ b/apps/dashboard/src/components/custom-contract/contract-header/metadata-header.tsx @@ -83,7 +83,7 @@ export const MetadataHeader: React.FC = ({ {chain && ( {cleanedChainName && ( diff --git a/apps/dashboard/src/contract-ui/tabs/proposals/components/proposal-button.tsx b/apps/dashboard/src/contract-ui/tabs/proposals/components/proposal-button.tsx index 69c32923fee..e2eadfe9a23 100644 --- a/apps/dashboard/src/contract-ui/tabs/proposals/components/proposal-button.tsx +++ b/apps/dashboard/src/contract-ui/tabs/proposals/components/proposal-button.tsx @@ -1,12 +1,23 @@ "use client"; -import { useDisclosure } from "@chakra-ui/react"; +import { + Sheet, + SheetContent, + SheetHeader, + SheetTitle, + SheetTrigger, +} from "@/components/ui/sheet"; +import { FormControl, Textarea } from "@chakra-ui/react"; import { TransactionButton } from "components/buttons/TransactionButton"; +import { useTrack } from "hooks/analytics/useTrack"; import { PlusIcon } from "lucide-react"; +import { useState } from "react"; +import { useForm } from "react-hook-form"; +import { toast } from "sonner"; import type { ThirdwebContract } from "thirdweb"; +import * as VoteExt from "thirdweb/extensions/vote"; import { useSendAndConfirmTransaction } from "thirdweb/react"; -import { Button, Drawer } from "tw-components"; -import { CreateProposalForm } from "./proposal-form"; +import { Button, FormErrorMessage, FormLabel } from "tw-components"; interface VoteButtonProps { contract: ThirdwebContract; @@ -15,55 +26,93 @@ interface VoteButtonProps { const PROPOSAL_FORM_ID = "proposal-form-id"; export const ProposalButton: React.FC = ({ contract }) => { - const { isOpen, onOpen, onClose } = useDisclosure(); - + const [open, setOpen] = useState(false); const sendTx = useSendAndConfirmTransaction(); - + const { + register, + handleSubmit, + formState: { errors }, + } = useForm<{ description: string }>(); + const trackEvent = useTrack(); return ( - <> - Create new proposal }} - footer={{ - children: ( - <> - - - Submit Proposal - - - ), - }} - > - - - - + + + + + + + Create new proposal + +
{ + const tx = VoteExt.propose({ + contract, + calldatas: ["0x"], + values: [0n], + targets: [contract.address], + description: data.description, + }); + toast.promise( + sendTx.mutateAsync(tx, { + onSuccess: () => { + trackEvent({ + category: "vote", + action: "create-proposal", + label: "success", + }); + setOpen(false); + }, + onError: (error) => { + console.error(error); + trackEvent({ + category: "vote", + action: "create-proposal", + label: "error", + error, + }); + }, + }), + { + loading: "Creating proposal...", + success: "Proposal created successfully", + error: "Failed to create proposal", + }, + ); + })} + > + + Description +