From c83f8d121adba92e45bf1989c334f909943a5959 Mon Sep 17 00:00:00 2001 From: Aziz Jalel Date: Sun, 18 Jun 2023 14:09:37 -0700 Subject: [PATCH] push to prod --- .env | 8 +++++++ src/components/Content.jsx | 45 ++++++++++++++++++++++++++++++++---- src/firebase/config.js | 22 ++++++++---------- src/pages/homepage.js | 8 +++---- src/pages/smart-contracts.js | 42 +++++++++++++++++++++++++++++---- 5 files changed, 101 insertions(+), 24 deletions(-) create mode 100644 .env diff --git a/.env b/.env new file mode 100644 index 0000000..f723a56 --- /dev/null +++ b/.env @@ -0,0 +1,8 @@ +REACT_APP_FIREBASE_API_KEY=AIzaSyDTRY5WPQm0v26OuJDvkZ-FwDiPMtoIg_U +REACT_APP_FIREBASE_AUTH_DOMAIN=indexsc-80164.firebaseapp.com +REACT_APP_FIREBASE_PROJECT_ID=indexsc-80164 +REACT_APP_FIREBASE_STORAGE_BUCKET=indexsc-80164.appspot.com +REACT_APP_FIREBASE_MESSAGING_SENDER_ID=450893406130 +REACT_APP_FIREBASE_APP_ID=1:450893406130:web:5326cc19258ad6e83f8eab +REACT_APP_FIREBASE_MEASUREMENT_ID=G-NN253G450K +REACT_APP_FIREBASE_DATABASE_URL=https://indexsc-80164-default-rtdb.firebaseio.com/ \ No newline at end of file diff --git a/src/components/Content.jsx b/src/components/Content.jsx index bad1ea7..2a5a02f 100644 --- a/src/components/Content.jsx +++ b/src/components/Content.jsx @@ -4,7 +4,13 @@ import { Link } from "react-router-dom"; import { doc, getDoc, - // onSnapshot, + query, + collection, + where, + getDocs, + deleteDoc, + arrayRemove, + updateDoc, } from "firebase/firestore"; import { db as fsDatabase } from "../firebase/config"; import { v4 as uuid4 } from "uuid"; @@ -20,10 +26,31 @@ import Tippy from "@tippyjs/react"; import "tippy.js/dist/tippy.css"; // optional for styling import "tippy.js/themes/light-border.css"; +const deleteContract = async (contractTimestamp, userId, setKey) => { + // console.log("deleteContract", contractId); + const contractRef = query( + collection(fsDatabase, "contracts"), + where("timestamp", "==", contractTimestamp) + ); + + const contractSnapshot = await getDocs(contractRef); + contractSnapshot.forEach(async (doc) => { + await deleteDoc(doc.ref); + }); + + // remove from smart_contracts array + const userRef = doc(fsDatabase, "users", userId); + await updateDoc(userRef, { + smart_contracts: arrayRemove(contractSnapshot.docs[0].id), + }); + setKey(uuid4()); +}; + const Content = ({ userData, searchQuery }) => { // console.log('userData', userData); const [contracts, setContracts] = useState([]); const [contractsLoaded, setContractsLoaded] = useState(false); + const [key, setKey] = useState(0); // console.log('contracts', contracts); useEffect(() => { @@ -31,7 +58,12 @@ const Content = ({ userData, searchQuery }) => { if (userData === null) { return; } - const contractIds = userData.smart_contracts; + + //refetch smart_contracts + const userRef = doc(fsDatabase, "users", userData.uid); + const userSnap = await getDoc(userRef); + const userData2 = userSnap.data(); + const contractIds = userData2.smart_contracts; // console.log('contractIds', contractIds); const contract = contractIds.map(async (contractId) => { const getSc = await getDoc(doc(fsDatabase, "contracts", contractId)); @@ -52,7 +84,7 @@ const Content = ({ userData, searchQuery }) => { setContractsLoaded(true); }; fetchContracts(); - }, [userData]); + }, [userData, key]); // filter contracts based on searchQuery on contract.name and contract.address useEffect(() => { @@ -388,8 +420,13 @@ const Content = ({ userData, searchQuery }) => { } flex items-center w-full px-4 py-2 text-sm`} onClick={() => { console.log("Delete clicked"); + // delete firestore item based on timestamp + deleteContract( + contract.timestamp, + userData.uid, + setKey + ); }} - disabled > Delete diff --git a/src/firebase/config.js b/src/firebase/config.js index 871e19f..3389a29 100644 --- a/src/firebase/config.js +++ b/src/firebase/config.js @@ -1,18 +1,16 @@ -import { initializeApp } from 'firebase/app'; -import { getAuth } from 'firebase/auth'; +import { initializeApp } from "firebase/app"; +import { getAuth } from "firebase/auth"; import { getFirestore } from "firebase/firestore"; import { getDatabase } from "firebase/database"; - - const firebaseConfig = { - apiKey: 'AIzaSyDTRY5WPQm0v26OuJDvkZ-FwDiPMtoIg_U', - authDomain: 'indexsc-80164.firebaseapp.com', - projectId: 'indexsc-80164', - storageBucket: 'indexsc-80164.appspot.com', - messagingSenderId: '450893406130', - appId: '1:450893406130:web:5326cc19258ad6e83f8eab', - measurementId: 'G-NN253G450K', - databaseURL: "https://indexsc-80164-default-rtdb.firebaseio.com/" + apiKey: process.env.REACT_APP_FIREBASE_API_KEY, + authDomain: process.env.REACT_APP_FIREBASE_AUTH_DOMAIN, + projectId: process.env.REACT_APP_FIREBASE_PROJECT_ID, + storageBucket: process.env.REACT_APP_FIREBASE_STORAGE_BUCKET, + messagingSenderId: process.env.REACT_APP_FIREBASE_MESSAGING_SENDER_ID, + appId: process.env.REACT_APP_FIREBASE_APP_ID, + measurementId: process.env.REACT_APP_FIREBASE_MEASUREMENT_ID, + databaseURL: process.env.REACT_APP_FIREBASE_DATABASE_URL, }; // Initialize Firebase diff --git a/src/pages/homepage.js b/src/pages/homepage.js index ee70b2d..6cf07fd 100644 --- a/src/pages/homepage.js +++ b/src/pages/homepage.js @@ -118,10 +118,10 @@ export const Homepage = () => { {/*
*/}

