-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProfile.js
275 lines (206 loc) · 8.52 KB
/
Profile.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import './Profile.css';
import { useParams, Link, useNavigate } from 'react-router-dom'
import { useMoralis } from "react-moralis";
import { useState, useEffect } from 'react'
import Button from '../components/Button'
import AssetDisplay from '../components/AssetDisplay'
import unbolt from '../truffle/build/contracts/UnBolt.json'
import contractAddress from '../constants/contractAddress';
// this is a handler for the profile page
const Profile = () => {
const {value} = useParams()
// The useMoralis hook provides all the basics functionalities that is needed for authentication and user data.
const {user,Moralis, setUserData,useMoralisQuery, authenticate, isAuthenticated, enableWeb3,isWeb3Enabled, logout } = useMoralis();
const [ethAddress, setEthAddress] = useState('');
const [bio, setBio] = useState('');
const [otherUserbio, setOtherUserBio] = useState('');
const [username, setUsername] = useState(user.get('username'));
const [name, setName] = useState(user.get(''));
const [userAssets, setUserAssets] = useState([]);
const navigate = useNavigate()
// 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 loadPage = async () => {
const connectorId = window.localStorage.getItem("connectorId");
if (isAuthenticated && !isWeb3Enabled){
await enableWeb3({ provider: connectorId });
}
if (bio== ''){
const bioValue = await getUsersBio()
if (bioValue != undefined) {
await setBio(bioValue)
} else {
await setBio("This user has not defined a bio")
}
}
if(userAssets.length == 0){
// call the function loadData() but wait 1 second before doing this in order to let enableweb3() run above
await setTimeout(function() { getUserAssets() }, 100);
}
}
loadPage()
},[isAuthenticated, isWeb3Enabled,bio]);
//add bio to database helper function
async function saveChange(){
//add conditions here to know that username is not taken
if (username!= user.get('username') && bio != user.get('bio')) {
const tempUsername = username
await setUserData({username})
await addBio()
console.log('bio:', bio," " );
await setUsername('')
await setBio('')
await alert("username and new bio added")
//navigate to the url of the new newusername as old endpoint doesnt work anymore
await navigate(`../profile/${tempUsername}`, { replace: true });
await window.location.reload();
//user is changing username only
} else if (username!= user.get('username') && bio == user.get('bio')){
const tempUsername = username
await setUserData({username})
await setUsername('')
await alert("username saved successfully")
//navigate to the url of the new newusername as old endpoint doesnt work anymore
await navigate(`../profile/${tempUsername}`, { replace: true });
await window.location.reload();
//user is chaning bio only
}else if (username== user.get('username') && bio!= user.get('bio')){
await addBio()
console.log('bio:', bio);
await setBio('')
await alert("Bio saved successfully")
//user has not changed anything
}else{
alert('Change username or add bio before submitting form! ')
}
}
//commit changes of bio to the database
async function addBio(){
// add new column bio in the users table
const user = Moralis.User.current();
user.set('bio',bio)
const result = await user.save();
console.log(result);
}
//commit changes of bio to the database
async function getUsersBio(){
// https://ethereum.stackexchange.com/questions/1374/how-can-i-check-if-an-ethereum-address-is-valid
function validateInputAddresses(address) {
return (/^(0x){1}[0-9a-fA-F]{40}$/i.test(address));
}
// if we have the eth address as the params
// get username only
if (validateInputAddresses(value)){
setEthAddress(value)
const results = await Moralis.Cloud.run("getUsernames", {address:value.toLowerCase()})
console.log(results[0].attributes.username);
setName(results[0].attributes.username)
return results[0].attributes.bio
// we have the user name, therefore lets get the address
// get user by username
}else{
setName(value)
const results = await Moralis.Cloud.run("getEthAddress", {name:value})
console.log(results[0]);
setEthAddress(results[0].attributes.ethAddress)
return results[0].attributes.bio
}
}
async function getUserAssets(){
//defining the parameters for the execute function call, which executes a function in the smart contract
console.log(unbolt.abi)
const options = {
abi: unbolt.abi,
contractAddress: contractAddress.unboltContractAddress,
functionName: 'getUserAssets',
params:{
_userAddress: ethAddress
}
}
//calls the smart contract function while returning the data in variable message
const message = await Moralis.executeFunction(options)
console.log(message);
setUserAssets(message)
}
return (
// <> { name != 'search' ? <> profile </> : <> fllf </> }</>
<div className='user-container-profile'>
{/* Profile of {params.name} */}
<div className='section-container-profile'>
<div className='section-header-profile' >@{name}</div>
<div className='section-header2-profile' >
<Link to={'/dashboard'}> <small>Go Back to dashboard</small> </Link>
</div>
<div>
{/* <p className='profile-message'> */}
{/* Loading of Assets asscoaited with {address} coming soon */}
{/* {otherUserbio} */}
{/* </p> */}
{ user.get('username') != name ?
<div className='bio-container'>
{/* <Button text={'test'} classVar='dark' onClick = {(e) => getUsersBio()}/> */}
{bio}
</div>
:
<div>
{/* <Button text={'test'} classVar='dark' onClick = {(e) => getUsersBio()}/> */}
<div className='bio-container'>
{bio}
</div>
{/* <input type='text' value={bio} placeholder = 'Add Bio...' onChange = {(e) => setBio(e.target.value)}/> */}
<div className='form-box'>
<div className='input'>
<label className='input-title'>Username</label>
<div className='input-box'>
<input type='text' value={username} placeholder = 'Add New Message...' onChange = {(e) => setUsername(e.target.value)}/>
</div>
</div>
</div>
<div className='form-box'>
<div className='input'>
<label className='input-title'>Bio</label>
<div className='input-box'>
<input className='bio' type='text' value={bio} placeholder = 'Add Bio...' onChange = {(e) => setBio(e.target.value)}/>
</div>
</div>
</div>
<Button text={'Save Bio'} classVar='dark' onClick = {(e) => saveChange()}/>
</div>
}
<div className='user-asset-display'>
{ userAssets.length > 0 ?
<div>
<div className='item-header'>
All assets asscoaited with this user
</div>
{userAssets.map((item)=>{
return(
<div className='item' key={parseInt(item.id)}>
<AssetDisplay
id={item.id}
assetName={item.assetName}
username={item.creator.slice(0,4)+'...'+item.creator.slice(-4)}
onClick={(e)=>{console.log(item.assetName)}
} />
</div>
)
})}
</div>
:
<>
No assets asscoaited with this user
</>
}
{/* <Button text={'test'} classVar='dark' onClick = {(e) => console.log(userAssets)}/>
<Button text={'call'} classVar='dark' onClick = {(e) => getUserAssets()}/> */}
</div>
</div>
</div>
<div/>
</div>
);
}
export default Profile;