8. Challenge: Extend the Smart Contract and Integrate with the UI
This challenge will test your ability to modify the TodoList smart contract and integrate changes into the user interface. You will add a new parameter to the smart contract and reflect this change in the frontend application.
Challenge Overview
Objective:
- Add a
priorityparameter to theTodoListsmart contract to assign a priority level to each task (e.g.,Low,Medium,High). - Update the frontend to:
- Allow users to assign a priority when creating a task.
- Display the priority of each task in the task list.
Skills You'll Practice:
- Smart contract development with Solidity.
- Frontend integration using React and Wagmi.
- State management and contract interaction.
Part 1: Modify the Smart Contract
Steps:
- Open the
TodoList.solcontract. - Add a new parameter
priorityto theTaskstruct:struct Task {
uint32 id;
string task;
bool completed;
string priority; // New parameter
} - Update the
createTaskfunction to accept thepriorityparameter:function createTask(string memory _task, string memory _priority) external {
UserTasks storage userTaskList = userTasks[msg.sender];
userTaskList.tasks[userTaskList.taskCount] = Task({
id: userTaskList.taskCount,
task: _task,
completed: false,
priority: _priority
});
userTaskList.taskCount++;
emit TaskCreated(msg.sender, userTaskList.taskCount, _task, false, _priority);
} - Emit the new
priorityparameter in theTaskCreatedevent:event TaskCreated(address user, uint32 id, string task, bool completed, string priority); - Deploy the updated contract to the testnet.
Part 2: Update the Frontend
Steps:
-
Update the Task Form:
- Modify the
src/app/page.tsxfile to include an input field forpriority. - Example:
<MDBInput
label="Enter priority (Low, Medium, High)"
id="formPriority"
type="text"
style={{ backgroundColor: "#2B2F36", color: "#fff", fontSize: "1rem" }}
value={priority}
onChange={(e) => setPriority(e.target.value)}
/> - Add a new state variable
priority:const [priority, setPriority] = useState("");
- Modify the
-
Update the
handleAddTaskFunction:- Modify the
createTaskcall to include thepriorityparameter:const result = await writeContractAsync({
...todolistContract,
functionName: "createTask",
args: [task, priority],
account: address as `0x${string}`,
});
- Modify the
-
Display Priority in the Task List:
- Update the task list table to display the
priorityfield:<td>{todo.priority}</td> - Add a new table header:
<th scope="col">Priority</th>
- Update the task list table to display the
Part 3: Test Your Changes
- Run the application locally:
npm run dev - Create new tasks with priorities and verify they appear in the task list.
- Ensure all existing functionality (e.g., completing and deleting tasks) still works.
Bonus Challenge
-
Add Priority Sorting:
- Implement a feature to sort tasks by priority (e.g., High > Medium > Low).
- Hint: Use the JavaScript
sort()method to reorder the task array before rendering.
-
Style by Priority:
- Apply different colors or styles to tasks based on their priority level (e.g., red for High, yellow for Medium, green for Low).
Submission
Once you've completed the challenge, submit the following:
- The updated
TodoList.solfile. - A link to your updated repository.
- A brief description of the changes you made.
Good luck! 🚀