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
priority
parameter to theTodoList
smart 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.sol
contract. - Add a new parameter
priority
to theTask
struct:struct Task {
uint32 id;
string task;
bool completed;
string priority; // New parameter
} - Update the
createTask
function to accept thepriority
parameter: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
priority
parameter in theTaskCreated
event: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.tsx
file 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
handleAddTask
Function:- Modify the
createTask
call to include thepriority
parameter: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
priority
field:<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.sol
file. - A link to your updated repository.
- A brief description of the changes you made.
Good luck! 🚀