-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCreateTask.js
194 lines (166 loc) · 7.87 KB
/
CreateTask.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import { useState, useEffect } from 'react'
import { useMoralis, useWeb3ExecuteFunction } from "react-moralis";
import './CreateTask.css';
import Button from '../components/Button'
import Navbar from '../components/Navbar'
import { useParams,Link, Navigate, useNavigate, useLocation} from 'react-router-dom'
// import tasks from '../truffle/build/contracts/Tasks.json'
import contractAddress from '../constants/contractAddress';
import unbolt from '../truffle/build/contracts/UnBolt.json'
const CreateTask = () => {
const navigate = useNavigate()
const params = useParams()
// The useMoralis hook provides all the basics functionalities that is needed for authentication and user data.
const { Moralis, 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 [content, setContent] = useState('')
const [creatorMessage, setCreatorMessage] = useState('')
const [signator, setSignator] = useState('')
const [intermediaryList, setIntermediaryList]= 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 });
}
if(intermediaryList.length < 1){
getAllIntermediaries()
}
}, [isAuthenticated, isWeb3Enabled,intermediaryList]);
//getting an asset data based on the index of the mapping from the smart contract, returns a message object
async function getAssetDataIndex(){
//defining the parameters for the execute function call, which executes a function in the smart contract
const options = {
abi: unbolt.abi,
contractAddress: contractAddress.unboltContractAddress,
functionName: 'assets',
//empty parameter because this getter is generated automatically by solidity on creation of a mapping
params: {
'': params.id-1,
}
}
//calls the smart contract function while returning the data in variable message
const message = await Moralis.executeFunction(options)
console.log(message);
return message.creator
}
async function onSubmit(e){
await e.preventDefault()
if (!content | !creatorMessage| !signator ) {
alert('Fill in the missing field')
return
}
console.log('content:',content,'msg:',creatorMessage,'id:',params.id,'sig:',signator);
const creator = await getAssetDataIndex();
await setSignator(creator)
//check that the signed in user is the asset creator for the asset they are trying to change
const _currUser = await user.get("ethAddress")
if ( _currUser.toLowerCase() != creator.toLowerCase() ){
await alert('User does not have access to add task to this application')
console.log(_currUser, "L", creator);
return
}else{
// maybe take in props which is the asset id which is the params.id
console.log(unbolt.abi);
const options = {
abi: unbolt.abi,
contractAddress: contractAddress.unboltContractAddress,
functionName: 'createTask',
params: {
_taskContent: content,
_note: creatorMessage,
_assetId: params.id,
_signator: signator
}
}
const message = await fetch({params: options})
console.log(message);
await console.log("Error: ",error);
await console.log("Data: ",data);
}
setContent('')
setCreatorMessage('')
setSignator('')
alert("Task Created Go back to Asset page to View :)")
}
// function for getting all the intermediaries related to a current user
async function getAllIntermediaries(){
const Intermediary = Moralis.Object.extend("Intermediary");
const query = new Moralis.Query(Intermediary);
// query.equalTo("user", user.get('username'));
query.equalTo("users", Moralis.User.current());
const results = await query.find();
// alert("Successfully retrieved " + results.length + " intermediaries.");
setIntermediaryList(results)
}
return (
<>
<Navbar/>
<div className='createAsset'>
{/* <Link to= '/dashboard'> <Button color='black' text={'Go Back'} /> </Link> */}
<div className='title'>Add a Task</div>
<div className='createAsset-form'>
<div className='createAsset-data'>
<div className='form-box'>
<div className='input'>
<label className='input-title'>Task Description</label>
<div className='input-box'>
<input type='text' value={content} placeholder = 'Add New Description...' onChange = {(e) => setContent(e.target.value)}/>
</div>
</div>
</div>
<div className='form-box'>
<div className='input'>
<label className='input-title'>Creators Message</label>
<div className='input-box'>
<input type='text' value={creatorMessage} placeholder = 'Add New Message...' onChange = {(e) => setCreatorMessage(e.target.value)}/>
</div>
</div>
</div>
<div className='form-box'>
<div className='input'>
<label className='input-title'>Signator</label>
<div className='input-box'>
<input type='text' value={signator} placeholder = 'Add Signator...' onChange = {(e) => setSignator(e.target.value)}/>
</div>
</div>
</div>
<div className='form-box'>
<div className='select'>
<label className='select-title'>Search Intermediaries</label>
<div className='select-box'>
<select value={signator} onChange={(e) => setSignator(e.target.value)}>
<option defaultValue>None</option>
{ intermediaryList.length > 0 &&
<>
{ intermediaryList.map((item)=>{
// console.log(item.get("name"));
return(
<option key={item.id} value={item.attributes.ethAddress}>{item.get("name")}</option>
);
})
}
</>
}
</select>
</div>
</div>
</div>
</div>
</div>
<div className='createAsset-button-container'>
<Link to= {`/asset/${params.id}`}> <Button text={'Go Back'} /> </Link>
<Button text={"Complete"} classVar={'dark'} onClick ={(e) => onSubmit(e)} />
</div>
</div>
</>
);
}
export default CreateTask