"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 (
{data ? `${data.address?.slice(0, 4)}..${data.address?.slice( data.address?.length - 4 )}` : 0}
2yr
{address.slice(0, 4)}... {address.slice(address.length - 4)}
2yr
{upvote ? upvote : 0}
{downvote ? downvote : 0}
Winner Bounty
Pool Bounty
{bountyWinner.slice(0, 6)}.... {bountyWinner.slice(bountyWinner.length - 5)}