-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCreateAsset.js
137 lines (115 loc) · 5.31 KB
/
CreateAsset.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
133
134
135
136
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 './CreateAsset.css';
// import assets from '../truffle/build/contracts/Assets.json'
import contractAddress from '../constants/contractAddress';
import unbolt from '../truffle/build/contracts/UnBolt.json'
function onAdd (name,description,quantity){
console.log(name, ":", description, ":",quantity);
}
const CreateAsset = () => {
// The useMoralis hook provides all the basics functionalities that is needed for authentication and user data.
const { authenticate, isAuthenticated, enableWeb3,isWeb3Enabled, logout, user } = useMoralis();
// The useWeb3ExecuteFunction hook is used to execute on-chain functions.
// to call the on chain functions, an abi, contract address, functionName and params need to be specified
const {fetch, error, data} = useWeb3ExecuteFunction();
// useState gives a local state in a function component
// the first paramter is the value and the second is the setter
const [name, setName] = useState('')
const [description, setDescription] = useState('')
const [quantity, setQuantity] = useState(0)
const [complete, setComplete] = useState(false)
const [image, setImage] = useState('')
// allows for performing side effects in the component
// side effect in this case being calling the enableWeb3 function
// use effect runs after every render
// if the value of the second argunment changes(isAuthenticated, isWeb3Enabled), useEffect is rerun
useEffect(() => {
const connectorId = window.localStorage.getItem("connectorId");
if (isAuthenticated && !isWeb3Enabled)
enableWeb3({ provider: connectorId });
}, [isAuthenticated, isWeb3Enabled]);
// function for handling the submition of a form
// calls the smart contract function then clears the form
async function onSubmit(e){
console.log(unbolt.abi);
e.preventDefault()
//check if any input is missing
if (!name | !description | !quantity) {
alert('Fill in the missing field')
return
}
// console.log(assets.abi);
const options = {
abi: unbolt.abi,
contractAddress: contractAddress.unboltContractAddress,
functionName: 'createAsset',
params: {
_assetName: name,
_description: description,
_quantity: quantity
}
}
// calling of the smart contract function using the options const as a parameter
const message = await fetch({params: options})
console.log(message);
await console.log("Error: ",error);
await console.log("Data: ",data);
// for test purposes just print it in the onAdd function
// onAdd({name,description,quantity})
// set the local state to default (this changes the value in the form as the asset has already been created)
setName('')
setDescription('')
setQuantity(0)
setComplete(false)
}
return (
<>
<Navbar/>
<div className='createAsset'>
{/* <Link to= '/dashboard'> <Button color='black' text={'Go Back'} /> </Link> */}
<div className='title'>Add an Asset</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={name} placeholder = 'Add Asset Name...' onChange = {(e) => setName(e.target.value)}/>
</div>
</div>
</div>
<div className='form-box'>
<div className='input'>
<label className='input-title'>Asset Description*</label>
<div className='input-box'>
<input type='text' value={description} placeholder = 'Add New Description...' onChange = {(e) => setDescription(e.target.value)}/>
</div>
</div>
</div>
<div className='form-box'>
<div className='input'>
<label className='input-title'>Quantity*</label>
<div className='input-box'>
<input type='number' value={quantity} onChange = {(e) => setQuantity(e.target.value)}/>
</div>
</div>
</div>
</div>
<div className='createAsset-image-container'>
<input type="file"/>
</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 CreateAsset