Skip to content

Commit

Permalink
making react.. wip
Browse files Browse the repository at this point in the history
  • Loading branch information
newbreedofgeek committed May 18, 2021
1 parent 7441173 commit ee05b95
Show file tree
Hide file tree
Showing 15 changed files with 19,803 additions and 205 deletions.
26 changes: 25 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,28 @@ dist
.tern-port

# other
/src/secrets.js
secrets.js

# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# production
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*
19,340 changes: 19,340 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

44 changes: 37 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,48 @@
"name": "data-dex",
"version": "0.0.1",
"description": "The DEX where you can package and sell personal data",
"main": "index.html",
"dependencies": {
"@chakra-ui/react": "^1.5.2",
"@emotion/react": "^11.1.5",
"@emotion/styled": "^11.3.0",
"@testing-library/jest-dom": "^5.11.4",
"@testing-library/react": "^11.1.0",
"@testing-library/user-event": "^12.1.10",
"framer-motion": "^4.1.6",
"moralis": "^0.0.15",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-moralis": "^0.1.1",
"react-scripts": "4.0.3",
"web-vitals": "^1.0.1"
},
"scripts": {
"start": "npx http-server"
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"start-flat": "npx http-server -o /flat"
},
"repository": {
"type": "git",
"url": "git+https://github.com/Itheum/data-dex.git"
},
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/Itheum/data-dex/issues"
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"homepage": "https://github.com/Itheum/data-dex#readme"
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
Binary file added public/favicon.ico
Binary file not shown.
4 changes: 2 additions & 2 deletions index.html → public/flat/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<script src="https://cdn.jsdelivr.net/npm/web3@latest/dist/web3.min.js"></script>
<script src="https://unpkg.com/moralis/dist/moralis.js"></script>

<link href="src/style.css" rel="stylesheet">
<link href="style.css" rel="stylesheet">

<title>Itheum Data Dex</title>
</head>
Expand Down Expand Up @@ -77,7 +77,7 @@
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js" integrity="sha384-j0CNLUeiqtyaRmlzUHCPZ+Gy5fQu0dQ6eZ/xAww941Ai1SxSY+0EQqNXNE6DZiVc" crossorigin="anonymous"></script>
-->

<script src="src/app.js" type="module"></script>
<script src="lib.js" type="module"></script>
</body>
</html>

Expand Down
199 changes: 199 additions & 0 deletions public/flat/lib.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
// import secrets (these are exported as like "export const appId = 'xxx';")
import {appId, serverURL} from './secrets.js';

const templates = {
dataOrder: {
state: 1,
sellerEthAddress: null,
data: null
}
}

const btnRefs = {
metamaskLogin: null,
fullmenu: null
};

window.state = {
loggedIn: false,
user: null,
currBuyObjectId: ''
};

window.requestToBuy = (objectId) => {
console.log(objectId);

window.state.currBuyObjectId = objectId;

btnRefs.buyPanelReason.style.display = 'block';
}

const sendBuyOffer = () => {
if (btnRefs.reasonToBuy.value.trim() === '') {
alert('You need to provide a reason to buy this');
} else {
console.log('lets buy ', window.state.currBuyObjectId);
console.log('reason ', btnRefs.reasonToBuy.value);
}
}

const onPageLoad = () => {
// elem refs
btnRefs.alerts = document.getElementById('alerts');
btnRefs.metamaskLogin = document.getElementById('metamask-login');
btnRefs.logout = document.getElementById('metamask-logout');
btnRefs.metamaskUser = document.getElementById('metamask-user');
btnRefs.fullmenu = document.getElementById('full-menu');
btnRefs.sellData = document.getElementById('sell-data');
btnRefs.buyData = document.getElementById('buy-data');
btnRefs.buyPanelReason = document.getElementById('buy-panel-reason');
btnRefs.reasonToBuy = document.getElementById('reasonToBuy');
btnRefs.sendBuyOffer = document.getElementById('sendBuyOffer');
btnRefs.sellPanel = document.getElementById('sell-panel');
btnRefs.buyPanel = document.getElementById('buy-panel');
btnRefs.dataOrdersView = document.getElementById('dataOrdersView');

// listeners
btnRefs.metamaskLogin.addEventListener('click', login);
btnRefs.logout.addEventListener('click', logout);
btnRefs.sellData.addEventListener('click', sellView);
btnRefs.buyData.addEventListener('click', buyView);
btnRefs.sendBuyOffer.addEventListener('click', sendBuyOffer);

document.getElementById('sellerOrder').addEventListener('click', sellOrderSubmit);

initMoralis();
}

