{"size": 3207, "ext": "js", "max_stars_repo_path": "client/src/components/mypapers/Mypapers.js", "max_stars_repo_name": "SairajK19/PaperHouse", "max_stars_repo_stars_event_min_datetime": "2021-08-09T10:17:54.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-31T07:19:58.000Z", "max_issues_repo_path": "client/src/components/mypapers/Mypapers.js", "max_issues_repo_name": "SairajK19/PaperHouse", "max_issues_repo_issues_event_min_datetime": "2021-10-03T07:27:51.000Z", "max_issues_repo_issues_event_max_datetime": "2022-02-14T14:36:56.000Z", "max_forks_repo_path": "client/src/components/mypapers/Mypapers.js", "max_forks_repo_name": "SairajK19/PaperHouse", "max_forks_repo_forks_event_min_datetime": "2021-09-16T10:23:56.000Z", "max_forks_repo_forks_event_max_datetime": "2022-02-18T03:21:06.000Z"}
{"max_stars_count": 13, "max_issues_count": 4, "max_forks_count": 9, "avg_line_length": 29.4220183486, "max_line_length": 79, "alphanum_fraction": 0.5687558466}
import React from "react";
import "./mypapers.css";
import PaperCard from "../explore/PaperCard";
import { useSelector } from "react-redux";
import { ToastContainer, toast } from "react-toastify";
import Web3 from "web3";
import { PaperCardLoading } from "../paperCardLoading/index";
import ConnectWallet from "../connectWallet/ConnectWallet";
import NoPapers from "../NoPapers/NoPapers";

export const Mypapers = ({ path }) => {
  const paperData = useSelector((state) => state.paper.myPapers).data;
  const { address } = useSelector((state) => state.paper.wallet);
  const myPapersLoading = useSelector((state) => state.paper.myPapers.loading);

  return (
    <>
      <ToastContainer
        className="toast-margin"
        position="top-right"
        autoClose={5000}
        hideProgressBar={false}
        newestOnTop={false}
        closeOnClick
        rtl={false}
        pauseOnFocusLoss
        draggable
        pauseOnHover
      />
      <div className="mypapers">
        {!myPapersLoading ? (
          paperData.length !== 0 ? (
            <div className="mypapers_papers">
              <PaperCardRenderer data={paperData} path={path} />
            </div>
          ) : !address ? (
            <div className="mypapers_papers" style={{ display: "block" }}>
              <ConnectWallet />
            </div>
          ) : (
            <NoPapers />
          )
        ) : (
          <div className="mypapers_papers">
            {[1, 2, 3, 4, 5].map((item) => (
              <PaperCardLoading page={"/mypapers"} />
            ))}
          </div>
        )}
      </div>
    </>
  );
};

const PaperCardRenderer = ({ data, path }) => {
  const { connected, address, correctNetwork } = useSelector(
    (state) => state.paper.wallet
  );
  const contract = useSelector((state) => state.paper.contract);

  const toastStyles = {
    position: "top-right",
    autoClose: 5000,
    hideProgressBar: false,
    closeOnClick: true,
    pauseOnHover: true,
    draggable: true,
    progress: undefined,
  };

  function UpdatePaper(paperid, updateAmount, fundToggle, setUpdating) {
    let fund = fundToggle ? updateAmount : "0";

    if (!updateAmount) fund = "0";
    if (Number(updateAmount) < 0) fund = "0";

    if (connected && correctNetwork) {
      setUpdating(true);
      contract.methods
        .updatepaper(paperid, fundToggle, Web3.utils.toWei(fund, "ether"))
        .send({ from: address })
        .then(() => {
          toast("🦄️ Research Paper Updated!", toastStyles);
          setUpdating(false);
        })
        .catch((error) => {
          toast("Error while updating paper, try again...", toastStyles);
          setUpdating(false);
          console.log(error);
        });
    }
  }

  return data.map((paper) => {
    return (
        <PaperCard
          data={paper}
          page={path === "/profile" ? "" : "mypapers"}
          currentAmount={`${paper.fundAmount} MATIC`}
          callupdate={(updateAmount, fundToggle, setUpdating) =>
            UpdatePaper(paper.paperid, updateAmount, fundToggle, setUpdating)
          }
          allowFunding={paper.allowFunding}
          paperLink={`/paper/${paper.paperid}`}
        />
    );
  });
};
