"use client"; import { Header, CountDownRenderer, Loader, Footer } from "@/components"; import { random } from "@/libs/constant"; import { useRouter } from "next/router"; import React, { useEffect, useState } from "react"; import Countdown from "react-countdown"; import { FaRegEdit } from "react-icons/fa"; import TimeAgo from "react-timeago"; import NotePicker from "@/components/TextEditor/NotePicker"; import { BiSolidDownvote, BiSolidUpvote } from "react-icons/bi"; import Link from "next/link"; import { ethers } from "ethers"; import { addAnswerFn, dislikeAnswerFn, distributeMainBountyFn, getQuestionFn, likeAnswerFn, } from "@/libs/contractFunctionCall"; import { addAnswertoTableFn, readAnswersFunc, readQuestionFunc, updateAnswerFn, } from "@/libs/TablelandFnCall"; const Question = () => { const router = useRouter(); const [likeError, setLikeError] = useState(true); const [isQuestioner, setQuestioner] = useState(false); const [data, setData] = useState(""); const [mainBounty, setMainBounty] = useState(0); const [poolBounty, setPoolBounty] = useState(0); const [answerData, setAnswerData] = useState([]); const [loader, setLoader] = useState(false); const [content, setContent] = useState(""); const [bountyWinner, setBountyWinner] = useState(""); const [db, setDb] = useState(null); const { questions } = router.query; const getData = async () => { setLoader(true); const provider = new ethers.providers.Web3Provider(window.ethereum); await provider.send("eth_requestAccounts", []); const signer = await provider.getSigner(); const account = await signer.getAddress(); if (questions) { let res = await readQuestionFunc(questions); res = res[0]; console.log(res); setData(res); if (res?.address == account) { setQuestioner(true); } let res1 = await readAnswersFunc(questions); // res1 = res1[0]; console.log(res1); setAnswerData(res1); if (res?.time_based) { const { _mainBounty, _bountyPool, bountyWinner } = await getQuestionFn( signer, questions ); setMainBounty(_mainBounty); setPoolBounty(_bountyPool); setBountyWinner(bountyWinner); } } setLoader(false); }; useEffect(() => { getData(); }, [questions]); const addAnswer = async () => { if (content) { setLoader(true); const provider = new ethers.providers.Web3Provider(window.ethereum); await provider.send("eth_requestAccounts", []); const signer = await provider.getSigner(); const account = await signer.getAddress(); const data = { answerId: random(), questionId: Number(questions), addr: account, answerText: content, timeOfCreation: new Date().toISOString(), upvote: 0, downvote: 0, }; console.log(data); await addAnswertoTableFn(signer, data); await addAnswerFn(signer, data.answerId, data.questionId); setLoader(false); window?.location.reload(); } }; const updateAnswer = async (answerId, vote, upvote, downvote) => { setLoader(true); const provider = new ethers.providers.Web3Provider(window.ethereum); await provider.send("eth_requestAccounts", []); const signer = await provider.getSigner(); console.log(answerId, upvote, downvote); if (vote) { const res = await likeAnswerFn(signer, answerId); if (res) { document.getElementById("my_modal_1").showModal(); setLoader(false); return; } } else { const res = await dislikeAnswerFn(signer, answerId); if (res) { document.getElementById("my_modal_1").showModal(); setLoader(false); return; } } await updateAnswerFn(signer, answerId, upvote, downvote); setTimeout(() => { setLoader(false); window?.location.reload(); }, 1000); }; const giveMainBounty = async (asnwerId) => { setLoader(true); const provider = new ethers.providers.Web3Provider(window.ethereum); await provider.send("eth_requestAccounts", []); const signer = await provider.getSigner(); const res = await distributeMainBountyFn(signer, questions, asnwerId); if (res) { setLikeError(false); document.getElementById("my_modal_1").showModal(); } setLoader(false); }; return (
{loader && }

{likeError ? "Already liked or disliked" : "Bounty already alloted"}

{likeError ? "You can give only one response!" : "Bounty has already been Given"}

{/* if there is a button in form, it will close the modal */}

{data?.question}f

{Array.isArray(data?.tags) ? data.tags.map((item, index) => (
{item}
)) : ""}
Asked{" "}
{!isQuestioner && ( Answer )}
{data ? data.like : 0}
{data ? data.dislike : 0}
D

{data ? `${data.address?.slice(0, 4)}..${data.address?.slice( data.address?.length - 4 )}` : 0}

2yr

{/* // ) : ( // // )} */}
Answers
{answerData?.map( ({ address, questionId, answer_Id, answer, time_of_creation, comments, upvote, downvote, }) => { // console.log(answerId); return (
D

{address.slice(0, 4)}... {address.slice(address.length - 4)}

2yr

Answered{" "}
{isQuestioner ? ( ) : (
)}
updateAnswer(answer_Id, 1, upvote + 1, downvote) } className=" h-8 w-8 cursor-pointer hover:scale-110 p-1 border rounded-tl-md rounded-bl-md " />

{upvote ? upvote : 0}

updateAnswer(answer_Id, 0, upvote, downvote + 1) } className=" h-8 w-8 cursor-pointer hover:scale-110 p-1 border rounded-tl-md rounded-bl-md " />

{downvote ? downvote : 0}

); } )}
Your Answer

{data?.time_based ? (

Time Remaining

Winner Bounty

{mainBounty ? ethers.utils.formatEther(mainBounty) : "0"}eth

Pool Bounty

{poolBounty ? ethers.utils.formatEther(poolBounty) : "0"}eth

Main Bounty Winner

{bountyWinner.slice(0, 6)}.... {bountyWinner.slice(bountyWinner.length - 5)}

) : (
No Bounty Active
)}
); }; export default Question;