const initMoralis = async () => {
Moralis.initialize(appId);
Moralis.serverURL = serverURL;

window.web3 = await Moralis.Web3.enable();

// is user already logged in?
const user = await Moralis.User.current();

if (user) {
console.log('🚀 ~ user in session');

showUser(user);
}
}

const login = async () => {
try {
const user = await Moralis.Web3.authenticate();
console.log('🚀 ~ login done');

showUser(user);
} catch (error) {
const code = error.code;
const message = error.message;

console.log('🚀 ~ login err= ~ code', code);
console.log('🚀 ~ login err= ~ message', message);
}
}

const logout = async () => {
await Moralis.User.logOut();
console.log('🚀 ~ logout done');

showUser();
}

const showUser = user => {
console.log('user = ', user);

btnRefs.metamaskUser.innerHTML = user && user.get('ethAddress') || '';
btnRefs.fullmenu.style.display = user && 'block' || 'none';
btnRefs.logout.style.display = user && 'block' || 'none';
btnRefs.metamaskLogin.style.display = user && 'none' || 'block';

window.state.loggedIn = !!user;
window.state.user = user;
}

const buyView = async() => {
setAlert('');

btnRefs.buyPanel.style.display = 'block';
btnRefs.sellPanel.style.display = 'none';

const DataOrder = Moralis.Object.extend("DataOrder");
const query = new Moralis.Query(DataOrder);

query.equalTo("state", '1');

const results = await query.find();

setAlert('Successfully retrieved ' + results.length + ' data orders available for sale');

let dataorders = '';

for (let i = 0; i < results.length; i++) {
const object = results[i];
dataorders += object.id + ' - ' + object.get('sellerEthAddress') + ' - ' + object.get('data');

// if curr user address is not seller's then show a 'buy' button
if (object.get('sellerEthAddress') !== window.state.user.get('ethAddress')) {
dataorders += '<button type="button" class="btn btn-secondary" onclick="window.requestToBuy(\''+object.id+'\')">Request to buy</button>';
}

dataorders += '<br /><br />';
}

btnRefs.dataOrdersView.innerHTML = dataorders;
}

const sellView = () => {
setAlert('');

btnRefs.buyPanel.style.display = 'none';
btnRefs.sellPanel.style.display = 'block';

document.getElementById('sellerEthAddress').value = window.state.user.get('ethAddress');
}

const sellOrderSubmit = async () => {
// grab the data
const sellerData = document.getElementById('sellerData').value.trim();

if (sellerData === '') {
alert('You need to provide some data!')
} else {
// create the object
const newDataOrder = {...templates.dataOrder,
data: sellerData,
sellerEthAddress: window.state.user.get('ethAddress')
};

console.log('🚀 ~ sellOrderSubmit ~ newDataOrder', newDataOrder);
// encrypt

// store in moralis object storage (in future - store in IPFS)
const DataOrder = Moralis.Object.extend("DataOrder");
const dataOrder = new DataOrder();

let savedSataOrder = null;

try {
savedSataOrder = dataOrder.save(newDataOrder);

alert('Data Saved!');
} catch(error) {
const code = error.code;
const message = error.message;

console.log('🚀 ~ sellOrderSubmit err= ~ code', code);
console.log('🚀 ~ sellOrderSubmit err= ~ message', message);
}
}
}

const setAlert = msg => {
btnRefs.alerts.innerHTML = msg;
}

window.addEventListener('load', onPageLoad);
File renamed without changes.
43 changes: 43 additions & 0 deletions public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta
name="description"
content="Web site created using create-react-app"
/>
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
<!--
manifest.json provides metadata used when your web app is installed on a
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
-->
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<!--
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>React App</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.
You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.
To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
</body>
</html>
Binary file added public/logo192.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/logo512.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions public/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"short_name": "React App",
"name": "Create React App Sample",
"icons": [
{
"src": "favicon.ico",
"sizes": "64x64 32x32 24x24 16x16",
"type": "image/x-icon"
},
{
"src": "logo192.png",
"type": "image/png",
"sizes": "192x192"
},
{
"src": "logo512.png",
"type": "image/png",
"sizes": "512x512"
}
],
"start_url": ".",
"display": "standalone",
"theme_color": "#000000",
"background_color": "#ffffff"
}
3 changes: 3 additions & 0 deletions public/robots.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# https://www.robotstxt.org/robotstxt.html
User-agent: *
Disallow:
Loading

0 comments on commit ee05b95

Please sign in to comment.