From 8f1ff6db2e456e7291441b4f1c3477f42e62d614 Mon Sep 17 00:00:00 2001 From: Phillweston <2436559745@qq.com> Date: Thu, 16 May 2024 18:41:11 +0800 Subject: [PATCH 1/6] Optimize the handleAction code --- js/twitter-action.js | 116 +++++++++++++------------------------------ 1 file changed, 34 insertions(+), 82 deletions(-) diff --git a/js/twitter-action.js b/js/twitter-action.js index a41f843..35c7a78 100644 --- a/js/twitter-action.js +++ b/js/twitter-action.js @@ -174,39 +174,28 @@ function displayInfo(action, message, type) { async function handleAction(action) { try { + const actionConfig = { + 'follow-us': { configKey: 'userName', actionType: 'follow-us', info: 'follow-info', progress: 'follow-progress', section: 'follow-section' }, + 'retweet-2': { configKey: 'tweetId2', actionType: 'retweet', info: 'retweet-2-info', progress: 'retweet-2-progress', section: 'retweet-section-2' }, + 'retweet': { configKey: 'tweetId', actionType: 'retweet', info: 'retweet-info', progress: 'retweet-progress', section: 'retweet-section' }, + 'like': { configKey: 'tweetId', actionType: 'like', info: 'like-info', progress: 'like-progress', section: 'like-section' }, + }; + // Fetch the tweetId from the configuration file const configResponse = await fetch('../contract-config.json'); if (!configResponse.ok) { throw new Error(`Failed to load configuration file. Status: ${configResponse.status}`); } const jsonConfig = await configResponse.json(); - let queryParams = ''; - let actionType; - // Determine which identifier to use based on the action - if (action === 'follow-us') { - actionType = 'follow-us'; - const userName = jsonConfig.userName; // Assuming userName is stored in the config - if (!userName) { - throw new Error("Required configuration value 'userName' is missing."); - } - queryParams += `userName=${encodeURIComponent(userName)}`; - } else if (action === 'retweet-2') { - actionType = 'retweet'; - const tweetId2 = jsonConfig.tweetId2; - if (!tweetId2) { - throw new Error("Required configuration value 'tweetId2' is missing."); - } - queryParams += `tweetId=${encodeURIComponent(tweetId2)}`; - } else { - actionType = action; - const tweetId = jsonConfig.tweetId; - if (!tweetId) { - throw new Error("Required configuration value 'tweetId' is missing."); - } - queryParams += `tweetId=${encodeURIComponent(tweetId)}`; + + const configKey = actionConfig[action].configKey; + const configValue = jsonConfig[configKey]; + if (!configValue) { + throw new Error(`Required configuration value '${configKey}' is missing.`); } + const queryParams = `${configKey}=${encodeURIComponent(configValue)}`; + const actionType = actionConfig[action].actionType; - // If tweetId is fetched successfully, perform the action const actionResponse = await fetch(`${authWebAddress}/${actionType}?${queryParams}`, { method: 'GET', headers: { @@ -225,64 +214,27 @@ async function handleAction(action) { const response = await actionResponse.json(); console.log(`${action} action response:`, response); - displayInfo(action, `${action} action performed successfully`, 'info'); - - // Set the corresponding class to be disabled after success - switch (action) { - case 'retweet': - if (response.error && response.code == 10017) { - console.log('You have already retweeted this tweet.'); - displayInfo(action, 'You have already retweeted this tweet.', 'error'); - } else { - throw new Error('Error retweeting the tweet:', response.error); - } - hideElement('retweet-info'); - animateProgress('retweet-progress'); - setTimeout(() => { - document.getElementById('retweet-section').classList.add('disabled'); - }, 2000); - break; - case 'like': - if (response.error && response.code == 10018) { - console.log('You have already liked this tweet.'); - displayInfo(action, 'You have already liked this tweet.', 'error'); - } else { - throw new Error('Error liking the tweet:', response.error); - } - hideElement('like-info'); - animateProgress('like-progress'); - setTimeout(() => { - document.getElementById('like-section').classList.add('disabled'); - }, 2000); - break; - case 'retweet-2': - if (response.error && response.code == 10017) { - console.log('You have already retweeted this tweet.'); - displayInfo(action, 'You have already retweeted this tweet.', 'error'); - } else { - throw new Error('Error retweeting the tweet:', response.error); - } - hideElement('retweet-2-info'); - animateProgress('retweet-2-progress'); - setTimeout(() => { - document.getElementById('retweet-section-2').classList.add('disabled'); - }, 2000); - break; - case 'follow-us': - // Note: Since Twitter disallows checking if a user is already followed, the error is thrown if found in the response - if (response.error) { - throw new Error('Error following the user:', response.error); - } - hideElement('follow-info'); - animateProgress('follow-progress'); - setTimeout(() => { - document.getElementById('follow-section').classList.add('disabled'); - }, 2000); - break; - default: - break; + + const infoElementId = actionConfig[action].info; + const progressElementId = actionConfig[action].progress; + const sectionElementId = actionConfig[action].section; + + if (response.error && (response.code == 10017 || response.code == 10018)) { + console.log(`You have already ${actionType} this tweet.`); + displayInfo(action, `You have already ${actionType} this tweet.`, 'error'); + } else if (!response.error && response.code == 0) { + console.log(`${actionType.charAt(0).toUpperCase() + actionType.slice(1)} successful.`); + displayInfo(action, `${actionType.charAt(0).toUpperCase() + actionType.slice(1)} successful.`, 'info'); + } else { + throw new Error(`Error ${actionType} the tweet:`, response.error); } - // Wait for 3 seconds + + hideElement(infoElementId); + animateProgress(progressElementId); + setTimeout(() => { + document.getElementById(sectionElementId).classList.add('disabled'); + }, 2000); + setTimeout(() => { checkAllActionsDisabled(); }, 3000); From 6685c7f1103102a717cdf5921842a0745ee2a424 Mon Sep 17 00:00:00 2001 From: Phillweston <2436559745@qq.com> Date: Thu, 16 May 2024 19:12:54 +0800 Subject: [PATCH 2/6] Add success message for promotion code input row --- index.html | 5 ++--- js/claim-airdrop.js | 4 ++-- js/contact-form.js | 17 ++++++++++++----- js/get-promotion-code.js | 14 ++++++++++---- 4 files changed, 26 insertions(+), 14 deletions(-) diff --git a/index.html b/index.html index 6dd4bc1..e5d3dcb 100644 --- a/index.html +++ b/index.html @@ -408,8 +408,7 @@
-
-
+
-
+
diff --git a/js/claim-airdrop.js b/js/claim-airdrop.js index 0f3d54b..dc63dfa 100644 --- a/js/claim-airdrop.js +++ b/js/claim-airdrop.js @@ -359,11 +359,11 @@ function displayMessage(message, type) { messageElement.classList.add('error-message'); } - // Clear the message after 10 seconds + // Clear the message after 30 seconds setTimeout(() => { messageElement.innerText = ''; // Clear the text messageElement.className = ''; // Also clear any class that was added - }, 10000); + }, 30000); } } diff --git a/js/contact-form.js b/js/contact-form.js index 95dc8f2..2254a38 100644 --- a/js/contact-form.js +++ b/js/contact-form.js @@ -88,21 +88,26 @@ document.getElementById('subscriptionForm').addEventListener('submit', function( event.preventDefault(); // Prevent the default form submission // Clear existing errors - document.getElementById('subscriptionEmailError').textContent = ''; - document.getElementById('subscriptionSuccess').textContent = ''; + const subscriptionInfo = document.getElementById('subscriptionInfo'); + subscriptionInfo.textContent = ''; + // Clear the classlist + subscriptionInfo.classList.remove('success-message', 'error-message'); var email = document.getElementById('subscriptionEmail').value; var emailRegex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/; if (email.trim() === '') { - document.getElementById('subscriptionEmailError').textContent = 'Please enter your email address.'; + subscriptionInfo.classList.add('error-message'); + subscriptionInfo.textContent = 'Please enter your email address.'; } else if (!emailRegex.test(email)) { - document.getElementById('subscriptionEmailError').textContent = 'Please enter a valid email address.'; + subscriptionInfo.classList.add('error-message'); + subscriptionInfo.textContent = 'Please enter a valid email address.'; } else { + subscriptionInfo.classList.add('success-message'); // If email is valid - document.getElementById('subscriptionSuccess').textContent = 'Email is valid! Proceeding with subscription.'; + subscriptionInfo.textContent = 'Email is valid! Proceeding with subscription.'; // Email sending for subscription sendSubscriptionEmail(email) .then(() => { @@ -113,6 +118,8 @@ document.getElementById('subscriptionForm').addEventListener('submit', function( }) .catch(error => { console.log('Failed to send email: ' + error); + subscriptionInfo.classList.add('error-message'); + subscriptionInfo.textContent = 'Failed to send email. Please try again.'; }); } }); diff --git a/js/get-promotion-code.js b/js/get-promotion-code.js index 6f7605e..88270df 100644 --- a/js/get-promotion-code.js +++ b/js/get-promotion-code.js @@ -2,14 +2,17 @@ function checkPromotionCode(event) { event.preventDefault(); + const promotionInputMessage = document.getElementById('promotionInputMessage'); + promotionCode.classList.remove('success-message', 'error-message'); + var promotionCode = document.getElementById('promotion-code-input').value; // Show error if the length of the promotionCode is not equal to 16 if (promotionCode && promotionCode.length !== 16) { - document.getElementById('promotion-input-error').innerText = 'Promotion code must be 16 characters long.'; - setTimeout(function() { - document.getElementById('promotion-input-error').innerText = ''; - }, 5000); + promotionInputMessage.classList.add('error-message'); + promotionInputMessage.innerText = 'Promotion code must be 16 characters long.'; } else { + promotionInputMessage.classList.add('success-message'); + promotionInputMessage.innerText = 'Promotion code has been saved, please claim your airdrop.'; // Smoothly scroll to the desired section if the promotion code is correct var desiredSection = document.getElementById('airdrop'); if (desiredSection) { @@ -18,6 +21,9 @@ function checkPromotionCode(event) { }); } } + setTimeout(function() { + promotionInputMessage.innerText = ''; + }, 5000); } // eslint-disable-next-line no-unused-vars From 3cacdf360c87a3579248d7b1b72a8e2e9a96b4dc Mon Sep 17 00:00:00 2001 From: Phillweston <2436559745@qq.com> Date: Thu, 16 May 2024 22:54:39 +0800 Subject: [PATCH 3/6] Add reward amount display function --- .vscode/settings.json | 5 +- index.html | 22 +++-- js/claim-airdrop.js | 214 +++++++++++++++++++++++------------------- 3 files changed, 135 insertions(+), 106 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 6f3a291..b30c3c0 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,3 +1,6 @@ { - "liveServer.settings.port": 5501 + "liveServer.settings.port": 5501, + "githubPullRequests.ignoredPullRequestBranches": [ + "master" + ] } \ No newline at end of file diff --git a/index.html b/index.html index e5d3dcb..9958492 100644 --- a/index.html +++ b/index.html @@ -98,9 +98,9 @@ @@ -122,7 +122,7 @@ @@ -144,7 +144,7 @@ @@ -166,8 +166,8 @@ @@ -193,8 +193,10 @@
By clicking "Accept and Continue", you agree to our terms and conditions and privacy policy. You will be prompted to connect your wallet via an external link. Ensure you're using a trusted and secure wallet service.
-