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

Fix: New banner step when editing a quest #1060

Merged
merged 4 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
28 changes: 25 additions & 3 deletions app/admin/quests/dashboard/[questId]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import Typography from "@components/UI/typography/typography";
import QuestDetailsForm from "@components/admin/formSteps/QuestDetailsForm";
import RewardDetailsForm from "@components/admin/formSteps/RewardDetailsForm";
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";

Expand Down Expand Up @@ -355,6 +356,7 @@ export default function Page({ params }: QuestIdProps) {
[]
);


const handleTasksInputChange = useCallback(
(e: React.ChangeEvent<HTMLInputElement>, index: number) => {
const { name, value } = e.target;
Expand All @@ -369,6 +371,7 @@ export default function Page({ params }: QuestIdProps) {
[]
);


useEffect(() => {
//check if start time is less than current time
if (new Date(parseInt(startTime)).getTime() < new Date().getTime()) {
Expand Down Expand Up @@ -701,19 +704,23 @@ export default function Page({ params }: QuestIdProps) {
!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;

if (currentPage === 0) {
return questInputValid;
} else if (currentPage === 1) {
return (showBoost && boostInputValid) || nftUriValid || questRewardValid;
}
if (currentPage === 2) {
} else if (currentPage === 2) {
return steps.some((step) => step.type === "None");
} else if (currentPage === 3) {
return bannerValid;
}
return false;
}, [
currentPage,
questInput,
questInput.banner,
nfturi,
steps,
showBoost,
Expand Down Expand Up @@ -807,7 +814,22 @@ export default function Page({ params }: QuestIdProps) {
}}
/>
);
} 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);
Comment on lines +883 to +885
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add loading state to prevent multiple submissions.

The banner update submission handler doesn't prevent multiple submissions while the update is in progress. This could lead to race conditions or duplicate updates.

Add the buttonLoading prop to the BannerDetailsForm component and use it to disable the submit button during updates.

}}
/>
);
}
Comment on lines +875 to +889
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Add error handling for banner update.

The banner update logic lacks error handling. If the update fails, the user won't receive proper feedback and the form will still proceed to the next step.

       onSubmit={async () => {
-          await handleUpdateQuest(); 
-          showNotification("Banner updated successfully", "success");
-          setCurrentPage((prev) => prev + 1); 
+          try {
+            setButtonLoading(true);
+            await handleUpdateQuest();
+            showNotification("Banner updated successfully", "success");
+            setCurrentPage((prev) => prev + 1);
+          } catch (error) {
+            showNotification("Failed to update banner. Please try again.", "error");
+            console.error("Error updating banner:", error);
+          } finally {
+            setButtonLoading(false);
+          }
         }}
+        buttonLoading={buttonLoading}

Committable suggestion skipped: line range outside the PR's diff.

else if (currentPage === 4) {
if (questData.id === 0) {
return (
<div>
Expand Down
73 changes: 73 additions & 0 deletions components/admin/formSteps/BannerDetailsForm.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import React, { FunctionComponent } from "react";
import TextInput from "../textInput";
import { UpdateQuest } from "../../../types/backTypes";
import Button from "@components/UI/button";

type BannerDetailsFormProps = {
questInput: UpdateQuest;
setQuestInput: React.Dispatch<React.SetStateAction<UpdateQuest>>;
handleQuestInputChange: (e: React.ChangeEvent<HTMLInputElement>) => void;
onSubmit: () => void;
submitButtonDisabled: boolean;
};

const BannerDetailsForm: FunctionComponent<BannerDetailsFormProps> = ({
questInput,
handleQuestInputChange,
onSubmit,
submitButtonDisabled,
}) => {
return (
<div className="flex flex-col gap-4">
<TextInput
name="tag"
value={questInput.banner?.tag || ""}
onChange={handleQuestInputChange}
label="Tag"
placeholder="Enter banner tag"
/>
<TextInput
name="title"
value={questInput.banner?.title || ""}
onChange={handleQuestInputChange}
label="Title"
placeholder="Enter banner title"
/>
<TextInput
name="description"
value={questInput.banner?.description || ""}
onChange={handleQuestInputChange}
label="Description"
placeholder="Enter banner description"
/>
<TextInput
name="cta"
value={questInput.banner?.cta || ""}
onChange={handleQuestInputChange}
label="CTA"
placeholder="Enter Call-to-Action"
/>
<TextInput
name="href"
value={questInput.banner?.href || ""}
onChange={handleQuestInputChange}
label="Link"
placeholder="Enter Link"
/>
<TextInput
name="image"
value={questInput.banner?.image || ""}
onChange={handleQuestInputChange}
label="Image URL"
placeholder="Enter Image URL"
/>
<div className="w-full sm:w-fit">
<Button onClick={onSubmit} disabled={submitButtonDisabled}>
<p>Save Changes</p>
</Button>
</div>
</div>
);
};

export default BannerDetailsForm;
2 changes: 1 addition & 1 deletion constants/admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const CATEGORY_OPTIONS = [
"Starknet Pro Score by Braavos",
];

export const formSteps = ["Setup", "Reward", "Tasks", "Preview"];
export const formSteps = ["Setup", "Reward", "Tasks", "Banner", "Preview"];

export const TASK_OPTIONS = [
"Quiz",
Expand Down
Loading