Skip to content

Commit

Permalink
Refactor the code using map instead of multiple if statement
Browse files Browse the repository at this point in the history
  • Loading branch information
Phillweston committed May 16, 2024
1 parent 3cacdf3 commit a742249
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 110 deletions.
108 changes: 38 additions & 70 deletions js/claim-airdrop.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Fireworks } from 'fireworks-js';
// 1. Get a project ID at https://cloud.walletconnect.com
let projectId, activeNetwork, contractAddress, authWebAddress, turnstileSiteKey;
let tweetId, tweetId2, userName;
let checkRetweetEnabled, checkRetweet2Enabled, checkLikeEnabled;
let checkActionsEnabled = {};

// Updated contract ABI to include getAirdropAmount function
const airdropAcquireABI = [
Expand Down Expand Up @@ -64,9 +64,11 @@ try {
userName = jsonConfig.userName;

// Access additional properties for Twitter checks and actions
checkRetweetEnabled = jsonConfig.checkRetweetEnabled;
checkRetweet2Enabled = jsonConfig.checkRetweet2Enabled;
checkLikeEnabled = jsonConfig.checkLikeEnabled;
checkActionsEnabled = {
checkRetweet: jsonConfig.checkRetweetEnabled,
checkRetweet2: jsonConfig.checkRetweet2Enabled,
checkLike: jsonConfig.checkLikeEnabled
};

// Additional validation can be performed here as needed
if (!activeNetwork || !contractAddress || !authWebAddress || !turnstileSiteKey || !projectId) {
Expand All @@ -77,8 +79,8 @@ try {
throw new Error("Required configuration values (tweetId or tweetId2 or userName) are missing.");
}

if (!checkRetweetEnabled || !checkRetweet2Enabled || !checkLikeEnabled) {
throw new Error("Required configuration values (checkRetweetEnabled or checkRetweet2Enabled or checkLikeEnabled) are missing.");
if (Object.values(checkActionsEnabled).includes(false)) {
throw new Error("Required configuration values (checkRetweetEnabled, checkRetweet2Enabled, or checkLikeEnabled) are missing.");
}
} catch (error) {
// Check if the error is due to missing file
Expand All @@ -101,14 +103,17 @@ const metadata = {

let chains;

if (activeNetwork === 'baseMainnet') {
chains = [base];
} else if (activeNetwork === 'baseSepolia') {
chains = [baseSepolia];
} else if (activeNetwork === 'sepolia') {
chains = [sepolia];
} else if (activeNetwork === 'ganache') {
chains = [ganacheTestChain];
const networkChainMap = {
// Network name: [chain_config]
baseMainnet: [base],
baseSepolia: [baseSepolia],
sepolia: [sepolia],
ganache: [ganacheTestChain]
};

// Check if the active network is in the networkChainMap
if (Object.prototype.hasOwnProperty.call(networkChainMap, activeNetwork)) {
chains = networkChainMap[activeNetwork];
} else {
console.log('Invalid network selection');
process.exit(1);
Expand Down Expand Up @@ -666,44 +671,25 @@ async function checkIfPurchased(address) {
async function checkTwitterInteractions(tweetId, tweetId2) {
try {
let step_cnt = 0;
if (checkRetweet2Enabled === "true") {
const isRetweeted2 = await checkRetweet(tweetId2);
console.log('Retweet check:', isRetweeted2);
if (!isRetweeted2) {
console.log('Tweet2 has not been retweeted by the user.');
} else {
step_cnt++;
}
}

if (checkLikeEnabled === "true") {
const isLiked = await checkLike(tweetId);
console.log('Like check:', isLiked);
if (!isLiked) {
console.log('Tweet is not liked by the user.');
} else {
step_cnt++;
}
}

if (checkRetweetEnabled === "true") {
const isRetweeted = await checkRetweet(tweetId);
console.log('Retweet check:', isRetweeted);
if (!isRetweeted) {
console.log('Tweet has not been retweeted by the user.');
} else {
step_cnt++;
const actionsMap = {
checkRetweet2: { action: 'retweet', responseKey: 'isRetweeted', tweetId: tweetId2, message: 'Tweet2 has not been retweeted by the user.' },
checkLike: { action: 'like', responseKey: 'isLiked', tweetId: tweetId, message: 'Tweet is not liked by the user.' },
checkRetweet: { action: 'retweet', responseKey: 'isRetweeted', tweetId: tweetId, message: 'Tweet has not been retweeted by the user.' },
//checkBookmark: { action: 'bookmark', responseKey: 'isBookmarked', tweetId: tweetId, message: 'Tweet has not been bookmarked by the user.' }
};

for (let action in actionsMap) {
if (checkActionsEnabled[action] === "true") {
const isActionDone = await checkAction(actionsMap[action].action, actionsMap[action].tweetId, actionsMap[action].responseKey);
console.log(`${action} check:`, isActionDone);
if (!isActionDone) {
console.log(actionsMap[action].message);
} else {
step_cnt++;
}
}
}

// const isBookmarked = await checkBookmark(tweetId);
// console.log('Bookmark check:', isBookmarked);
// if (!isBookmarked) {
// console.log('Tweet has not been bookmarked by the user.');
// } else {
// step_cnt++;
// }

console.log('Checks passed steps: ', step_cnt);
if (step_cnt === 0) {
return { success: false, step: step_cnt, message: 'No checks passed' };
Expand All @@ -716,28 +702,10 @@ async function checkTwitterInteractions(tweetId, tweetId2) {
}
}

// function checkFollow(targetUserName) {
// return fetch(`${authWebAddress}/check-follow?userName=${targetUserName}`, { credentials: 'include' })
// .then(handleResponse)
// .then(response => response.data.isFollowing);
// }

// function checkBookmark(tweetId) {
// return fetch(`${authWebAddress}/check-bookmark?tweetId=${tweetId}`, { credentials: 'include' })
// .then(handleResponse)
// .then(response => response.data.isBookmarked);
// }

function checkLike(tweetId) {
return fetch(`${authWebAddress}/check-like?tweetId=${tweetId}`, { credentials: 'include' })
.then(handleResponse)
.then(response => response.data.isLiked);
}

function checkRetweet(tweetId) {
return fetch(`${authWebAddress}/check-retweet?tweetId=${tweetId}`, { credentials: 'include' })
function checkAction(action, tweetId, responseKey) {
return fetch(`${authWebAddress}/check-${action}?tweetId=${tweetId}`, { credentials: 'include' })
.then(handleResponse)
.then(response => response.data.isRetweeted);
.then(response => response.data[responseKey]);
}

function handleResponse(response) {
Expand Down
86 changes: 46 additions & 40 deletions js/twitter-action.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
let authWebAddress = null;
let retweetEnabled, retweet2Enabled, likeEnabled, followEnabled;
let actionsEnabled = {};

window.addEventListener('message', function(event) {
// Process the message data
Expand Down Expand Up @@ -44,17 +44,22 @@ document.addEventListener('DOMContentLoaded', function () {
authWebAddress = jsonConfig.authWebAddress;

// Access additional properties if needed
retweetEnabled = jsonConfig.retweetEnabled;
retweet2Enabled = jsonConfig.retweet2Enabled;
likeEnabled = jsonConfig.likeEnabled;
followEnabled = jsonConfig.followEnabled;
actionsEnabled = {
retweet: jsonConfig.retweetEnabled,
retweet2: jsonConfig.retweet2Enabled,
like: jsonConfig.likeEnabled,
follow: jsonConfig.followEnabled
};

// Additional validation can be performed here as needed
if (!authWebAddress) {
throw new Error("Required configuration values (authWebAddress) are missing.");
}

if (!retweetEnabled || !retweet2Enabled || !likeEnabled || !followEnabled) {
// Check if any action is not enabled
const isAnyActionDisabled = Object.entries(actionsEnabled).some(isEnabled => !isEnabled);

if (isAnyActionDisabled) {
throw new Error("Required configuration values (retweetEnabled or retweet2Enabled or likeEnabled or followEnabled) are missing.");
}

Expand Down Expand Up @@ -107,19 +112,19 @@ async function checkAuthStatus() {
console.log('Received authentication status:', result);

if (result.data.isAuthenticated) {
// Map of enabled flags to their corresponding element IDs
const actionElementMap = {
retweet: 'retweet-section',
retweet2: 'retweet-section-2',
like: 'like-section',
follow: 'follow-section'
};
// Remove the disabled class from the action buttons
if (retweetEnabled === "true") {
document.getElementById('retweet-section').classList.remove('disabled');
}
if (retweet2Enabled === "true") {
document.getElementById('retweet-section-2').classList.remove('disabled');
}
if (likeEnabled === "true") {
document.getElementById('like-section').classList.remove('disabled');
}
if (followEnabled === "true") {
document.getElementById('follow-section').classList.remove('disabled');
}
Object.entries(actionsEnabled).forEach(([action, isEnabled]) => {
if (isEnabled === "true") {
document.getElementById(actionElementMap[action]).classList.remove('disabled');
}
});
console.log('View unlocked successfully.');

// Hide the `twitterAuth` container
Expand All @@ -135,19 +140,16 @@ async function checkAuthStatus() {
}

function displayInfo(action, message, type) {
let elementId;

if (action === 'authentication') {
elementId = 'twitterAuthMessage';
} else if (action === 'retweet') {
elementId = 'repostMessage';
} else if (action === 'like') {
elementId = 'likeMessage';
} else if (action === 'retweet-2') {
elementId = 'repostMessage2';
} else if (action === 'follow-us') {
elementId = 'followMessage';
}
// Map the action to the corresponding element ID
const actionElementMap = {
'authentication': 'twitterAuthMessage',
'retweet': 'repostMessage',
'like': 'likeMessage',
'retweet-2': 'repostMessage2',
'follow-us': 'followMessage'
};

let elementId = actionElementMap[action];

// Set the message to the appropriate element and add relevant class
if (elementId) {
Expand Down Expand Up @@ -245,16 +247,20 @@ async function handleAction(action) {
}

function checkAllActionsDisabled() {
const isRetweetDisabled = document.getElementById('retweet-section').classList.contains('disabled');
const isLikeDisabled = document.getElementById('like-section').classList.contains('disabled');
const isRetweet2Disabled = document.getElementById('retweet-section-2').classList.contains('disabled');
const isFollowUsDisabled = document.getElementById('follow-section').classList.contains('disabled');
if (isRetweetDisabled && isLikeDisabled && isRetweet2Disabled && isFollowUsDisabled) {
const actionElementMap = {
retweet: 'retweet-section',
like: 'like-section',
retweet2: 'retweet-section-2',
follow: 'follow-section'
};

const areAllActionsDisabled = Object.values(actionElementMap).every(id => document.getElementById(id).classList.contains('disabled'));

if (areAllActionsDisabled) {
Object.values(actionElementMap).forEach(id => {
document.getElementById(id).style.display = 'none';
});
document.getElementById('promotionCodeInput').style.display = 'block';
document.getElementById('retweet-section').style.display = 'none';
document.getElementById('like-section').style.display = 'none';
document.getElementById('retweet-section-2').style.display = 'none';
document.getElementById('follow-section').style.display = 'none';
}
}

Expand Down

0 comments on commit a742249

Please sign in to comment.