Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
alimaktabi committed Sep 1, 2024
2 parents b397602 + 7469184 commit 956b191
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 71 deletions.
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ NEXT_PUBLIC_IS_STAGE=0
SOLANA_HTTP_PROVIDER="https://fragrant-intensive-patina.solana-mainnet.quiknode.pro/08a6330aa4eb2bbad964ee86b9a271fdb5e8a7ad/"

NEXT_PUBLIC_CLOUDFLARE_TURNSITE_SITEKEY="0x4AAAAAAAiB14Mx3cWX5zwu"
H_CAPTCHA_SITEKEY="68c27f52-99c6-4832-b2b0-b5ac343a9f90"
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ import { appInfos } from "@/app/incentive-center/constants/integrations";
import {
checkConnections,
loadingAnimationRequirementsOption,
renderLinkValue,
requirementsConnections,
requirementWithoutApps,
useRequirementLinkGenerator,
} from "@/components/containers/token-tap/Modals/TokenRequirementModal";
import { ClaimAndEnrollButton } from "@/components/ui/Button/button";
Expand All @@ -13,17 +11,14 @@ import { usePrizeTapContext } from "@/context/prizeTapProvider";
import { useUserProfileContext } from "@/context/userProfile";
import { Permission, PermissionInfoProp, Prize, UserConnection } from "@/types";
import { replacePlaceholders } from "@/utils";
import {
getRaffleConstraintsVerifications,
getTokenConstraintsVerifications,
} from "@/utils/api";
import { getAllConnections } from "@/utils/serverApis";
import Image from "next/image";
import Link from "next/link";
import Script from "next/script";
import { FC, Fragment, useEffect, useMemo, useState } from "react";
import Lottie from "react-lottie";
import ReactMarkdown from "react-markdown";
import HCaptcha from "@hcaptcha/react-hcaptcha";

const Sidebar: FC<{
prize: Prize;
Expand Down Expand Up @@ -235,48 +230,49 @@ const PrizeRequirementBody: FC<{
const isCloudflareCaptcha =
constraint?.name === "core.HasVerifiedCloudflareCaptcha";

windowObj.onloadTurnstileCallback = function () {
isMounted = true;
if (!isCloudflareCaptcha || constraint?.isVerified) return;
if (isCloudflareCaptcha) {
windowObj.onloadTurnstileCallback = function () {
isMounted = true;
if (!isCloudflareCaptcha || constraint?.isVerified) return;

windowObj.turnstile.render("#captcha-cloudflare-container", {
sitekey: process.env.NEXT_PUBLIC_CLOUDFLARE_TURNSITE_SITEKEY,
callback: function (token: string) {
localStorage.setItem("captcha-token", token);
},
});
};
windowObj.turnstile.render("#captcha-cloudflare-container", {
sitekey: process.env.NEXT_PUBLIC_CLOUDFLARE_TURNSITE_SITEKEY,
callback: function (token: string) {
localStorage.setItem("captcha-token", token);
},
});
};

const handleLoadTurnstile = () => {
if (!isCloudflareCaptcha) return;
const handleLoadTurnstile = () => {
if (!isCloudflareCaptcha) return;

if (!windowObj.turnstile) {
setTimeout(handleLoadTurnstile, 300);
return;
}
if (!windowObj.turnstile) {
setTimeout(handleLoadTurnstile, 300);
return;
}

const captchaElement = document.querySelector(
"#captcha-cloudflare-container",
);
const captchaElement = document.querySelector(
"#captcha-cloudflare-container",
);

if (!captchaElement) return;
if (!captchaElement) return;

captchaElement.innerHTML = "";

windowObj.turnstile.render("#captcha-cloudflare-container", {
sitekey: process.env.NEXT_PUBLIC_CLOUDFLARE_TURNSITE_SITEKEY,
callback: function (token: string) {
localStorage.setItem("captcha-token", token);
},
});
};
captchaElement.innerHTML = "";

if (isCloudflareCaptcha && !constraint.isVerified) handleLoadTurnstile();
windowObj.turnstile.render("#captcha-cloudflare-container", {
sitekey: process.env.NEXT_PUBLIC_CLOUDFLARE_TURNSITE_SITEKEY,
callback: function (token: string) {
localStorage.setItem("captcha-token", token);
},
});
};

// @ts-ignore
window.captchaCallback = (token: string) => {
localStorage.setItem("captcha-token", token);
};
if (isCloudflareCaptcha && !constraint.isVerified) handleLoadTurnstile();
// @ts-ignore
window.captchaCallback = (token: string) => {
localStorage.setItem("captcha-token", token);
};
}

return () => {
isMounted = false;
Expand Down Expand Up @@ -468,6 +464,14 @@ const PrizeRequirementBody: FC<{
></div>
</div>
)}
{constraint.name === "core.HasVerifiedHCaptcha" && (
<HCaptcha
sitekey="your-sitekey"
onVerify={(token, ekey) =>
localStorage.setItem("captcha-token", token)
}
/>
)}
</main>
<div className="mt-auto flex w-full items-center rounded-lg bg-gray20 p-2">
{checkConnections(connections, constraint.name) ? (
Expand Down
8 changes: 1 addition & 7 deletions context/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,7 @@ export const UnitapProvider: FC<PropsWithChildren> = async ({ children }) => {
},
cache: "no-store",
},
)
.then((res) => {
if (!res.ok) throw new Error("Unauthorized");

return res;
})
.then((res) => res.json());
);
} catch {}

