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

Feat: Add Mandatory Domain checkbox to QuestDetailsForm and update Cr… #1086

Merged
merged 2 commits into from
Jan 28, 2025
Merged
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
164 changes: 86 additions & 78 deletions app/admin/quests/dashboard/[questId]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import React, {
useMemo,
useRef,
useState,
SetStateAction
SetStateAction,
} from "react";
import styles from "@styles/admin.module.css";
import { AdminService } from "@services/authService";
Expand All @@ -28,7 +28,7 @@ import TaskDetailsForm from "@components/admin/formSteps/TaskDetailsForm";
import BannerDetailsForm from "@components/admin/formSteps/BannerDetailsForm";
import { TEXT_TYPE } from "@constants/typography";
import FormContainer from "@components/admin/FormContainer";
import { useRouter, useSearchParams } from 'next/navigation';
import { useRouter, useSearchParams } from "next/navigation";

type QuestIdProps = {
params: {
Expand All @@ -55,19 +55,20 @@ type StepMap =
export default function Page({ params }: QuestIdProps) {
const router = useRouter();
const searchParams = useSearchParams();
const [currentPage, setCurrentPage] = useState(() => {
const tabParam = searchParams.get('tab');
return tabParam ? parseInt(tabParam) : 0;
});
const handleTabChange = (pageOrUpdater: SetStateAction<number>) => {
// If it's a function, calculate the new page value
const newPage = typeof pageOrUpdater === 'function'
? pageOrUpdater(currentPage)
: pageOrUpdater;

setCurrentPage(newPage);
router.push(`?tab=${newPage}`, { scroll: false });
};
const [currentPage, setCurrentPage] = useState(() => {
const tabParam = searchParams.get("tab");
return tabParam ? parseInt(tabParam) : 0;
});
const handleTabChange = (pageOrUpdater: SetStateAction<number>) => {
// If it's a function, calculate the new page value
const newPage =
typeof pageOrUpdater === "function"
? pageOrUpdater(currentPage)
: pageOrUpdater;

setCurrentPage(newPage);
router.push(`?tab=${newPage}`, { scroll: false });
};
const questId = useRef(parseInt(params.questId));
const [questInput, setQuestInput] = useState<UpdateQuest>({
id: Number(params.questId),
Expand Down Expand Up @@ -354,12 +355,15 @@ const handleTabChange = (pageOrUpdater: SetStateAction<number>) => {
return { updatedTasks, removedTasks, addedTasks };
}, [steps, intialSteps]);

console.log(questInput);

const handleQuestInputChange = useCallback(
(e: React.ChangeEvent<HTMLInputElement>) => {
const { name, value } = e.target;

setQuestInput((prev) => ({
...prev,
[name]: value,
banner: {
...(prev.banner ?? {
tag: "",
Expand All @@ -368,15 +372,13 @@ const handleTabChange = (pageOrUpdater: SetStateAction<number>) => {
cta: "",
href: "",
image: "",
}),
[name]: value,
}),
},
}));
},
[]
);


const handleBoostInputChange = useCallback(
(e: React.ChangeEvent<HTMLInputElement>) => {
const { name, value } = e.target;
Expand All @@ -385,7 +387,6 @@ const handleTabChange = (pageOrUpdater: SetStateAction<number>) => {
[]
);


const handleTasksInputChange = useCallback(
(e: React.ChangeEvent<HTMLInputElement>, index: number) => {
const { name, value } = e.target;
Expand All @@ -400,7 +401,6 @@ const handleTabChange = (pageOrUpdater: SetStateAction<number>) => {
[]
);


useEffect(() => {
//check if start time is less than current time
if (new Date(parseInt(startTime)).getTime() < new Date().getTime()) {
Expand Down Expand Up @@ -452,7 +452,7 @@ const handleTabChange = (pageOrUpdater: SetStateAction<number>) => {
// add tasks
await handleAddTasks(addedTasks);

handleTabChange(currentPage + 1)
handleTabChange(currentPage + 1);
}, [steps, intialSteps]);

const handleCreateBoost = useCallback(async () => {
Expand Down Expand Up @@ -497,7 +497,7 @@ const handleTabChange = (pageOrUpdater: SetStateAction<number>) => {
}
}
setButtonLoading(false);
handleTabChange(currentPage + 1)
handleTabChange(currentPage + 1);
};

const handleAddTasks = useCallback(async (addedTasks: StepMap[]) => {
Expand All @@ -513,18 +513,19 @@ const handleTabChange = (pageOrUpdater: SetStateAction<number>) => {
cta: step.data.quiz_cta,
help_link: step.data.quiz_help_link,
});

const quizQuestionPromises = step.data.questions.map(async (question: any) =>
AdminService.createQuizQuestion({
quiz_id: step.data.quiz_name,
question: question.question,
options: question.options,
correct_answers: question.correct_answers,
})

const quizQuestionPromises = step.data.questions.map(
async (question: any) =>
AdminService.createQuizQuestion({
quiz_id: step.data.quiz_name,
question: question.question,
options: question.options,
correct_answers: question.correct_answers,
})
);
await Promise.all(quizQuestionPromises);
break;

case "TwitterFw":
await AdminService.createTwitterFw({
quest_id: questId.current,
Expand All @@ -533,7 +534,7 @@ const handleTabChange = (pageOrUpdater: SetStateAction<number>) => {
username: step.data.twfw_username,
});
break;

case "TwitterRw":
await AdminService.createTwitterRw({
quest_id: questId.current,
Expand All @@ -542,7 +543,7 @@ const handleTabChange = (pageOrUpdater: SetStateAction<number>) => {
post_link: step.data.twrw_post_link,
});
break;

case "Discord":
await AdminService.createDiscord({
quest_id: questId.current,
Expand All @@ -552,7 +553,7 @@ const handleTabChange = (pageOrUpdater: SetStateAction<number>) => {
guild_id: step.data.dc_guild_id,
});
break;

case "Custom":
await AdminService.createCustom({
quest_id: questId.current,
Expand All @@ -563,15 +564,15 @@ const handleTabChange = (pageOrUpdater: SetStateAction<number>) => {
api: step.data.custom_api,
});
break;

case "Domain":
await AdminService.createDomain({
quest_id: questId.current,
name: step.data.domain_name,
desc: step.data.domain_desc,
});
break;

case "Balance":
await AdminService.createBalance({
quest_id: questId.current,
Expand All @@ -582,7 +583,7 @@ const handleTabChange = (pageOrUpdater: SetStateAction<number>) => {
href: step.data.balance_href,
});
break;

case "Contract":
await AdminService.createContract({
quest_id: questId.current,
Expand All @@ -593,7 +594,7 @@ const handleTabChange = (pageOrUpdater: SetStateAction<number>) => {
calls: JSON.parse(step.data.contract_calls),
});
break;

case "CustomApi":
await AdminService.createCustomApi({
quest_id: questId.current,
Expand All @@ -610,7 +611,7 @@ const handleTabChange = (pageOrUpdater: SetStateAction<number>) => {
showNotification(`Error adding ${step.type} task: ${error}`, "error");
}
});

await Promise.all(taskPromises);
}, []);

Expand Down Expand Up @@ -638,27 +639,29 @@ const handleTabChange = (pageOrUpdater: SetStateAction<number>) => {
help_link: step.data.quiz_help_link,
quiz_id: step.data.quiz_id,
});

const quizQuestionPromises = step.data.questions.map(async (question: any) => {
if (question.id === 0) {
await AdminService.createQuizQuestion({
quiz_id: step.data.quiz_id,

const quizQuestionPromises = step.data.questions.map(
async (question: any) => {
if (question.id === 0) {
await AdminService.createQuizQuestion({
quiz_id: step.data.quiz_id,
question: question.question,
options: question.options,
correct_answers: question.correct_answers,
});
}
return AdminService.updateQuizQuestion({
id: question.id,
question: question.question,
options: question.options,
correct_answers: question.correct_answers,
quiz_id: step.data.quiz_id,
});
}
return AdminService.updateQuizQuestion({
id: question.id,
question: question.question,
options: question.options,
correct_answers: question.correct_answers,
quiz_id: step.data.quiz_id,
});
});
);
await Promise.all(quizQuestionPromises);
break;

case "TwitterFw":
await AdminService.updateTwitterFw({
id: step.data.id,
Expand All @@ -667,7 +670,7 @@ const handleTabChange = (pageOrUpdater: SetStateAction<number>) => {
username: step.data.twfw_username,
});
break;

case "TwitterRw":
await AdminService.updateTwitterRw({
id: step.data.id,
Expand All @@ -676,7 +679,7 @@ const handleTabChange = (pageOrUpdater: SetStateAction<number>) => {
post_link: step.data.twrw_post_link,
});
break;

case "Discord":
await AdminService.updateDiscord({
id: step.data.id,
Expand All @@ -686,7 +689,7 @@ const handleTabChange = (pageOrUpdater: SetStateAction<number>) => {
guild_id: step.data.dc_guild_id,
});
break;

case "Custom":
await AdminService.updateCustom({
id: step.data.id,
Expand All @@ -697,15 +700,15 @@ const handleTabChange = (pageOrUpdater: SetStateAction<number>) => {
api: step.data.custom_api,
});
break;

case "Domain":
await AdminService.updateDomain({
id: step.data.id,
name: step.data.custom_name,
desc: step.data.custom_desc,
});
break;

case "Balance":
await AdminService.updateBalance({
id: step.data.id,
Expand All @@ -716,7 +719,7 @@ const handleTabChange = (pageOrUpdater: SetStateAction<number>) => {
href: step.data.balance_href,
});
break;

case "Contract":
await AdminService.updateContract({
id: step.data.id,
Expand All @@ -727,7 +730,7 @@ const handleTabChange = (pageOrUpdater: SetStateAction<number>) => {
calls: JSON.parse(step.data.contract_calls),
});
break;

case "CustomApi":
await AdminService.updateCustomApi({
id: step.data.id,
Expand All @@ -744,7 +747,7 @@ const handleTabChange = (pageOrUpdater: SetStateAction<number>) => {
showNotification(`Error updating ${step.type} task: ${error}`, "error");
}
});

await Promise.all(taskPromises);
}, []);

Expand All @@ -761,8 +764,14 @@ const handleTabChange = (pageOrUpdater: SetStateAction<number>) => {
!questInput.category;

const questRewardValid = !questInput.rewards_title || !questInput.logo;

const bannerValid = !questInput.banner?.tag || !questInput.banner?.title || !questInput.banner?.description || !questInput.banner?.cta || !questInput.banner?.href || !questInput.banner?.image;

const bannerValid =
!questInput.banner?.tag ||
!questInput.banner?.title ||
!questInput.banner?.description ||
!questInput.banner?.cta ||
!questInput.banner?.href ||
!questInput.banner?.image;

if (currentPage === 0) {
return questInputValid;
Expand Down Expand Up @@ -872,22 +881,21 @@ const handleTabChange = (pageOrUpdater: SetStateAction<number>) => {
}}
/>
);
} else if (currentPage === 3) {
} else if (currentPage === 3) {
return (
<BannerDetailsForm
setQuestInput={setQuestInput}
questInput={questInput}
handleQuestInputChange={handleQuestInputChange}
submitButtonDisabled={isButtonDisabled}
onSubmit={async () => {
await handleUpdateQuest();
showNotification("Banner updated successfully", "success");
setCurrentPage((prev) => prev + 1);
}}
/>
<BannerDetailsForm
setQuestInput={setQuestInput}
questInput={questInput}
handleQuestInputChange={handleQuestInputChange}
submitButtonDisabled={isButtonDisabled}
onSubmit={async () => {
await handleUpdateQuest();
showNotification("Banner updated successfully", "success");
setCurrentPage((prev) => prev + 1);
}}
/>
);
}
else if (currentPage === 4) {
} else if (currentPage === 4) {
if (questData.id === 0) {
return (
<div>
Expand Down
Loading