Skip to content

Commit

Permalink
Merge pull request #988 from MaiCVCR/fix-978
Browse files Browse the repository at this point in the history
Fix: New Download quest users button
  • Loading branch information
Marchand-Nicolas authored Dec 15, 2024
2 parents 311b9bf + a9f6d7d commit c9e2487
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 2 deletions.
1 change: 1 addition & 0 deletions app/admin/quests/create/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,7 @@ export default function Page() {
await fetchQuestData();
}}
overrideDisabledState={false}
isEdit={false}
/>
);
}
Expand Down
1 change: 1 addition & 0 deletions app/admin/quests/dashboard/[questId]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -834,6 +834,7 @@ export default function Page({ params }: QuestIdProps) {
await fetchPageData();
}}
overrideDisabledState={false}
isEdit={true}
/>
);
}
Expand Down
11 changes: 10 additions & 1 deletion components/admin/questDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ 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 { AdminService } from "@services/authService";
import { useNotification } from "@context/NotificationProvider";
import Button from "@components/UI/button";
Expand All @@ -34,6 +35,7 @@ type QuestDetailsProps = {
rewardButtonTitle?: string;
onRewardButtonClick?: () => void;
overrideDisabledState?: boolean;
isEdit?: boolean;
};

const AdminQuestDetails: FunctionComponent<QuestDetailsProps> = ({
Expand All @@ -43,6 +45,7 @@ const AdminQuestDetails: FunctionComponent<QuestDetailsProps> = ({
hasNftReward,
rewardButtonTitle,
onRewardButtonClick,
isEdit,
}) => {
const { address } = useAccount();
const router = useRouter();
Expand Down Expand Up @@ -234,9 +237,15 @@ const AdminQuestDetails: FunctionComponent<QuestDetailsProps> = ({
<p>Go To Quest</p>
</Button>
</div>
{isEdit && (
<div className="w-fit">
<DownloadQuestUsersButton questId={questId} />
</div>
)}
</div>

</>
);
};

export default AdminQuestDetails;
export default AdminQuestDetails;
37 changes: 37 additions & 0 deletions components/quests/downloadQuestUsersButton.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 QuestUsersButtonProps = {
questId: string;
};

const DownloadQuestUsersButton: React.FC<QuestUsersButtonProps> = ({ questId }) => {
const { showNotification } = useNotification();

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

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 = `quest_${questId}_users.json`;
a.click();
URL.revokeObjectURL(url);
} catch (error) {
console.error("Error downloading quest users:", error);
showNotification("Failed to download quest users.", "error");
}
};

return (
<Button onClick={handleDownload}>
<p>Download quest users</p>
</Button>
);
};

export default DownloadQuestUsersButton;
25 changes: 24 additions & 1 deletion services/authService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import {
const baseurl = process.env.NEXT_PUBLIC_API_LINK;

const login = async (params: { passcode: string }) => {
try {
try {
const { passcode } = params;
const response = await fetch(`${baseurl}/admin/login?code=${passcode}`);
return await response.json();
Expand Down Expand Up @@ -89,6 +89,28 @@ const getTasksByQuestId = async (id: number) => {
console.log("Error while getting tasks by id", err);
}
};

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

const getQuests = async () => {
try {
const response = await fetch(`${baseurl}/admin/quest/get_quests`, {
Expand Down Expand Up @@ -611,6 +633,7 @@ export const AdminService = {
updateQuest,
getQuestById,
getTasksByQuestId,
getQuestUsersByQuestId,
updateBoost,
updateTwitterRw,
updateTwitterFw,
Expand Down

0 comments on commit c9e2487

Please sign in to comment.