{"size": 2211, "ext": "js", "max_stars_repo_path": "NFT/web/client/src/pages/home/index.js", "max_stars_repo_name": "CheesecakeLabs/researches", "max_stars_repo_stars_event_min_datetime": "2021-07-15T15:02:09.000Z", "max_stars_repo_stars_event_max_datetime": "2021-07-15T15:02:09.000Z", "max_issues_repo_path": "NFT/web/client/src/pages/home/index.js", "max_issues_repo_name": "CheesecakeLabs/researches", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "NFT/web/client/src/pages/home/index.js", "max_forks_repo_name": "CheesecakeLabs/researches", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 27.2962962963, "max_line_length": 71, "alphanum_fraction": 0.6490275893}
import React from "react";

import Container from "@material-ui/core/Container";
import Divider from "@material-ui/core/Divider";

import { toBigNumber, toEther } from "@helpers/web3";
import request from "@interfaces/http";
import usePoketoken from "@hooks/usePoketoken";
import Header from "@organisms/header";
import PokeList from "@organisms/poke-list";

function Home() {
  const { contract, myPokemons, pokemonsForSale, refreshLists } =
    usePoketoken();

  const mintToken = async () => {
    const transaction = await contract.createPokemon({
      value: toEther(process.env.REACT_APP_MINT_TAX),
    });
    const transactionResult = await transaction.wait();
    const tokenId = toBigNumber(transactionResult.events[0].topics[3]);
    await request(
      "post",
      `${process.env.REACT_APP_SERVER_URL}api/create-pokemon-meta`,
      { tokenId }
    );
    refreshLists();
  };

  const waitTransactionAndRefresh = async (transaction) => {
    await transaction.wait();
    refreshLists();
  };

  const addForSale = async (tokenId, value) => {
    const tx = await contract.addForSale(value, tokenId, {
      value: toEther(process.env.REACT_APP_SELL_TAX),
    });
    waitTransactionAndRefresh(tx);
  };

  const removeFromSale = async (tokenId) => {
    const tx = await contract.cancelSale(tokenId);
    waitTransactionAndRefresh(tx);
  };

  const buyFromSale = async (tokenId, value) => {
    const tx = await contract.buyFromSale(tokenId, { value });
    waitTransactionAndRefresh(tx);
  };

  return (
    <div className="Home">
      <Header
        onCreatePoketokenPress={mintToken}
        onRefreshPress={refreshLists}
      />
      <Container>
        <PokeList
          pokemons={myPokemons}
          title="My Pokemons"
          myPokemons={myPokemons}
          addForSale={addForSale}
          removeFromSale={removeFromSale}
          buyFromSale={buyFromSale}
        />
        <Divider light />
        <PokeList
          pokemons={pokemonsForSale}
          title="Pokemons for sale"
          myPokemons={myPokemons}
          removeFromSale={removeFromSale}
          buyFromSale={buyFromSale}
        />
      </Container>
    </div>
  );
}

export default Home;
