Skip to content

Commit

Permalink
Fix: Persist current step in URL for Quest dashboard
Browse files Browse the repository at this point in the history
  • Loading branch information
davedumto committed Jan 24, 2025
1 parent cf1a679 commit 0ca386b
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions app/admin/quests/dashboard/[questId]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import React, {
useMemo,
useRef,
useState,
SetStateAction
} from "react";
import styles from "@styles/admin.module.css";
import { useRouter } from "next/navigation";
import { AdminService } from "@services/authService";
import { QuestDefault } from "@constants/common";
import { nft_uri, QuizQuestionDefaultInput, formSteps } from "@constants/admin";
Expand All @@ -27,6 +27,7 @@ import RewardDetailsForm from "@components/admin/formSteps/RewardDetailsForm";
import TaskDetailsForm from "@components/admin/formSteps/TaskDetailsForm";
import { TEXT_TYPE } from "@constants/typography";
import FormContainer from "@components/admin/FormContainer";
import { useRouter, useSearchParams } from 'next/navigation';

type QuestIdProps = {
params: {
Expand All @@ -52,7 +53,20 @@ type StepMap =

export default function Page({ params }: QuestIdProps) {
const router = useRouter();
const [currentPage, setCurrentPage] = useState(0);
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 questId = useRef(parseInt(params.questId));
const [questInput, setQuestInput] = useState<UpdateQuest>({
id: Number(params.questId),
Expand Down Expand Up @@ -420,7 +434,7 @@ export default function Page({ params }: QuestIdProps) {
// add tasks
await handleAddTasks(addedTasks);

setCurrentPage((prev) => prev + 1);
handleTabChange(currentPage + 1)
}, [steps, intialSteps]);

const handleCreateBoost = useCallback(async () => {
Expand Down Expand Up @@ -465,7 +479,7 @@ export default function Page({ params }: QuestIdProps) {
}
}
setButtonLoading(false);
setCurrentPage((prev) => prev + 1);
handleTabChange(currentPage + 1)
};

const handleAddTasks = useCallback(async (addedTasks: StepMap[]) => {
Expand Down Expand Up @@ -764,7 +778,7 @@ export default function Page({ params }: QuestIdProps) {
questInput={questInput}
handleQuestInputChange={handleQuestInputChange}
submitButtonDisabled={isButtonDisabled}
onSubmit={() => setCurrentPage(currentPage + 1)}
onSubmit={() => handleTabChange(currentPage + 1)}
/>
);
} else if (currentPage === 1) {
Expand Down Expand Up @@ -840,7 +854,7 @@ export default function Page({ params }: QuestIdProps) {
headingText="Edit Quest"
steps={formSteps}
currentPage={currentPage}
setCurrentPage={setCurrentPage}
setCurrentPage={handleTabChange}
>
{questData.id !== 0 ? (
renderFormStep()
Expand Down

0 comments on commit 0ca386b

Please sign in to comment.