const settings: Settings = settingsRes.reduce((prev, curr) => {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
},
"dependencies": {
"@ethersproject/providers": "^5.7.2",
"@hcaptcha/react-hcaptcha": "^1.11.0",
"@metamask/logo": "^3.1.2",
"@sentry/nextjs": "7.105.0",
"@solana/web3.js": "^1.89.1",
Expand Down
2 changes: 2 additions & 0 deletions utils/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export const serverFetch = async (url: string, init?: RequestInit) => {
if (init?.headers) {
// @ts-ignore
init.headers.backend_service_key = process.env.BACKEND_SERVICE_KEY;
// @ts-ignore
init.headers.Cookie = `backend_service_key=${process.env.BACKEND_SERVICE_KEY};`;
}

const res = await fetch(process.env.NEXT_PUBLIC_API_URL! + url, {
Expand Down
6 changes: 2 additions & 4 deletions utils/serverApis/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@ export const getUserHistory = async (token?: string) => {
};

export const getAllConnections = async (token?: string) => {
const res = await serverFetch("/api/auth/user/all-connections/", {
const data = (await serverFetch("/api/auth/user/all-connections/", {
headers: {
Authorization: `token ${token}`,
},
});

const data = (await res.json()) as UserConnection[];
})) as UserConnection[];

const transformedData = data.reduce((prev, curr) => {
const name = Object.keys(curr)[0];
Expand Down
15 changes: 6 additions & 9 deletions utils/serverApis/contributionHub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const getUserRaffleListServerSide = async (token?: string) => {
Authorization: `token ${token}`,
},
},
).then((res) => res.json());
);

if (!Array.isArray(res)) return [];

Expand All @@ -27,27 +27,23 @@ export const getUserDistributionListServerSide = async (token?: string) => {
headers: {
Authorization: `token ${token}`,
},
}).then((res) => res.json());
});

if (!Array.isArray(res)) return [];

return res as UserTokenDistribution[];
};

export const getConstraintListServer = async () => {
const res = await serverFetch("/api/prizetap/get-constraints/").then((res) =>
res.json(),
);
const res = await serverFetch("/api/prizetap/get-constraints/");

if (typeof res !== "object") return {};

return res as { [key: string]: ConstraintProps[] };
};

export const getTokenTapConstraintListServer = async () => {
const res = await serverFetch("/api/tokentap/get-constraints/").then((res) =>
res.json(),
);
const res = await serverFetch("/api/tokentap/get-constraints/");

if (typeof res !== "object") return {};

Expand All @@ -62,6 +58,7 @@ export const getUserDonationsServer = async (token?: string) => {
Authorization: `token ${token}`,
},
},
).then((res) => res.json());
);

