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

Add lucky wheel #13

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/portal/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"react-infinite-scroll-component": "^6.1.0",
"react-oauth2-code-pkce": "^1.20.1",
"react-top-loading-bar": "^2.3.1",
"react-wheel-of-prizes": "1.0.9",
"tailwind-merge": "^2.3.0",
"tailwindcss-animate": "^1.0.7"
},
Expand Down
11 changes: 2 additions & 9 deletions packages/portal/src/app/portal/airdrop/airdrop-waterfall.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { useRedirectLogin } from "@/containers/authentication/hooks/use-redirect
import { useRequestAirdrop } from "@/app/portal/airdrop/hooks/use-request-airdrop";
import { useToast } from "@/components/ui/use-toast";
import { EmailVerifyOtpDialog } from "@/app/portal/airdrop/email-verify-otp-dialog";
import { DailyCheckInDialog } from "@/app/portal/airdrop/daily-check-in-dialog";
import {
AirdropType,
selectHistoryItems,
Expand Down Expand Up @@ -180,15 +181,7 @@ export const AirdropWaterfall = () => {
<p>0 ~ 5 $CAI Randomly </p>
</CardContent>
<CardFooter>
<Button
onClick={() => onClaim(AirdropType.DAILY_CLAIM)}
className={getDoneBtnStyle(dailyClaim)}
variant="secondary"
isLoading={isLoading}
disabled={dailyClaim}
>
{dailyClaim ? "Claimed" : "Claim"}
</Button>
<DailyCheckInDialog done={dailyClaim} isLoading={isLoading} />
</CardFooter>
</Card>

Expand Down
170 changes: 170 additions & 0 deletions packages/portal/src/app/portal/airdrop/daily-check-in-dialog.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
import { Button } from "@/components/ui/button";
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
DialogTrigger,
} from "@/components/ui/dialog";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { cn } from "@/lib/utils";
import { useUpdateAccount } from "@/app/portal/airdrop/hooks/use-update-account";
import { useRef, useState } from "react";
import serialize from "form-serialize";
import { useToast } from "@/components/ui/use-toast";
import { useRequestAirdrop } from "@/app/portal/airdrop/hooks/use-request-airdrop";
import { useUser } from "@/containers/authentication/hooks/use-user";
import { useIsLoggedIn } from "@/containers/authentication/hooks/use-is-logged-in";
import { useRedirectLogin } from "@/containers/authentication/hooks/use-redirect-login";
import { useOnClaim } from "@/app/portal/airdrop/hooks/use-on-claim";
import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert";
import { AlertCircle } from "lucide-react";
import { AirdropType } from "@/app/portal/airdrop/selectors/select-history-items";
import WheelComponent from "react-wheel-of-prizes";

const doneButtonStyle = (done: boolean) =>
cn("w-full", done && "bg-green-600 text-white hover:bg-green-800");

export function DailyCheckInDialog({
done,
isLoading: parentIsLoading,
}: {
isLoading: boolean;
done: boolean;
}) {
const formRef = useRef<HTMLFormElement>(null);
const { dataUser, loadingUser } = useUser();
const { linkAccountLoading, updateAccount } = useUpdateAccount();
const { requestAirdrop, requestAirdropLoading } = useRequestAirdrop();
const { toast } = useToast();
const [otpSent, setOtpSent] = useState(false);
const [error, setError] = useState<string>("");
const isLoading =
parentIsLoading ||
linkAccountLoading ||
requestAirdropLoading ||
linkAccountLoading;
const { isLoggedIn, isLoggedInLoading } = useIsLoggedIn();
const redirectLogin = useRedirectLogin();
const onClaim = useOnClaim(isLoggedIn, redirectLogin, requestAirdrop, toast);

const segments = [
"1 $CAI",
"2 $CAI",
"3 $CAI",
"4 $CAI",
"5 $CAI",
];
const segColors = ["black", "#60BA97", "black", "#60BA97", "black"];
const onWheelFinished = (winner: string) => {
const match = winner.match(/^(\d+)/);
const amount: number = match ? parseInt(match[1], 10) : NaN;
onClaim(AirdropType.DAILY_CLAIM, amount)
};
return (
<Dialog>
<DialogTrigger asChild>
<Button
variant="secondary"
className={doneButtonStyle(done)}
disabled={done || isLoading}
isLoading={isLoading}
>
{done ? "Claimed" : "Claim"}
</Button>
</DialogTrigger>
<DialogContent className="sm:max-w-[425px]">
<DialogHeader>
<DialogTitle>Spin the Wheel for Your Daily Drop!</DialogTitle>
<DialogDescription>
Spin now to reveal today's exclusive offer and snag amazing deals! Try your luck and see what you win!
</DialogDescription>
</DialogHeader>

<div className="flex justify-center items-center w-full">
<div className="wheel-container">
<WheelComponent
segments={segments}
segColors={segColors}
onFinished={(winner: string) => onWheelFinished(winner)}
primaryColor="black"
contrastColor="white"
buttonText="Spin"
isOnlyOnce={true}
size={200} // Adjust this size to fit your dialog
upDuration={200}
downDuration={800}
fontFamily="Arial"
/>
</div>
</div>

<DialogFooter>
<Button
isLoading={isLoading}
disabled={!otpSent || requestAirdropLoading}
onClick={async (e) => {
e.preventDefault();

try {
const formObj = serialize(formRef.current!, { hash: true });
const resp = await updateAccount({
variables: {
data: {
email: formObj.email,
emailOtp: formObj.emailOtp,
type: "EMAIL_VERIFY_OTP",
},
},
});

if (!resp.data?.updateAccount.ok) {
setError("Incorrect code");
return;
}

setError("");

const requestAirdropData = await requestAirdrop({
variables: {
data: {
type: AirdropType.ADD_EMAIL,
},
},
});

if (requestAirdropData.data?.requestAirdrop) {
toast({
title: "Congratulations!",
description: "You have claimed the airdrop",
});

window.location.reload();
} else if (dataUser?.user.email) {
// updating email
window.location.reload();
} else {
setError("Cannot claim");
toast({
variant: "destructive",
title: "Failed to claim",
description: "Criteria unmet",
});
}
} catch (err: any) {
console.error("failed to verify email OTP", err);
setError(err?.graphQLErrors?.at(0).message || "Unknown error");
}
}}
type="submit"
>
Claim
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
);
}
45 changes: 42 additions & 3 deletions packages/portal/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,13 @@
dependencies:
regenerator-runtime "^0.14.0"

"@babel/runtime@^7.15.4":
version "7.25.4"
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.25.4.tgz#6ef37d678428306e7d75f054d5b1bdb8cf8aa8ee"
integrity sha512-DSgLeL/FNcpXuzav5wfYvHCGvynXkJbn3Zvc3823AEe9nPwW9IK4UoCSS5yGymmQzN0pCPvivtgS6/8U2kkm1w==
dependencies:
regenerator-runtime "^0.14.0"

"@babel/runtime@^7.24.8":
version "7.25.0"
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.25.0.tgz#3af9a91c1b739c569d5d80cc917280919c544ecb"
Expand Down Expand Up @@ -9219,6 +9226,13 @@ react-top-loading-bar@^2.3.1:
resolved "https://registry.yarnpkg.com/react-top-loading-bar/-/react-top-loading-bar-2.3.1.tgz#d727eb6aaa412eae52a990e5de9f33e9136ac714"
integrity sha512-rQk2Nm+TOBrM1C4E3e6KwT65iXyRSgBHjCkr2FNja1S51WaPulRA5nKj/xazuQ3x89wDDdGsrqkqy0RBIfd0xg==

[email protected]:
version "1.0.9"
resolved "https://registry.yarnpkg.com/react-wheel-of-prizes/-/react-wheel-of-prizes-1.0.9.tgz#3458b68668f5ef1e732949d28608a0af62e9e415"
integrity sha512-aqRfC8+RZwRPp3fJMmcefxLpRfV3dEfaLZP0NQhwy7vXpf0zuaxesZ+Ut5yFkWyzTuxI3b/qKVbtBB1Yy6OePw==
dependencies:
"@babel/runtime" "^7.15.4"

react@^18:
version "18.3.1"
resolved "https://registry.yarnpkg.com/react/-/react-18.3.1.tgz#49ab892009c53933625bd16b2533fc754cab2891"
Expand Down Expand Up @@ -9758,7 +9772,16 @@ string-env-interpolation@^1.0.1:
resolved "https://registry.yarnpkg.com/string-env-interpolation/-/string-env-interpolation-1.0.1.tgz#ad4397ae4ac53fe6c91d1402ad6f6a52862c7152"
integrity sha512-78lwMoCcn0nNu8LszbP1UA7g55OeE4v7rCeWnM5B453rnNr4aq+5it3FEYtZrSEiMvHZOZ9Jlqb0OD0M2VInqg==

"string-width-cjs@npm:string-width@^4.2.0", string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.3:
"string-width-cjs@npm:string-width@^4.2.0":
version "4.2.3"
resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010"
integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==
dependencies:
emoji-regex "^8.0.0"
is-fullwidth-code-point "^3.0.0"
strip-ansi "^6.0.1"

string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.3:
version "4.2.3"
resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010"
integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==
Expand Down Expand Up @@ -9836,7 +9859,14 @@ string_decoder@~1.1.1:
dependencies:
safe-buffer "~5.1.0"

"strip-ansi-cjs@npm:strip-ansi@^6.0.1", strip-ansi@^6.0.0, strip-ansi@^6.0.1:
"strip-ansi-cjs@npm:strip-ansi@^6.0.1":
version "6.0.1"
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9"
integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==
dependencies:
ansi-regex "^5.0.1"

strip-ansi@^6.0.0, strip-ansi@^6.0.1:
version "6.0.1"
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9"
integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==
Expand Down Expand Up @@ -10901,7 +10931,7 @@ word-wrap@^1.2.5:
resolved "https://registry.yarnpkg.com/word-wrap/-/word-wrap-1.2.5.tgz#d2c45c6dd4fbce621a66f136cbe328afd0410b34"
integrity sha512-BN22B5eaMMI9UMtjrGd5g5eCYPpCPDUy0FJXbYsaT5zYxjFOckS53SQDE3pWkVoWpHXVb3BrYcEN4Twa55B5cA==

"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0", wrap-ansi@^7.0.0:
"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0":
version "7.0.0"
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43"
integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==
Expand All @@ -10919,6 +10949,15 @@ wrap-ansi@^6.0.1, wrap-ansi@^6.2.0:
string-width "^4.1.0"
strip-ansi "^6.0.0"

wrap-ansi@^7.0.0:
version "7.0.0"
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43"
integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==
dependencies:
ansi-styles "^4.0.0"
string-width "^4.1.0"
strip-ansi "^6.0.0"

wrap-ansi@^8.1.0:
version "8.1.0"
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-8.1.0.tgz#56dc22368ee570face1b49819975d9b9a5ead214"
Expand Down