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

added new contract task type in the admin dashboard #843

Merged
merged 11 commits into from
Oct 6, 2024
31 changes: 31 additions & 0 deletions app/admin/quests/dashboard/[questId]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type StepMap =
| { type: "Custom"; data: WithNewField<CustomInputType, "id", number> }
| { type: "Domain"; data: WithNewField<DomainInputType, "id", number> }
| { type: "Balance"; data: WithNewField<BalanceInputType, "id", number> }
| { type: "Contract"; data: WithNewField<ContractInputType, "id", number> }
| { type: "None"; data: object };

export default function Page({ params }: QuestIdProps) {
Expand Down Expand Up @@ -215,6 +216,18 @@ export default function Page({ params }: QuestIdProps) {
balance_href: task.href,
},
};
} else if (task.task_type === "contract") {
return {
type: "Contract",
data: {
id: task.id,
contract_name: task.name,
contract_desc: task.desc,
contract_href: task.href,
contract_cta: task.cta,
contract_calls: task.calls,
},
};
}
});

Expand Down Expand Up @@ -509,6 +522,15 @@ export default function Page({ params }: QuestIdProps) {
cta: step.data.balance_cta,
href: step.data.balance_href,
});
} else if (step.type === "Contract") {
await AdminService.createContract({
quest_id: questId.current,
name: step.data.contract_name,
desc: step.data.contract_desc,
href: step.data.contract_href,
cta: step.data.contract_cta,
calls: step.data.calls,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Here, this needs to be an object, not a string. You can simply use JSON.parse()

});
}
} catch (error) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

In this catch, you can add

showNotification(`Error adding ${step.type} task: ${error}`, "error");

So that if there is any issue while parsing the JSON, the user is instantly informed

console.error(`Error adding task of type ${step.type}:`, error);
Expand Down Expand Up @@ -607,6 +629,15 @@ export default function Page({ params }: QuestIdProps) {
cta: step.data.balance_cta,
href: step.data.balance_href,
});
} else if (step.type === "Contract") {
await AdminService.updateContract({
Copy link
Collaborator

Choose a reason for hiding this comment

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

Also you could wrap this call with a try catch and add show

Notification(`Error updating ${step.type} task: ${error}`, "error");

in the catch

id: step.data.id,
name: step.data.contract_name,
desc: step.data.contract_desc,
href: step.data.contract_href,
cta: step.data.contract_cta,
calls: step.data.calls,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same here for JSON.parse()

});
}
});

Expand Down
9 changes: 9 additions & 0 deletions components/admin/formSteps/TaskDetailsForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import DomainStep from "../taskSteps/domainStep";
import Typography from "@components/UI/typography/typography";
import { TEXT_TYPE } from "@constants/typography";
import BalanceStep from "../taskSteps/balanceStep";
import ContractStep from "../taskSteps/contractStep";

