Skip to content

Commit

Permalink
cleaning the code
Browse files Browse the repository at this point in the history
  • Loading branch information
Marchand-Nicolas committed May 31, 2024
1 parent 2061244 commit 8177da2
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import styles from "../../styles/discount.module.css";
import SearchBar from "../UI/searchBar";
import Timer from "../UI/timer";

type FreeRegisterDiscountProps = {
type FreeRegisterPresentationProps = {
setSearchResult: (searchResult: SearchResult) => void;
setScreen: (screen: number) => void;
title: { desc: string; catch: string; descAfter?: string };
Expand All @@ -12,14 +12,9 @@ type FreeRegisterDiscountProps = {
expiry: number;
};

const FreeRegisterDiscount: FunctionComponent<FreeRegisterDiscountProps> = ({
setSearchResult,
setScreen,
title,
desc,
image,
expiry,
}) => {
const FreeRegisterPresentation: FunctionComponent<
FreeRegisterPresentationProps
> = ({ setSearchResult, setScreen, title, desc, image, expiry }) => {
function onSearch(searchResult: SearchResult) {
setSearchResult(searchResult);
setScreen(2);
Expand Down Expand Up @@ -49,4 +44,4 @@ const FreeRegisterDiscount: FunctionComponent<FreeRegisterDiscountProps> = ({
);
};

export default FreeRegisterDiscount;
export default FreeRegisterPresentation;
6 changes: 3 additions & 3 deletions components/discount/freeRenewalDiscount.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ import {
import { areArraysEqual } from "@/utils/arrayService";
import useNeedSubscription from "@/hooks/useNeedSubscription";

type FreeRenewalDiscountProps = {
type FreeRenewalCheckoutProps = {
groups: string[];
goBack: () => void;
duration: number;
Expand All @@ -61,7 +61,7 @@ type FreeRenewalDiscountProps = {
renewPrice: string;
};

const FreeRenewalDiscount: FunctionComponent<FreeRenewalDiscountProps> = ({
const FreeRenewalCheckout: FunctionComponent<FreeRenewalCheckoutProps> = ({
groups,
priceInEth,
renewPrice,
Expand Down Expand Up @@ -442,4 +442,4 @@ const FreeRenewalDiscount: FunctionComponent<FreeRenewalDiscountProps> = ({
);
};

export default FreeRenewalDiscount;
export default FreeRenewalCheckout;
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,17 @@ import homeStyles from "../../styles/Home.module.css";
import Timer from "../UI/timer";
import Button from "../UI/button";

type DiscountRenewalScreenProps = {
type FreeRenewalPresentationProps = {
title: { desc: string; catch: string };
desc: string;
image: string;
expiry: number;
setScreen: (screen: number) => void;
};

const DiscountRenewalScreen: FunctionComponent<DiscountRenewalScreenProps> = ({
title,
desc,
image,
expiry,
setScreen,
}) => {
const FreeRenewalPresentation: FunctionComponent<
FreeRenewalPresentationProps
> = ({ title, desc, image, expiry, setScreen }) => {
return (
<div className={homeStyles.wrapperScreen}>
<div className={styles.container}>
Expand All @@ -42,4 +38,4 @@ const DiscountRenewalScreen: FunctionComponent<DiscountRenewalScreenProps> = ({
);
};

export default DiscountRenewalScreen;
export default FreeRenewalPresentation;
21 changes: 11 additions & 10 deletions components/discount/registerFree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import ConnectButton from "../UI/connectButton";
import { getFreeDomain } from "@/utils/campaignService";
import TermCheckbox from "../domains/termCheckbox";

type RegisterFreeProps = {
type FreeRegisterCheckoutProps = {
domain: string;
duration: number;
customMessage: string;
Expand All @@ -32,7 +32,7 @@ type RegisterFreeProps = {
banner: string;
};

const RegisterFree: FunctionComponent<RegisterFreeProps> = ({
const FreeRegisterCheckout: FunctionComponent<FreeRegisterCheckoutProps> = ({
domain,
duration,
customMessage,
Expand Down Expand Up @@ -150,6 +150,13 @@ const RegisterFree: FunctionComponent<RegisterFreeProps> = ({
});
}, [coupon, domain, address]);

const handleRegister = () =>
execute().then(() =>
setDomainsMinting((prev) =>
new Map(prev).set(encodedDomain.toString(), true)
)
);

return (
<div className={styles.container}>
<div className={styles.card}>
Expand Down Expand Up @@ -187,13 +194,7 @@ const RegisterFree: FunctionComponent<RegisterFreeProps> = ({
/>
{address ? (
<Button
onClick={() =>
execute().then(() =>
setDomainsMinting((prev) =>
new Map(prev).set(encodedDomain.toString(), true)
)
)
}
onClick={handleRegister}
disabled={
(domainsMinting.get(encodedDomain) as boolean) ||
!account ||
Expand Down Expand Up @@ -226,4 +227,4 @@ const RegisterFree: FunctionComponent<RegisterFreeProps> = ({
);
};

export default RegisterFree;
export default FreeRegisterCheckout;
8 changes: 4 additions & 4 deletions pages/freeregistration.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import styles from "../styles/discount.module.css";
import DiscountEndScreen from "../components/discount/discountEndScreen";

import { freeRegistration } from "../utils/discounts/freeRegistration";
import FreeRegisterDiscount from "@/components/discount/freeRegisterDiscount";
import RegisterFree from "@/components/discount/registerFree";
import FreeRegisterPresentation from "@/components/discount/FreeRegisterPresentation";
import FreeRegisterCheckout from "@/components/discount/registerFree";

const FreeRegistration: NextPage = () => {
const [searchResult, setSearchResult] = useState<SearchResult | undefined>();
Expand All @@ -30,7 +30,7 @@ const FreeRegistration: NextPage = () => {
/>
) : null}
{screen === 1 ? (
<FreeRegisterDiscount
<FreeRegisterPresentation
title={freeRegistration.offer.title}
desc={freeRegistration.offer.desc}
image={freeRegistration.offer.image ?? freeRegistration.image}
Expand All @@ -41,7 +41,7 @@ const FreeRegistration: NextPage = () => {
) : null}
{screen === 2 ? (
<div className={styles.container}>
<RegisterFree
<FreeRegisterCheckout
domain={searchResult?.name ?? ""}
duration={freeRegistration.offer.duration}
customMessage={freeRegistration.offer.customMessage}
Expand Down
8 changes: 4 additions & 4 deletions pages/freerenewal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import homeStyles from "../styles/Home.module.css";
import styles from "../styles/search.module.css";
import { freeRenewalDiscount } from "../utils/discounts/freeRenewal";
import DiscountEndScreen from "../components/discount/discountEndScreen";
import DiscountRenewalScreen from "../components/discount/discountRenewalScreen";
import FreeRenewalDiscount from "../components/discount/freeRenewalDiscount";
import FreeRenewalPresentation from "@/components/discount/freeRenewalPresentation";
import FreeRenewalCheckout from "@/components/discount/freeRenewalDiscount";

const FreeRenewalPage: NextPage = () => {
const [screen, setScreen] = useState<number>(1);
Expand Down Expand Up @@ -33,7 +33,7 @@ const FreeRenewalPage: NextPage = () => {
) : null}

{screen === 1 ? (
<DiscountRenewalScreen
<FreeRenewalPresentation
title={freeRenewalDiscount.offer.title}
desc={freeRenewalDiscount.offer.desc}
image={freeRenewalDiscount.image}
Expand All @@ -43,7 +43,7 @@ const FreeRenewalPage: NextPage = () => {
) : null}
{screen === 2 ? (
<div className={styles.container}>
<FreeRenewalDiscount
<FreeRenewalCheckout
groups={[
process.env.NEXT_PUBLIC_MAILING_LIST_GROUP_AUTO_RENEWAL ?? "",
freeRenewalDiscount.discountMailGroupId,
Expand Down
28 changes: 28 additions & 0 deletions tests/utils/campaignService.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,32 @@ describe("getFreeDomain function", () => {
s: "0x1234567890123456789012345678901234567890",
});
});

it("should handle an error response from the server", async () => {
global.fetch.mockResolvedValueOnce({
status: 500,
json: () => {
throw new Error("Internal Server Error");
},
text: () => "Internal Server Error",
});
const domain = await getFreeDomain();
expect(domain).toEqual({
error: "Internal Server Error",
});
});

it("should handle Coupon code already used", async () => {
global.fetch.mockResolvedValueOnce({
status: 400,
json: () => {
throw new Error("Coupon code already used");
},
text: () => "Coupon code already used",
});
const domain = await getFreeDomain();
expect(domain).toEqual({
error: "Coupon code already used",
});
});
});

0 comments on commit 8177da2

Please sign in to comment.