-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIntermediaries.js
133 lines (107 loc) · 5.24 KB
/
Intermediaries.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import { useState, useEffect } from 'react'
import {Link} from 'react-router-dom'
import { useMoralis, useWeb3ExecuteFunction } from "react-moralis";
import Button from '../components/Button'
import Navbar from '../components/Navbar'
import Input from '../components/Input' //at the moment this is only used to target the css, using componets causes some errors!
import './Intermediaries.css';
// import assets from '../truffle/build/contracts/Assets.json'
import contractAddress from '../constants/contractAddress';
import unbolt from '../truffle/build/contracts/UnBolt.json'
const Intermediaries = () => {
// The useMoralis hook provides all the basics functionalities that is needed for authentication and user data.
const {user,Moralis, setUserData,useMoralisQuery } = useMoralis();
// state hooks to know which body to use for the dashbaord
const [username, setUsername] = useState(user.get('username'));
const [intermediaryName, setIntermediaryName] = useState('');
const [intermediaryAddress, setIntermediaryAddress] = useState('');
const [toggleAdd, setToggleAdd] = useState(false);
const [toggleView, setToggleView] = useState(false);
const [buttonTextAdd, setButtonTextAdd] = useState('Add');
const [buttonTextView, setButtonTextView] = useState('View Exisiting Intermediaries');
const [intermedairyList, setIntermediaryList]= useState([])
async function onSubmit(e){
//user is addinng an intermediary
if (intermediaryName != '' && intermediaryAddress!= ''){
addIntermediary()
console.log('name:', intermediaryName," ", 'addr:',intermediaryAddress );
setIntermediaryName('')
setIntermediaryAddress('')
//user has not enter details in form
}else{
alert('add intermediary details before submitting form! ')
}
}
async function addIntermediary(){
// creating a new subclass of of an Intermediary Object.
//creates a new table db table called Intermediary (if not exisiting)
const Intermediary = Moralis.Object.extend("Intermediary"); //creates a new table db table called Intermediary
// Create a new instance of that class.
//use this object to add data to a row of that table
const intermediary = new Intermediary();
// map the current user to the intermediry relationship
intermediary.set('user', user.get('username'))
intermediary.set('name', intermediaryName)
intermediary.set('ethAddress', intermediaryAddress)
//create a new column userRelationship which holds the a user object
//database relation (user-> intermediary relationship)
const relation = intermediary.relation("userRelationship");
relation.add(Moralis.User.current());
//save the intermediary
const result = await intermediary.save()
//create a relation in the user table and another column called users
createRelationship(intermediary)
// alert("Successfully added " + results.length + " intermediary");
alert("Successfully added intermediary");
console.log(result); // if result try to
console.log(user); // if result try to
}
async function createRelationship(_intermediary){
//add relation in the intermedairy table as users
const intermedairy = _intermediary
const relation1 = intermedairy.relation("users")
relation1.add(Moralis.User.current());
const result1 = await intermedairy.save();
console.log(result1);
// add the realtion in the users table as Intermediaries
const user = Moralis.User.current();
const relation = user.relation("Intermediaries");
relation.add(_intermediary);
const result = await user.save();
console.log(result);
}
return (
<>
<Navbar/>
<div className='createAsset'>
{/* <Link to= '/dashboard'> <Button color='black' text={'Go Back'} /> </Link> */}
<div className='title'>Add an Intermediary Relationship</div>
<div className='createAsset-form'>
<div className='createAsset-data'>
<div className='form-box'>
<div className='input'>
<label className='input-title'>Name*</label>
<div className='input-box'>
<input type='text' value={intermediaryName} placeholder = 'Add Asset Name...' onChange = {(e) => setIntermediaryName(e.target.value)}/>
</div>
</div>
</div>
<div className='form-box'>
<div className='input'>
<label className='input-title'>Ethereum Address*</label>
<div className='input-box'>
<input type='text' value={intermediaryAddress} placeholder = 'Add New Description...' onChange = {(e) => setIntermediaryAddress(e.target.value)}/>
</div>
</div>
</div>
</div>
</div>
<div className='createAsset-button-container'>
<Link to= '/dashboard'> <Button text={'Go Back'} /> </Link>
<Button text={"Complete"} classVar={'dark'} onClick ={(e) => onSubmit(e)} />
</div>
</div>
</>
);
}
export default Intermediaries