Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MEP 16/09/2024 #880

Merged
merged 13 commits into from
Sep 17, 2024
4 changes: 2 additions & 2 deletions components/UI/modalProfilePic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import ClickableAction from "./iconsComponents/clickableAction";
import theme from "../../styles/theme";
import DoneFilledIcon from "./iconsComponents/icons/doneFilledIcon";
import ArrowLeftIcon from "./iconsComponents/icons/arrowLeftIcon";
import { useContractWrite } from "@starknet-react/core";
import { useSendTransaction } from "@starknet-react/core";
import { Call } from "starknet";
import identityChangeCalls from "../../utils/callData/identityChangeCalls";
import { hexToDecimal, toUint256 } from "../../utils/feltService";
Expand All @@ -32,7 +32,7 @@ const ModalProfilePic: FunctionComponent<ModalProfilePicProps> = ({
}) => {
const [callData, setCallData] = useState<Call[]>([]);
const { addTransaction } = useNotificationManager();
const { writeAsync: execute, data: updateData } = useContractWrite({
const { sendAsync: execute, data: updateData } = useSendTransaction({
calls: callData,
});

Expand Down
41 changes: 20 additions & 21 deletions components/discount/freeRegisterCheckout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import usePaymaster from "@/hooks/paymaster";

type FreeRegisterCheckoutProps = {
domain: string;
duration: number;
durationInDays: number;
goBack: () => void;
couponCode?: boolean;
couponHelper?: string;
Expand All @@ -33,14 +33,15 @@ type FreeRegisterCheckoutProps = {

const FreeRegisterCheckout: FunctionComponent<FreeRegisterCheckoutProps> = ({
domain,
duration,
durationInDays,
goBack,
couponCode,
couponHelper,
banner,
}) => {
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 @@ -198,20 +189,26 @@ const FreeRegisterCheckout: FunctionComponent<FreeRegisterCheckoutProps> = ({
</div>
</div>
<div className={styles.summary}>
<FreeRegisterSummary duration={duration} domain={domain} />
<FreeRegisterSummary
durationInDays={durationInDays}
domain={domain}
/>
<Divider className="w-full" />
<TermCheckbox
checked={termsBox}
onChange={() => setTermsBox(!termsBox)}
/>
{invalidTx && txError?.message ? (
<p className={styles.errorMessage}>{txError.message}</p>
) : null}
{address ? (
<Button
onClick={handleRegister}
disabled={
(domainsMinting.get(encodedDomain) as boolean) ||
!account ||
!coupon ||
!duration ||
!durationInDays ||
!targetAddress ||
!termsBox ||
Boolean(couponError) ||
Expand All @@ -225,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
4 changes: 2 additions & 2 deletions components/discount/freeRegisterPresentation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type FreeRegisterPresentationProps = {
title: { desc: string; catch: string; descAfter?: string };
desc: string;
image: string;
expiry: number;
expiry?: number;
};

const FreeRegisterPresentation: FunctionComponent<
Expand Down Expand Up @@ -42,7 +42,7 @@ const FreeRegisterPresentation: FunctionComponent<
className={styles.illustrationVariant}
alt="Registration illustration"
/>
<Timer expiry={expiry} fixed />
{expiry && <Timer expiry={expiry} fixed />}
</div>
</div>
</div>
Expand Down
12 changes: 7 additions & 5 deletions components/discount/freeRegisterSummary.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import React, { FunctionComponent } from "react";
import styles from "../../styles/components/registerV3.module.css";
import { getYearlyPrice } from "@/utils/priceService";
import { getDisplayablePrice, getYearlyPriceWei } from "@/utils/priceService";
import DoneIcon from "../UI/iconsComponents/icons/doneIcon";
import { useAccount } from "@starknet-react/core";

type FreeRegisterSummaryProps = {
duration: number;
durationInDays: number;
domain: string;
};

const FreeRegisterSummary: FunctionComponent<FreeRegisterSummaryProps> = ({
domain,
duration,
durationInDays,
}) => {
const { address } = useAccount();

function getMessage() {
return `${Math.floor(duration / 30)} months of domain registration`;
return `${Math.floor(durationInDays / 30)} months of domain registration`;
}

return (
Expand All @@ -26,7 +26,9 @@ const FreeRegisterSummary: FunctionComponent<FreeRegisterSummaryProps> = ({
<div className={styles.priceContainer}>
<p className={styles.legend}>{getMessage()}</p>
<p>
<span className="line-through">{getYearlyPrice(domain)} ETH</span>
<span className="line-through">
{getDisplayablePrice(getYearlyPriceWei(domain))} ETH
</span>
&nbsp;&nbsp;
<strong>Free</strong>
</p>
Expand Down
Loading