Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New #29

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

New #29

Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
218 changes: 110 additions & 108 deletions js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,129 +5,131 @@ let editions = [];
let dots = 1;

window.addEventListener('DOMContentLoaded', () => {
const onboarding = new MetaMaskOnboarding();
const onboardButton = document.getElementById('connectWallet');
let accounts;

const updateButton = async () => {
if (!MetaMaskOnboarding.isMetaMaskInstalled()) {
onboardButton.innerText = 'Install MetaMask!';
onboardButton.onclick = () => {
onboardButton.innerText = 'Connecting...';
onboardButton.disabled = true;
onboarding.startOnboarding();
};
} else if (accounts && accounts.length > 0) {
onboardButton.innerText = `✔ ...${accounts[0].slice(-4)}`;
onboardButton.disabled = true;
onboarding.stopOnboarding();
checkOwner(accounts[0]);
} else {
onboardButton.innerText = 'Connect MetaMask!';
onboardButton.onclick = async () => {
await window.ethereum.request({
method: 'eth_requestAccounts',
})
.then(function(accounts) {
onboardButton.innerText = `✔ ...${accounts[0].slice(-4)}`;
onboardButton.disabled = true;
checkOwner(accounts[0]);
const onboarding = new MetaMaskOnboarding();
const onboardButton = document.getElementById('connectWallet');
let accounts;

const updateButton = async() => {
if (!MetaMaskOnboarding.isMetaMaskInstalled()) {
onboardButton.innerText = 'Install MetaMask!';
onboardButton.onclick = () => {
onboardButton.innerText = 'Connecting...';
onboardButton.disabled = true;
onboarding.startOnboarding();
};
} else if (accounts && accounts.length > 0) {
onboardButton.innerText = `✔ ...${accounts[0].slice(-4)}`;
onboardButton.disabled = true;
onboarding.stopOnboarding();
checkOwner(accounts[0]);
} else {
onboardButton.innerText = 'Connect MetaMask!';
onboardButton.onclick = async() => {
await window.ethereum.request({
method: 'eth_requestAccounts',
})
.then(function(accounts) {
onboardButton.innerText = `✔ ...${accounts[0].slice(-4)}`;
onboardButton.disabled = true;
checkOwner(accounts[0]);
});
};
}
};

updateButton();
if (MetaMaskOnboarding.isMetaMaskInstalled()) {
window.ethereum.on('accountsChanged', (newAccounts) => {
accounts = newAccounts;
updateButton();
});
};
}
};

updateButton();
if (MetaMaskOnboarding.isMetaMaskInstalled()) {
window.ethereum.on('accountsChanged', (newAccounts) => {
accounts = newAccounts;
updateButton();
});
}
});

const checkOwner = async (account) => {
if(account) {
let isOwner = false;
let page = 1

const data = await fetchWithRetry(`/.netlify/functions/isowner/?wallet=${account}&page=${page}`);

isOwner = !isOwner ? data.isOwner : isOwner;
updateStatusText(isOwner, true)

editions = [...data.editions]
let nextPage = data.next_page

while(nextPage) {
page = nextPage
const data = await fetchWithRetry(`/.netlify/functions/isowner/?wallet=${account}&page=${page}`);

isOwner = !isOwner ? data.isOwner : isOwner;
updateStatusText(isOwner, true)

editions = [...editions, ...data.editions]
nextPage = data.next_page
}
const checkOwner = async(account) => {
if (account) {
let isOwner = false;
let page = 1

const data = await fetchWithRetry(`/.netlify/functions/isowner/?wallet=${account}&page=${page}`);

isOwner = !isOwner ? data.isOwner : isOwner;
updateStatusText(isOwner, true)

editions = [...data.editions]
let nextPage = data.next_page

while (nextPage) {
page = nextPage
const data = await fetchWithRetry(`/.netlify/functions/isowner/?wallet=${account}&page=${page}`);

updateStatusText(isOwner, false)
}
isOwner = !isOwner ? data.isOwner : isOwner;
updateStatusText(isOwner, true)

editions = [...editions, ...data.editions]
nextPage = data.next_page
}

updateStatusText(isOwner, false)
}
}

function updateStatusText(isOwner, checking) {
const statusText = document.querySelector('.owner-status');
if(checking) {
if(isOwner) {
statusText.innerText = `You do own ${COLLECTION_NAME}!! 😻 Let's see how many${renderDots(dots)}`;
} else {
statusText.innerText = `Checking to see if you own any ${COLLECTION_NAME} 😻${renderDots(dots)}`;
}
} else {
if(isOwner) {
statusText.innerText = `You own ${editions.length} ${COLLECTION_NAME}!! 😻`;
const statusText = document.querySelector('.owner-status');
if (checking) {
if (isOwner) {
statusText.innerText = `You do own ${COLLECTION_NAME}!! 😻 Let's see how many${renderDots(dots)}`;
} else {
statusText.innerText = `Checking to see if you own any ${COLLECTION_NAME} 😻${renderDots(dots)}`;
}
} else {
statusText.innerText = `You don't own any ${COLLECTION_NAME} 😿`;
if (isOwner) {
statusText.innerText = `You own ${editions.length} ${COLLECTION_NAME}!! 😻`;
} else {
statusText.innerText = `You don't own any ${COLLECTION_NAME} 😿`;
}
}
}
dots = dots === 3 ? 1 : dots + 1;
dots = dots === 3 ? 1 : dots + 1;
}

function renderDots(dots) {
let dotsString = '';
for (let i = 0; i < dots; i++) {
dotsString += '.';
}
return dotsString;
let dotsString = '';
for (let i = 0; i < dots; i++) {
dotsString += '.';
}
return dotsString;
}

function timer(ms) {
return new Promise(res => setTimeout(res, ms));
return new Promise(res => setTimeout(res, ms));
}

async function fetchWithRetry(url) {
await timer(TIMEOUT);
return new Promise((resolve, reject) => {
const fetch_retry = (_url) => {
return fetch(_url).then(async (res) => {
const status = res.status;

if(status === 200) {
return resolve(res.json());
}
else {
console.error(`ERROR STATUS: ${status}`)
console.log('Retrying')
await timer(TIMEOUT)
fetch_retry(_url)
}
})
.catch(async (error) => {
console.error(`CATCH ERROR: ${error}`)
console.log('Retrying')
await timer(TIMEOUT)
fetch_retry(_url)
});
}
return fetch_retry(url);
});
}
async function fetchWithRetry(url) {
await timer(TIMEOUT);
return new Promise((resolve, reject) => {
const fetch_retry = (_url) => {
return fetch(_url).then(async(res) => {
const status = res.status;

if (status === 200) {
return resolve(res.json());
} else {
console.error(`ERROR STATUS: ${status}`)
console.log('Retrying')
await timer(TIMEOUT)
fetch_retry(_url)
}
})
.catch(async(error) => {
console.error(`CATCH ERROR: ${error}`)
console.log('Retrying')
await timer(TIMEOUT)
fetch_retry(_url)
});
}
return fetch_retry(url);
});
}
window.addEventListener('load', () => {
console.log('hello');
});