type TaskDetailsFormProps = {
steps: StepMap[];
Expand Down Expand Up @@ -104,6 +105,14 @@ const TaskDetailsForm: FunctionComponent<TaskDetailsFormProps> = ({
step={step}
/>
);
} else if (step?.type === "Contract") {
return (
<ContractStep
Comment on lines +109 to +111
Copy link
Collaborator

Choose a reason for hiding this comment

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

Missing closing parenthesis

handleTasksInputChange={handleTasksInputChange}
index={currentTask}
step={step}
/>
);
}
};
return (
Expand Down
61 changes: 61 additions & 0 deletions components/admin/taskSteps/contractStep.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import React, { FunctionComponent } from "react";
import TextInput from "../textInput";

type ContractStepProps = {
handleTasksInputChange: (
e: React.ChangeEvent<HTMLInputElement>,
index: number
) => void;
step: StepMap;
index: number;
};
Marchand-Nicolas marked this conversation as resolved.
Show resolved Hide resolved

const ContractStep: FunctionComponent<ContractStepProps> = ({
handleTasksInputChange,
step,
index,
}) => {
return (
<div className="flex flex-col gap-4 pt-2">
<TextInput
onChange={(e) => handleTasksInputChange(e, index)}
value={step.data.contract_name || ""}
name="contract_name"
label="Name"
placeholder="Name"
/>
<TextInput
onChange={(e) => handleTasksInputChange(e, index)}
value={step.data.contract_desc || ""}
name="contract_desc"
label="Description"
placeholder="Description"
multiline={4}
/>
<TextInput
onChange={(e) => handleTasksInputChange(e, index)}
value={step.data.contract_href || ""}
name="contract_href"
label="URL"
placeholder="URL"
/>
<TextInput
onChange={(e) => handleTasksInputChange(e, index)}
value={step.data.contract_cta || ""}
name="contract_cta"
label="CTA"
placeholder="CTA"
/>
<TextInput
onChange={(e) => handleTasksInputChange(e, index)}
value={step.data.calls || ""}
PoulavBhowmick03 marked this conversation as resolved.
Show resolved Hide resolved
name="calls"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Still not working, because here it should be contract_calls instead of calls

label="Calls (JSON)"
placeholder='e.g.: [{ "contract": "0x...", "entry_point": "transfer", "call_data": ["0x..."], "regex": "..." }]'
multiline={4}
/>
</div>
);
};

export default ContractStep;
11 changes: 11 additions & 0 deletions constants/admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export const TASK_OPTIONS = [
"Custom",
"Domain",
"Balance",
"Contract",
];

export const TWITTER_OPTIONS = {
Expand Down Expand Up @@ -115,13 +116,23 @@ export const BalanceInput = {
balance_href: "",
};

export const ContractInput = {
contract_name: "",
contract_desc: "",
contract_href: "",
contract_cta: "",
calls: "",
PoulavBhowmick03 marked this conversation as resolved.
Show resolved Hide resolved
};
Marchand-Nicolas marked this conversation as resolved.
Show resolved Hide resolved

export const getDefaultValues = (type: TaskType) => {
if (type === "Quiz") return QuizDefaultInput;
if (type === "TwitterFw") return TwitterFwInput;
if (type === "TwitterRw") return TwitterRwInput;
if (type === "Discord") return DiscordInput;
if (type === "Custom") return CustomInput;
if (type === "Domain") return DomainInput;
if (type === "Balance") return BalanceInput;
if (type === "Contract") return ContractInput;
if (type === "None") return {};

return QuizDefaultInput;
Expand Down
36 changes: 36 additions & 0 deletions services/authService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import {
AddUser,
CreateBalance,
UpdateBalance,
CreateContract,
UpdateContract,
} from "../types/backTypes";

const baseurl = process.env.NEXT_PUBLIC_API_LINK;
Expand Down Expand Up @@ -354,6 +356,38 @@ const updateBalance = async (params: UpdateBalance) => {
}
};

const createContract = async (params: CreateContract) => {
try {
const response = await fetch(`${baseurl}/admin/tasks/contract/create`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${localStorage.getItem("token")}`,
},
body: JSON.stringify(params),
});
return await response.json();
} catch (err) {
console.log("Error creating contract task", err);
}
};

const updateContract = async (params: UpdateContract) => {
try {
const response = await fetch(`${baseurl}/admin/tasks/contract/update`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${localStorage.getItem("token")}`,
},
body: JSON.stringify(params),
});
return await response.json();
} catch (error) {
console.log("Error updating contract task", error);
}
};

const createQuiz = async (params: CreateQuiz) => {
try {
const response = await fetch(`${baseurl}/admin/tasks/quiz/create`, {
Expand Down Expand Up @@ -515,6 +549,8 @@ export const AdminService = {
createCustom,
createBalance,
updateBalance,
createContract,
updateContract,
createQuiz,
createQuizQuestion,
deleteTask,
Expand Down
19 changes: 19 additions & 0 deletions types/backTypes.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type UserTask = {
task_type: string | null;
discord_guild_id: string | null;
contracts: string[] | null;
calls: object | null;
};

type UserDocument = {
Expand Down Expand Up @@ -402,6 +403,24 @@ export type UpdateBalance = {
href?: string;
};

export type CreateContract = {
quest_id: number;
name: string;
desc: string;
href: string;
cta: string;
calls: object;
};

export type UpdateContract = {
id: number;
name?: string;
desc?: string;
href?: string;
cta?: string;
calls?: object;
};

export type UpdateCustom = {
id: number;
name?: string;
Expand Down
5 changes: 4 additions & 1 deletion types/frontTypes.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,8 @@ type StepMap =
| { type: "Custom"; data: CustomInputType }
| { type: "None"; data: object }
| { type: "Domain"; data: DomainInputType }
| { type: "Balance"; data: BalanceInputType };
| { type: "Balance"; data: BalanceInputType }
| { type: "Contract"; data: ContractInputType };
Copy link
Collaborator

Choose a reason for hiding this comment

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

Remove the ";" at the end of this line


type CustomInputType = typeof CustomInput;
type DiscordInputType = typeof DiscordInput;
Expand All @@ -324,6 +325,7 @@ type QuizInputType = typeof QuizDefaultInput;
type TwitterFwInputType = typeof TwitterFwInput;
type TwitterRwInputType = typeof TwitterRwInput;
type BalanceInputType = typeof BalanceInput;
type ContractInputType = typeof ContractInput;
type TaskType =
| "Quiz"
| "TwitterFw"
Expand All @@ -332,6 +334,7 @@ type TaskType =
| "TwitterRw"
| "Domain"
| "Balance"
| "Contract"
| "None";

type networks = "MAINNET" | "TESTNET";