Skip to content

Commit

Permalink
Handle word preview on tx rejection + wait for confirm before fetchin…
Browse files Browse the repository at this point in the history
…g blockchain
  • Loading branch information
PhiMarHal authored Nov 6, 2024
1 parent 0e71e3b commit 7559b55
Showing 1 changed file with 38 additions and 17 deletions.
55 changes: 38 additions & 17 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ async function updateWordsDisplay() {
});
}

function showWordPopup(wordIndex, wordInfo) {
async function showWordPopup(wordIndex, wordInfo) {
if (!userAddress) {
showStatus('Please connect your wallet first', 'error');
return;
Expand Down Expand Up @@ -406,13 +406,6 @@ function showWordPopup(wordIndex, wordInfo) {
content.appendChild(info);
popup.appendChild(content);

// Set up event listeners
popup.addEventListener('click', (e) => {
if (e.target === popup) {
popup.remove();
}
});

button.addEventListener('click', async () => {
const newWord = input.value.trim();
if (!newWord) {
Expand Down Expand Up @@ -446,16 +439,37 @@ function showWordPopup(wordIndex, wordInfo) {
// Close popup immediately
popup.remove();

// Send transaction
const tx = await contract.contribute(wordIndex, newWord);
showStatus('Transaction sent! Waiting for confirmation...', 'success');

// Wait for transaction confirmation
await tx.wait();
showStatus('Word contributed successfully!', 'success');
try {
// Send transaction
const tx = await contract.contribute(wordIndex, newWord);
showStatus('Transaction sent! Waiting for confirmation...', 'success');

try {
// Wait for transaction confirmation
await tx.wait();
showStatus('Word contributed successfully!', 'success');
// Only now update with real data from blockchain
await updateSingleWord(wordIndex);
} catch (confirmError) {
showStatus('Transaction failed or was dropped', 'error');
// Revert to original state and fetch current blockchain state
if (originalWordInfo) {
wordCache.words[wordIndex] = originalWordInfo;
await updateWordsDisplay();
}
await updateSingleWord(wordIndex);
}

// Update with real data from blockchain
await updateSingleWord(wordIndex);
} catch (txError) {
// Transaction was rejected at wallet level
showStatus('Transaction cancelled', 'error');
// Revert to original state and fetch current blockchain state
if (originalWordInfo) {
wordCache.words[wordIndex] = originalWordInfo;
await updateWordsDisplay();
}
await updateSingleWord(wordIndex);
}

} catch (error) {
// Parse the error message
Expand Down Expand Up @@ -492,6 +506,13 @@ function showWordPopup(wordIndex, wordInfo) {
}
});

// Close popup when clicking outside
popup.addEventListener('click', (e) => {
if (e.target === popup) {
popup.remove();
}
});

document.body.appendChild(popup);
popup.style.display = 'flex';
input.focus();
Expand Down

0 comments on commit 7559b55

Please sign in to comment.