- Smoldata is Zero-ETL platform that delivers fast and simple - indexing for smart contracts. Replace time-consuming ETL processes - with direct data transfers between systems, and asynchronously - update your databases and API endpoints in real-time. + Smoldata is a platform that enables quick and easy indexing for + smart contracts, eliminating the need for time-consuming ETL + processes. It simplifies the process of ingesting smart contract + data with minimal alterations and transformations.

diff --git a/src/pages/smart-contracts.js b/src/pages/smart-contracts.js index 17970bf..40ba511 100644 --- a/src/pages/smart-contracts.js +++ b/src/pages/smart-contracts.js @@ -1,7 +1,17 @@ import React, { useState, useEffect } from "react"; import PropTypes from "prop-types"; // ES6 import { Link } from "react-router-dom"; -import { doc, getDoc } from "firebase/firestore"; +import { + doc, + getDoc, + query, + getDocs, + collection, + where, + updateDoc, + arrayRemove, + deleteDoc, +} from "firebase/firestore"; import { db as fsDatabase } from "../firebase/config"; import { v4 as uuid4 } from "uuid"; import { Menu, Transition } from "@headlessui/react"; @@ -30,6 +40,7 @@ const SmartContracts = ({ searchQuery }) => { const [userData, setUserData] = useState(null); const { user, loading } = useContext(AuthContext); const [filteredContracts, setFilteredContracts] = useState(contracts); + const [key, setKey] = useState(0); useEffect(() => { const fetchContracts = async () => { @@ -59,7 +70,7 @@ const SmartContracts = ({ searchQuery }) => { }; fetchContracts(); setContractsLoaded(true); - }, [userData]); + }, [userData, key]); useEffect(() => { // console.log("searchQuery", searchQuery, contracts); @@ -78,6 +89,25 @@ const SmartContracts = ({ searchQuery }) => { // setContracts(filteredContracts); }, [searchQuery, contractsLoaded]); + const deleteContract = async (contractTimestamp, userId, setKey) => { + // console.log("deleteContract", contractId); + const contractRef = query( + collection(fsDatabase, "contracts"), + where("timestamp", "==", contractTimestamp) + ); + + const contractSnapshot = await getDocs(contractRef); + contractSnapshot.forEach(async (doc) => { + await deleteDoc(doc.ref); + }); + + // remove from smart_contracts array + const userRef = doc(fsDatabase, "users", userId); + await updateDoc(userRef, { + smart_contracts: arrayRemove(contractSnapshot.docs[0].id), + }); + setKey(uuid4()); + }; if (loading && !user) { // console.log('Dashboard loading', loading, user); return ( @@ -239,7 +269,7 @@ const SmartContracts = ({ searchQuery }) => {
Add new contract @@ -387,7 +417,11 @@ const SmartContracts = ({ searchQuery }) => { : "text-gray-700" } flex items-center w-full px-4 py-2 text-sm`} onClick={() => { - console.log("Delete clicked"); + deleteContract( + contract.timestamp, + contract.id, + setKey + ); }} >