Skip to content

Commit

Permalink
fix: coupon errors (#875)
Browse files Browse the repository at this point in the history
* fix: coupon errors

* improve call data loading message

* fix: reset error message when trying another ticket

* removing one setter
  • Loading branch information
Marchand-Nicolas authored Sep 12, 2024
1 parent 5eb1ee2 commit 3cf64b3
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 18 deletions.
30 changes: 13 additions & 17 deletions components/discount/freeRegisterCheckout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const FreeRegisterCheckout: FunctionComponent<FreeRegisterCheckoutProps> = ({
}) => {
const [targetAddress, setTargetAddress] = useState<string>("");
const [callData, setCallData] = useState<Call[]>([]);
const [loadingCallData, setLoadingCallData] = useState<boolean>(true);
const [salt, setSalt] = useState<string | undefined>();
const encodedDomain = utils
.encodeDomain(domain)
Expand All @@ -67,6 +68,7 @@ const FreeRegisterCheckout: FunctionComponent<FreeRegisterCheckoutProps> = ({
loadingDeploymentData,
refreshRewards,
invalidTx,
txError,
loadingTypedData,
} = usePaymaster(
callData,
Expand All @@ -76,7 +78,7 @@ const FreeRegisterCheckout: FunctionComponent<FreeRegisterCheckoutProps> = ({
);
if (transactionHash) setTransactionHash(transactionHash);
},
!coupon
loadingCallData
);

useEffect(() => {
Expand Down Expand Up @@ -107,7 +109,7 @@ const FreeRegisterCheckout: FunctionComponent<FreeRegisterCheckoutProps> = ({
}, [salt]);

useEffect(() => {
if (signature[0] === null) return;
if (!signature[0]) return;
// Variables
const newTokenId: number = Math.floor(Math.random() * 1000000000000);
setTokenId(newTokenId);
Expand All @@ -118,7 +120,8 @@ const FreeRegisterCheckout: FunctionComponent<FreeRegisterCheckoutProps> = ({
signature,
txMetadataHash
);
return setCallData(freeRegisterCalls);
setCallData(freeRegisterCalls);
setLoadingCallData(false);
}, [metadataHash, encodedDomain, signature]);

function changeCoupon(value: string): void {
Expand Down Expand Up @@ -146,17 +149,8 @@ const FreeRegisterCheckout: FunctionComponent<FreeRegisterCheckoutProps> = ({

useEffect(() => {
if (!coupon) return setLoadingCoupon(false);
const lastSuccessCoupon = localStorage.getItem("lastSuccessCoupon");
if (coupon === lastSuccessCoupon) {
setCouponError("");
setLoadingCoupon(false);
const signature = JSON.parse(
localStorage.getItem("couponSignature") as string
);
setSignature(signature);
return;
}
if (!address) return;
setLoadingCallData(true);
getFreeDomain(address, `${domain}.stark`, coupon).then((res) => {
if (res.error)
setCouponError(
Expand All @@ -166,9 +160,6 @@ const FreeRegisterCheckout: FunctionComponent<FreeRegisterCheckoutProps> = ({
const signature = [res.r, res.s];
setSignature(signature);
setCouponError("");
// Write in local storage
localStorage.setItem("lastSuccessCoupon", coupon);
localStorage.setItem("couponSignature", JSON.stringify(signature));
}
setLoadingCoupon(false);
});
Expand Down Expand Up @@ -207,6 +198,9 @@ const FreeRegisterCheckout: FunctionComponent<FreeRegisterCheckoutProps> = ({
checked={termsBox}
onChange={() => setTermsBox(!termsBox)}
/>
{invalidTx && txError?.message ? (
<p className={styles.errorMessage}>{txError.message}</p>
) : null}
{address ? (
<Button
onClick={handleRegister}
Expand All @@ -228,9 +222,11 @@ const FreeRegisterCheckout: FunctionComponent<FreeRegisterCheckoutProps> = ({
? "Please accept terms & policies"
: couponError || !coupon
? "Enter a valid Coupon"
: loadingCallData
? "Loading call data"
: loadingGas
? invalidTx
? "Invalid signature"
? txError?.short
: "Loading gas"
: loadingTypedData
? "Building typed data"
Expand Down
12 changes: 12 additions & 0 deletions hooks/paymaster.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ import isStarknetDeployed from "./isDeployed";
import { gaslessOptions } from "@/utils/constants";
import { decimalToHex } from "@/utils/feltService";

type ErrorMessage = {
message: string;
short: string;
};

const usePaymaster = (
callData: Call[],
then: (transactionHash: string) => void,
Expand All @@ -51,6 +56,7 @@ const usePaymaster = (
const { isDeployed, deploymentData } = isStarknetDeployed(account?.address);
const [deploymentTypedData, setDeploymentTypedData] = useState<TypedData>();
const [invalidTx, setInvalidTx] = useState<boolean>(false);
const [txError, setTxError] = useState<ErrorMessage>();

const argentWallet = useMemo(
() => connector?.id === "argentX" /*|| connector?.id === "argentMobile"*/,
Expand Down Expand Up @@ -105,6 +111,10 @@ const usePaymaster = (
)
.catch((e) => {
console.error(e);
const stringError = e.toString();
if (stringError.includes("Invalid signature"))
setTxError({ message: "", short: "Invalid signature" });
else setTxError({ message: stringError, short: "TX error" });
setInvalidTx(true);
});
},
Expand All @@ -123,6 +133,7 @@ const usePaymaster = (
if (!account || !gasTokenPrice || !gaslessCompatibility || loadingCallData)
return;
setLoadingGas(true);
setInvalidTx(false);
estimateCalls(account, callData).then((fees) => {
if (!fees) return;
setInvalidTx(false);
Expand Down Expand Up @@ -258,6 +269,7 @@ const usePaymaster = (
refreshRewards,
invalidTx,
loadingTypedData,
txError,
};
};

Expand Down
7 changes: 7 additions & 0 deletions styles/components/registerV2.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,13 @@
opacity: 0.7;
}

.errorMessage {
font-size: 12px;
color: rgb(210, 27, 27);
max-width: 100%;
word-break: break-word;
}

@media (max-width: 1084px) {
.image {
visibility: hidden;
Expand Down
4 changes: 3 additions & 1 deletion styles/components/textField.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
}

.radioWhite {
background-color: #FFFFFF;
background-color: #ffffff;
border-radius: 7.983px;
padding: 8px 15px;
box-shadow: 0px 2px 30px 0px rgba(0, 0, 0, 0.06);
Expand All @@ -30,6 +30,8 @@
font-weight: 500;
line-height: 20px; /* 142.857% */
align-self: flex-start;
max-width: 100%;
word-break: break-word;
}

.errorLegend {
Expand Down

0 comments on commit 3cf64b3

Please sign in to comment.