return res;
};
12 changes: 4 additions & 8 deletions utils/serverApis/gastap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ import { ClaimReceipt, Faucet, FuelChampion } from "@/types";
import { convertFaucetToChain, serverFetch } from "../api";

export const getFaucetListServer = async () => {
const chainsApi = await serverFetch("/api/gastap/faucet/list/");

const data = await chainsApi.json();
const data = await serverFetch("/api/gastap/faucet/list/");

return data.map((item: Faucet) => convertFaucetToChain(item));
};
Expand All @@ -16,7 +14,7 @@ export const getClaimedReceiptsServer = async (token?: string) => {
headers: {
Authorization: `token ${token}`,
},
}).then((res) => res.json());
});

if (!Array.isArray(res)) return [];

Expand All @@ -33,7 +31,7 @@ export const getOneTimeClaimedReceiptsServer = async (token?: string) => {
headers: {
Authorization: `token ${token}`,
},
}).then((res) => res.json());
});

if (!Array.isArray(res)) return [];

Expand All @@ -44,9 +42,7 @@ export const getOneTimeClaimedReceiptsServer = async (token?: string) => {
};

export async function getFuelChampionListServerSide() {
const response = await serverFetch("/api/gastap/fuel-champion").then((res) =>
res.json(),
);
const response = await serverFetch("/api/gastap/fuel-champion");

return response as FuelChampion[];
}
4 changes: 1 addition & 3 deletions utils/serverApis/prizetap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ import { Prize } from "@/types";
import { serverFetch } from "../api";

export async function getRafflesServerSideListAPI() {
const response: Prize[] = await serverFetch(
"/api/prizetap/raffle-list/",
).then((res) => res.json());
const response: Prize[] = await serverFetch("/api/prizetap/raffle-list/");

return response.filter(
(raffle) => raffle.status !== "PENDING" && raffle.status !== "REJECTED",
Expand Down
20 changes: 20 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@
dependencies:
regenerator-runtime "^0.14.0"

"@babel/runtime@^7.17.9":
version "7.25.6"
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.25.6.tgz#9afc3289f7184d8d7f98b099884c26317b9264d2"
integrity sha512-VBj9MYyDb9tuLq7yzqjgzt6Q+IBQLrGZfdjOekyEirZPHxXWoTSGUTMrpsfi58Up73d13NfYLv8HT9vmznjzhQ==
dependencies:
regenerator-runtime "^0.14.0"

"@babel/runtime@^7.23.2":
version "7.23.2"
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.23.2.tgz#062b0ac103261d68a966c4c7baf2ae3e62ec3885"
Expand Down Expand Up @@ -787,6 +794,19 @@
dependencies:
"@hapi/hoek" "^9.0.0"

"@hcaptcha/loader@^1.2.1":
version "1.2.4"
resolved "https://registry.yarnpkg.com/@hcaptcha/loader/-/loader-1.2.4.tgz#541714395a82e27ec0f0e8bd80ef1a0bea141cc3"
integrity sha512-3MNrIy/nWBfyVVvMPBKdKrX7BeadgiimW0AL/a/8TohNtJqxoySKgTJEXOQvYwlHemQpUzFrIsK74ody7JiMYw==

"@hcaptcha/react-hcaptcha@^1.11.0":
version "1.11.0"
resolved "https://registry.yarnpkg.com/@hcaptcha/react-hcaptcha/-/react-hcaptcha-1.11.0.tgz#d4520dd3b7ece735bc928dedcda048be477b2dcd"
integrity sha512-UKHtzzVMHLTGwab5pgV96UbcXdyh5Qyq8E0G5DTyXq8txMvuDx7rSyC+BneOjWVW0a7O9VuZmkg/EznVLRE45g==
dependencies:
"@babel/runtime" "^7.17.9"
"@hcaptcha/loader" "^1.2.1"

"@humanwhocodes/config-array@^0.11.13":
version "0.11.13"
resolved "https://registry.yarnpkg.com/@humanwhocodes/config-array/-/config-array-0.11.13.tgz#075dc9684f40a531d9b26b0822153c1e832ee297"
Expand Down

0 comments on commit 956b191

Please sign in to comment.