Skip to content

Commit

Permalink
Merge pull request #992 from gadyrcdz/fix-#980
Browse files Browse the repository at this point in the history
Fix: Add reusable component for downloading boost winners using authS…
  • Loading branch information
Marchand-Nicolas authored Dec 21, 2024
2 parents f6ccbf5 + 932b966 commit c24d9d8
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 6 deletions.
5 changes: 2 additions & 3 deletions app/admin/quests/create/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -378,12 +378,11 @@ export default function Page() {
api_url: step.data.api_url,
cta: step.data.api_cta,
href: step.data.api_href,
regex: step.data.api_regex
regex: step.data.api_regex,
});
} catch (error) {
console.error("Error while creating balance task:", error);
}

} else if (step.type === "Contract") {
try {
await AdminService.createContract({
Expand Down Expand Up @@ -495,7 +494,7 @@ export default function Page() {
<AdminQuestDetails
quest={finalQuestData}
// eslint-disable-next-line @typescript-eslint/no-empty-function
setShowDomainPopup={() => { }}
setShowDomainPopup={() => {}}
hasRootDomain={false}
rewardButtonTitle={finalQuestData.disabled ? "Enable" : "Disable"}
onRewardButtonClick={async () => {
Expand Down
37 changes: 37 additions & 0 deletions components/admin/DownloadBoostWinnersButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import React from "react";
import Button from "@components/UI/button";
import { useNotification } from "@context/NotificationProvider";
import { AdminService } from '@services/authService';

type DownloadBoostWinnersButtonProps = {
boostId: string;
};

const DownloadBoostWinnersButton: React.FC<DownloadBoostWinnersButtonProps> = ({ boostId }) => {
const { showNotification } = useNotification();

const handleDownload = async () => {
try {
const data = await AdminService.getBoostWinnersByBoostId({ id: Number(boostId) });

const blob = new Blob([JSON.stringify(data, null, 2)], { type: "application/json" });
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = `boost_${boostId}_winners.json`;
a.click();
URL.revokeObjectURL(url);
} catch (error) {
console.error("Error downloading boost winners:", error);
showNotification("Failed to download boost winners.", "error");
}
};

return (
<Button onClick={handleDownload}>
<p>Download boost winners</p>
</Button>
);
};

export default DownloadBoostWinnersButton;
14 changes: 11 additions & 3 deletions components/admin/questDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ import Timer from "@components/quests/timer";
import NftImage from "@components/quests/nftImage";
import Task from "@components/quests/task";
import Reward from "@components/quests/reward";
import DownloadQuestUsersButton from "@components/quests/downloadQuestUsersButton"
import DownloadQuestUsersButton from "@components/quests/downloadQuestUsersButton";
import { AdminService } from "@services/authService";
import { useNotification } from "@context/NotificationProvider";
import Button from "@components/UI/button";
import { useRouter } from "next/navigation";
import DownloadQuestParticipantsButton from './DownloadQuestParticipantsButton';
import DownloadQuestParticipantsButton from "./DownloadQuestParticipantsButton";
import DownloadBoostWinnersButton from "./DownloadBoostWinnersButton";

type QuestDetailsProps = {
quest: QuestDocument;
Expand Down Expand Up @@ -238,6 +239,13 @@ const AdminQuestDetails: FunctionComponent<QuestDetailsProps> = ({
<p>Go To Quest</p>
</Button>
</div>
{isEdit &&
quest.boosts?.length > 0 &&
quest.boosts.map((boost) => (
<div className="w-fit" key={boost.id}>
<DownloadBoostWinnersButton boostId={boost.id.toString()} />
</div>
))}
{isEdit && (
<>
<div className="w-fit">
Expand All @@ -253,4 +261,4 @@ const AdminQuestDetails: FunctionComponent<QuestDetailsProps> = ({
);
};

export default AdminQuestDetails;
export default AdminQuestDetails;
22 changes: 22 additions & 0 deletions services/authService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,27 @@ const getQuestParticipantsByQuestId = async (params: { id: number }) => {
}
};

const getBoostWinnersByBoostId = async (params: { id: number }) => {
try {
const response = await fetch(
`${baseurl}/admin/boosts/get_boost_winners?boost_id=${params.id}`,
{
method: "GET",
headers: {
Authorization: `Bearer ${localStorage.getItem("token")}`,
},
}
);
if (!response.ok) {
throw new Error("Failed to fetch boost winners");
}
return await response.json();
} catch (err) {
console.log("Error while getting boost winners by boost id", err);
throw err;
}
};

export const AdminService = {
login,
getQuests,
Expand Down Expand Up @@ -671,4 +692,5 @@ export const AdminService = {
updateNftUri,
addUser,
getQuestParticipantsByQuestId,
getBoostWinnersByBoostId,
};
1 change: 1 addition & 0 deletions types/backTypes.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type QuestDocument = {
expired: boolean;
visible: boolean;
banner?: Banner;
boosts: Boost[];
};

type Banner = {
Expand Down

0 comments on commit c24d9d8

Please sign in to comment.