From 8cf27ea46a9e1df2e1c269c9e5197d5b5501181f Mon Sep 17 00:00:00 2001 From: parikshitbarua Date: Sat, 31 Aug 2024 22:45:39 +0530 Subject: [PATCH 1/2] =?UTF-8?q?=F0=9F=92=9Aimprove:=20code=20refactoring?= =?UTF-8?q?=20and=20tips=20validation.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- web-app/src/components/Tip.svelte | 180 +++++++----------- .../posts-page/CommentsOfAPublication.svelte | 10 + .../components/posts-page/Publication.svelte | 11 +- .../profile-page/ProfileInfo.svelte | 9 + web-app/src/utils/tips/send.tip.util.ts | 1 - 5 files changed, 98 insertions(+), 113 deletions(-) diff --git a/web-app/src/components/Tip.svelte b/web-app/src/components/Tip.svelte index 928e7c5d..015576fd 100644 --- a/web-app/src/components/Tip.svelte +++ b/web-app/src/components/Tip.svelte @@ -41,24 +41,25 @@ }); const checkAmountIsValid = () => { - if (userEnteredAmount.length === 0) { - isAmountInvalid = false; - // urlInvalidReason = "URL cannot be empty"; + if ( + userEnteredAmount.length === 0 || + userEnteredAmount.trim().length === 0 + ) { + isAmountInvalid = true; + AmountInvalidReason = ""; return; } - const indexOfSpace = userEnteredAmount.indexOf(" "); - if (indexOfSpace !== -1) { + if (Number(userEnteredAmount) <= 0.000001) { isAmountInvalid = true; - AmountInvalidReason = "Amount is not specified properly"; + AmountInvalidReason = "Minimum Tip Amount is 0.000001"; return; } - if (Number(userEnteredAmount) <= 0.000000000000000001) { - console.log("Entered amount", userEnteredAmount); + const indexOfSpace = userEnteredAmount.trim().indexOf(" "); + if (isNaN(Number(userEnteredAmount)) || indexOfSpace > -1) { isAmountInvalid = true; - AmountInvalidReason = "Amount cannot be negative or too small"; - console.log(AmountInvalidReason); + AmountInvalidReason = "Please enter a valid amount."; return; } @@ -71,20 +72,17 @@ }; const sendTip = async () => { + userEnteredAmount = userEnteredAmount.trim(); isSendingTip = true; let tipDetails; if (selectedToken == tokenSymbol.BONSAI) { - console.log("userEnteredAmount", userEnteredAmount); - console.log("bonsaiBalance", bonsaiBalance); if (userEnteredAmount > bonsaiBalance) { - addNotification({ - position: "top-right", - heading: "Insufficient Balance", - description: - "You do not have the required balance. Please buy some tokens before sending a tip", - type: wallet, - removeAfter: 4000 - }); + addNotificationEvent( + "Insufficient Balance", + "You do not have the required balance. Please buy some tokens before sending a tip", + wallet, + 4000 + ); setTimeout(async () => { await web3Modal.open(); }, 2000); @@ -98,37 +96,14 @@ toAddress, userEnteredAmount ); - if (tipDetails.success) { - userEnteredAmount = ""; - isSendingTip = false; - tippingSuccess = true; - setTimeout(() => { - tippingSuccess = false; - dialog.close(); - }, 5000); - return; - } else { - dialog.close(); - addNotification({ - position: "top-right", - heading: "Error while sending tip", - description: "Failed to send tip, please try again", - type: cross, - removeAfter: 4000 - }); - isSendingTip = false; - return; - } } else if (selectedToken == tokenSymbol.MATIC) { if (userEnteredAmount > maticBalance) { - addNotification({ - position: "top-right", - heading: "Insufficient Balance", - description: - "You do not have the required balance. Please buy some tokens before sending a tip", - type: wallet, - removeAfter: 4000 - }); + addNotificationEvent( + "Insufficient Balance", + "You do not have the required balance. Please buy some tokens before sending a tip", + wallet, + 4000 + ); setTimeout(async () => { await web3Modal.open(); }, 2000); @@ -138,30 +113,39 @@ return; } tipDetails = await sendTipUtilMatic(toAddress, userEnteredAmount); - if (tipDetails.success) { - userEnteredAmount = ""; - isSendingTip = false; - tippingSuccess = true; - setTimeout(() => { - tippingSuccess = false; - dialog.close(); - }, 5000); - return; - } else { + } + if (tipDetails.success && !tipDetails.error) { + userEnteredAmount = ""; + isSendingTip = false; + tippingSuccess = true; + setTimeout(() => { + tippingSuccess = false; dialog.close(); - addNotification({ - position: "top-right", - heading: "Error while sending tip", - description: "Failed to send tip, please try again", - type: cross, - removeAfter: 4000 - }); - isSendingTip = false; - return; - } + }, 10000); + return; + } else { + dialog.close(); + addNotificationEvent( + "Error while sending tip", + "Failed to send tip, please try again", + cross, + 4000 + ); + isSendingTip = false; + return; } }; + const addNotificationEvent = (heading, description, type, removeAfter) => { + addNotification({ + position: "top-right", + heading, + description, + type, + removeAfter + }); + }; + const setMaticBalance = (balance) => { maticBalance = balance; return Math.round(maticBalance * 100) / 100; @@ -196,30 +180,28 @@
-

Congratulations!! 🥳

+

+ Your tip was successfully sent to + @{toHandle}. +

+
+ 🎉 +

- Your tip {userEnteredAmount} - was successfully sent to - {toHandle}. Your support means a lot and helps our viewers keep doing - what they love. We really appreciate it! + Your contributions help our Viewers to keep doing what they love + the most!