Hi guys
Iâm trying to get my buttons for my NFT Marketplace to work and Iâm struggling with this. Can anyone help me please as Iâm new to coding. Ive pasted my HTML and JavaScript below.
Cheers
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Morarable</title>
<div>
<button id="btnConnect">Connect wallet</button>
<button id="btnUserInfo">Profile</button>
</div>
<div id="userInfo">
<h4>User Profile</h4>
<input type="text" id="txtUsername" required placeholder="Enter username">
<input type="text" id="txtEmail" placeholder="Enter email">
<small>Optional</small>
<img width="50' Height="50" src="" id="imgAvatar" alt="">
<label for="fileAvatar">Select Avatar</label>
<input type="file" id="FileAvatar">
<button id="btnLogout">Log out</button>
<button id="btnCloseUserInfo">Close</button>
<button id="btnSaveUserInfo">Save</button>
</div>
<script src="https://cdn.jsdeliver.net/npm/web3@latest/dist/web3.min.jsdeliver"></script>
<script src="https://unpkg.com/moralis/dist/moralis.js"></script>
<script src="main.js"> </script>
const serverUrl = âhttps://giwpmltygioj.usemoralis.com:2053/serverâ;
const appId = âxuOLzgsgWfTESSw258bWrBlV7f4diP05wWa1MmUUâ;
Moralis.start({ serverUrl, appId });
init = async () => {
hideElement(userInfo);
window.web3 = await Moralis.web3.enable();
initUser();
}
initUser = async () => {
if (await Moralis.User.current()){
hideElement(userConnectbutton);
showElement(userProfilebutton);
}else{
showElement(userConnectbutton);
hideElement(userProfilebutton);
}
}
login = async () => {
try {
await Moralis.web3.authenticate();
inituser();
} catch(error) {
alert(error)
}
}
logout = async () => {
await Moralis.User.logout();
hideElement(userInfo);
initUser();
}
openUserInfo = async () => {
user= await Moralis.current();
if (user){
const email = user.get('email');
if(email){
userEmailField.value = email;
}else{
userEmailField.value ="";
}
userUsernamefield.value = user.get()
const userAvatar = user.get('Avatar ');
if(userAvatar){
userAvatarImg.src = userAvatar.url();
showElement(userAvatarImg);
}else{
hideElement(userAvatarImg);
}
showElement(UserInfo);
}else{
login();
}
}
saveUserInfo = async () => {
user.set('email', userEmailField.value);
user.set('username', userUsernamefield.value);
if (userAvatarFile.files.length > 0) {
const Avatar = new Moralis.file("avatar.jpeg", userAvatarFile.files[0])
user.set('avatar', avatar);
}
await user.save();
alert("User info saved successfully")
openUserInfo();
}
hideElement = (element) => element.style.display = ânoneâ;
showElement = (element) => element.style.display = âblockâ;
const userConnectbutton = document.getElementbyid(âbtnConnectâ);
userConnectbutton.onclick = login;
const userProfilebutton = document.getElementbyid(âbtnUserInfoâ);
userProfilebutton.onclick = openUserInfo;
const userInfo = document.getElementbyid(âUserInfoâ);
const userUsernamefield = document.getElementbyid(âtxtUsernameâ);
const userEmailField = document.getElementbyid(âtxtEmailâ);
const userAvatarImg = document.getElementbyid(âimgAvatarâ);
const userAvatarFile = document.getElementbyid(âfileAvatarâ);
document.getElementbyid(âbtnCloseUserInfoâ).onclick = () => hideElement(userInfo);
document.getElementbyid(âbtnLogoutâ).onclick = logout;
document.getElementbyid(âbtnSaveUserInfoâ).onclick = saveUserInfo;
init();