Skip to content

Commit

Permalink
Merge pull request #1086 from Snehagupta1907/fix-1017
Browse files Browse the repository at this point in the history
Feat: Add Mandatory Domain checkbox to QuestDetailsForm and update Cr…
  • Loading branch information
Marchand-Nicolas authored Jan 28, 2025
2 parents bebf69b + aff696e commit e00d8e9
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 80 deletions.
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

0 comments on commit e00d8e9

Please sign in to comment.