Skip to content

Commit

Permalink
fix: transaction not activating
Browse files Browse the repository at this point in the history
  • Loading branch information
zintarh committed Jun 12, 2024
1 parent c09e655 commit 222979d
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 100 deletions.
37 changes: 19 additions & 18 deletions frontend/src/components/Comments.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,33 +48,34 @@ export default function Comments({ proposalId }: Props) {
setIsCopied(true);
}

console.log(proposalId, "proposal Id");

return isLoading ? (
<div>loading.... </div>
) : (
<div className="w-[35rem] pt-5">
<div className="py-2">
{comments.length > 0 && comments.map(({address, text, starknet_id}, i) => (
<div key={i}>
<div className="grid grid-cols-[1fr_3fr] gap-3 ">
<p className="text-sm font-[400] ">Senders Address:</p>
<div className="flex items-center gap-2">
<p className="font-[600] text-sm">
{address.slice(0, 20)}
</p>
<div onClick={() => handleCopyClick(address)}>
{isCopied ? <TickIcon /> : <CopyIcon />}
{comments.length > 0 &&
comments.map(({ address, text, starknet_id }, i) => (
<div key={i}>
<div className="grid grid-cols-[1fr_3fr] gap-3 ">
<p className="text-sm font-[400] ">Senders Address:</p>
<div className="flex items-center gap-2">
<p className="font-[600] text-sm">{address.slice(0, 20)}</p>
<div onClick={() => handleCopyClick(address)}>
{isCopied ? <TickIcon /> : <CopyIcon />}
</div>
</div>
</div>

<p className="text-sm font-[400] ">Comment:</p>
<p className="font-[600] text-sm">{text}</p>
<p className="text-sm font-[400] ">Comment:</p>
<p className="font-[600] text-sm">{text}</p>

<p className="text-sm font-[400]">Token Balance:</p>
<p className="font-[600] text-sm">{starknet_id}</p>
<p className="text-sm font-[400]">Token Balance:</p>
<p className="font-[600] text-sm">{starknet_id}</p>
</div>
<div className="border border-b-gray-200 mt-2" />
</div>
<div className="border border-b-gray-200 mt-2" />
</div>
))}
))}
</div>
</div>
);
Expand Down
167 changes: 85 additions & 82 deletions frontend/src/components/NewProposalCommentForm.tsx
Original file line number Diff line number Diff line change
@@ -1,102 +1,105 @@
import React, { useEffect, useMemo } from "react";
import React, { useEffect, useMemo, useState } from "react";
import toast from "react-hot-toast";
import { useAccount, useContractWrite } from "@starknet-react/core";
import { submitCommentApi } from "../api/apiService";
import { CONTRACT_ADDR, formatAddress } from "../lib/config";
import { shortString } from "starknet";
import { submitCommentApi } from "../api/apiService";
export default function NewcommentCommentForm({
setIsModalOpen,
propId,
setIsModalOpen,
propId,
}: {
setIsModalOpen: React.Dispatch<React.SetStateAction<boolean>>;
propId: string;
setIsModalOpen: React.Dispatch<React.SetStateAction<boolean>>;
propId: string;
}) {
const { isConnected, address } = useAccount();
const [comment, setComment] = React.useState<string>("");
const [ipfsHash, setIpfsHash] = React.useState<string>("");
const [isLoading, setIsLoading] = React.useState<boolean>(false);
const { isConnected, address } = useAccount();
const [comment, setComment] = React.useState<string>("");
const [ipfsHash, setIpfsHash] = useState<string>("");
const [isLoading, setIsLoading] = React.useState<boolean>(false);

const calls = useMemo(() => {
const tx = {
contractAddress: CONTRACT_ADDR,
entrypoint: "add_comment",
calldata: [
propId.toString(),
shortString.splitLongString(ipfsHash).toString(),
],
};
return [tx];
}, [ipfsHash]);
const calls = useMemo(() => {
const tx = {
contractAddress: CONTRACT_ADDR,
entrypoint: "add_comment",
calldata: [
propId.toString(),
shortString.splitLongString(ipfsHash).toString(),
],
};
return [tx];
}, [ipfsHash]);

const { writeAsync } = useContractWrite({ calls });
const { writeAsync } = useContractWrite({ calls });

async function submitComment(e: React.FormEvent<HTMLFormElement>) {
e.preventDefault();
async function submitComment(e: React.FormEvent<HTMLFormElement>) {
e.preventDefault();

if (!isConnected) {
toast.error("Please connect your wallet");
return;
}
if (!isConnected) {
toast.error("Please connect your wallet");
return;
}

if (!comment) {
toast.error("Please fill out all fields");
return;
}
if (!comment) {
toast.error("Please fill out all fields");
return;
}

const payload = {
text: comment,
address: formatAddress(address),
};
const payload = {
text: comment,
address: formatAddress(address),
};

setIsLoading(true);
setIsLoading(true);
try {
const result = await submitCommentApi(payload);
if (result) {
setIpfsHash(result?.ipfs_hash);
}
} catch (error) {
toast.error("Something went wrong");
setIsModalOpen(false);
console.error(error);
}
}

try {
const result = await submitCommentApi(payload);
setIpfsHash(result?.ipfs_hash);
} catch (error) {
toast.error("Something went wrong");
console.error(error);
} finally {
setIsLoading(false);
setIsModalOpen(false);
}
}
useEffect(() => {
const updateProposal = async () => {
try {
await writeAsync();
toast.success("Proposal updated successfully");
setIsModalOpen(false);
} catch (error) {
toast.error("Unable to update proposal with comment");
console.error(error);
setIsModalOpen(false);
}
};

useEffect(() => {
const updateProposal = async () => {
try {
await writeAsync();
toast.success("Proposal updated successfully");
} catch (error) {
toast.error("Unable to update proposal with comment");
console.error(error);
}
};
if (ipfsHash.length > 0) {
updateProposal();
}
}, [ipfsHash]);

if (ipfsHash.length > 0) {
updateProposal();
}
}, [ipfsHash]);
console.log(ipfsHash, "result");

return (
<div className="w-[35rem]">
<form onSubmit={submitComment}>
<label htmlFor="#comment">Comment</label>
<input
id="#comment"
type="text"
placeholder="Leave a comment here"
className="w-full p-2 mb-2 border rounded-lg border-slate-300"
onChange={(e) => setComment(e.target.value)}
/>
return (
<div className="w-[35rem]">
<form onSubmit={submitComment}>
<label htmlFor="#comment">Comment</label>
<input
id="#comment"
type="text"
placeholder="Leave a comment here"
className="w-full p-2 mb-2 border rounded-lg border-slate-300"
onChange={(e) => setComment(e.target.value)}
/>

<button
type="submit"
className="w-full p-2 mt-4 text-white bg-blue-500 rounded-lg"
>
{isLoading ? "Loading..." : "Submit"}
</button>
</form>
</div>
);
<button
type="submit"
className="w-full p-2 mt-4 text-white bg-blue-500 rounded-lg"
>
{isLoading ? "Loading..." : "Submit"}
</button>
</form>
</div>
);
}

0 comments on commit 222979d

Please sign in to comment.