From be414d4bbc78ee3ea3aeed3083c1e0249426c345 Mon Sep 17 00:00:00 2001 From: rebekah-wang Date: Tue, 25 Jul 2023 21:37:40 -0400 Subject: [PATCH] finished dataset and dashboard pages --- blockchain/generate_data.py | 2 +- client/src/components/NavBar.jsx | 7 -- client/src/pages/DashboardPage.jsx | 24 +--- client/src/pages/DatasetPage.jsx | 172 +++++++++++++++++++++++------ 4 files changed, 143 insertions(+), 62 deletions(-) diff --git a/blockchain/generate_data.py b/blockchain/generate_data.py index 1dba046..3aaf5ac 100644 --- a/blockchain/generate_data.py +++ b/blockchain/generate_data.py @@ -40,7 +40,7 @@ test_data = dataset.data for i, tensor in enumerate(test_data): - img = transforms.ToPILImage()(tensor) + img = transforms.ToPILImage()(test_data[1]) base64_img = base64.b64encode(img.tobytes()) img.save("data/" + str(i) + ".png") break diff --git a/client/src/components/NavBar.jsx b/client/src/components/NavBar.jsx index f087669..4a613ed 100644 --- a/client/src/components/NavBar.jsx +++ b/client/src/components/NavBar.jsx @@ -34,13 +34,6 @@ function NavBar() { ) : null} - {auth ? ( -
  • - - Datasets - -
  • - ) : null} {auth ? (
  • diff --git a/client/src/pages/DashboardPage.jsx b/client/src/pages/DashboardPage.jsx index acf2c0d..45da4dc 100644 --- a/client/src/pages/DashboardPage.jsx +++ b/client/src/pages/DashboardPage.jsx @@ -95,7 +95,7 @@ function DashboardPage() {

    Datasets

      - {datasets.map((dataset) => { + {datasets.map((dataset, index) => { console.log(dataset); return (
    • @@ -104,28 +104,8 @@ function DashboardPage() { Description: {dataset.description}

      - Link + Link

      -

      Images:

      -
        - {dataset.images.map((image) => { - console.log(image); - return ( -
      • -

        {image.class}

        -

        - {image.value.substring(0, 22) === - "data:image/png;base64," ? ( - {image.class} - ) : ( - image.value - )} -

        -

        {image.approved ? "Approved" : "Not Approved"}

        -
      • - ); - })} -
    • ); })} diff --git a/client/src/pages/DatasetPage.jsx b/client/src/pages/DatasetPage.jsx index cbbd73e..375eec7 100644 --- a/client/src/pages/DatasetPage.jsx +++ b/client/src/pages/DatasetPage.jsx @@ -1,7 +1,13 @@ import { useEffect, useState } from "react"; +import web3 from "web3"; + // import { Grid } from "react-visual-grid"; import { Image } from "../components/Image"; +import { getABI } from "../api/abi"; +import { getDataset } from "../api/dataset"; + +import { DataContext } from "../components/DataProvider"; import styles from "../styles/pages/DatasetPage.module.css"; @@ -12,25 +18,134 @@ function DatasetPage() { const [id, setId] = useState(null); - const [images, setImage] = useState([ - { text: "../assets/dog1.png" }, - { text: "../assets/dog2.png" }, - { text: "../assets/dog3.png" }, - ]); - //code the img retrieval - // add key - - const addList = () => { - const newImage = { id: Math.random(), text: "New todo" }; - setImage([images, newImage]); - }; + const [dataImages, setDataImages] = useState([]); + + const verifyImage = (index) => { + const load = async () => { + let datasetABI = await getABI("1"); + let imageABI = await getABI("2"); + + if (datasetABI === null || imageABI === null) { + alert("Error loading dataset manager or dataset ABI."); + return; + } + + window.web3 = new web3(window.ethereum); + + const urlParams = new URLSearchParams(window.location.search); + let id = urlParams.get("id"); + setId(id); + + if (id === null) { + alert("ID not found."); + return; + } + + let dataset = await getDataset(id); + if (dataset === null) { + alert("Dataset not found."); + return; + } + + console.log(dataset); + + let DatasetContract = new window.web3.eth.Contract( + datasetABI, + dataset[0].address, + ); + + let image = await DatasetContract.methods.getImage(index); + let address = await image.call(); + let ImageContract = new window.web3.eth.Contract(imageABI, address); + await ImageContract.methods + .approve() + .send({ from: window.ethereum.selectedAddress }); + + let images = []; + let imageCount = await DatasetContract.methods.getImageCount().call(); + + for (let j = 0; j < imageCount; j++) { + let imageAddress = await DatasetContract.methods.getImage(j).call(); + + let ImageContract = await new window.web3.eth.Contract( + imageABI, + imageAddress, + ); + + let image = { + class: await ImageContract.methods.getClass().call(), + value: await ImageContract.methods.getValue().call(), + approved: await ImageContract.methods.getApproved().call(), + }; + + images.push(image); + } + setDataImages(images); - console.log(id); + alert("Image approved."); + }; + + load(); + }; useEffect(() => { - const urlParams = new URLSearchParams(window.location.search); - let id = urlParams.get("id"); - setId(id); + const load = async () => { + let datasetABI = await getABI("1"); + let imageABI = await getABI("2"); + + if (datasetABI === null || imageABI === null) { + alert("Error loading dataset manager or dataset ABI."); + return; + } + + window.web3 = new web3(window.ethereum); + + const urlParams = new URLSearchParams(window.location.search); + let id = urlParams.get("id"); + setId(id); + + if (id === null) { + alert("ID not found."); + return; + } + + let dataset = await getDataset(id); + if (dataset === null) { + alert("Dataset not found."); + return; + } + + console.log(dataset); + + let images = []; + + let DatasetContract = new window.web3.eth.Contract( + datasetABI, + dataset[0].address, + ); + + let imageCount = await DatasetContract.methods.getImageCount().call(); + + for (let j = 0; j < imageCount; j++) { + let imageAddress = await DatasetContract.methods.getImage(j).call(); + + let ImageContract = await new window.web3.eth.Contract( + imageABI, + imageAddress, + ); + + let image = { + class: await ImageContract.methods.getClass().call(), + value: await ImageContract.methods.getValue().call(), + approved: await ImageContract.methods.getApproved().call(), + }; + + images.push(image); + } + setDataImages(images); + }; + + load(); }, []); if (id === null) { @@ -45,24 +160,17 @@ function DatasetPage() {
        - {images.map((images, index) => ( -
      • {images.text}
      • + {dataImages.map((image, index) => ( +
      • +

        {image.class}

        + {image.class + +
      • ))}
      - {/* ; */} - {"test + {/* {data && data.length>0 && data.map((item)=>

      {item.about}

      )} */} {"ID:" + id}