'How to be aware of mapping updates so then i can update my client app?

Here is my smart contract for a todo list.

Everything works, i've made a front-end to test it.

But, i'm trying to get real time updates when a task is removed, added or updated.

I know there are events for that, but how can i use them with a mapping data type?

Anyone could help me ?

Thanks !

// SPDX-License-Identifier: MIT
pragma solidity >=0.4.22 <0.9.0;
pragma experimental ABIEncoderV2;

contract ToDoList {
    constructor() public {
        addTask("Tache initiale");
    }

    struct Task {
        uint256 id;
        string description;
        bool completed;
    }

    mapping(address => Task[]) public tasks;

    function addTask(string memory _description) public {
        uint256 id = uint256(
            keccak256(abi.encodePacked(msg.sender, _description))
        );

        tasks[msg.sender].push(
            Task({id: id, description: _description, completed: false})
        );
    }

    function changeTaskStatus(uint256 _id, bool _status) public {
        for (uint256 i = 0; i < tasks[msg.sender].length; i++) {
            if (tasks[msg.sender][i].id == _id) {
                tasks[msg.sender][i].completed = _status;
            }
        }
    }

    function removeTask(uint256 _id) public {
        for (uint256 i = 0; i < tasks[msg.sender].length; i++) {
            if (tasks[msg.sender][i].id == _id) {
                delete tasks[msg.sender][i];
            }
        }
    }

    function getTasks() public view returns (Task[] memory) {
        return tasks[msg.sender];
    }

    function tasksCount() public view returns (uint256) {
        return tasks[msg.sender].length;
    }
}


Solution 1:[1]

You are not emitting any events.You should create appropiate events and emit them properly. For example:

// SPDX-License-Identifier: MIT
pragma solidity >=0.4.22 <0.9.0;
pragma experimental ABIEncoderV2;

contract ToDoList {
    ...
   mapping(address => Task[]) public tasks;

    event AddTask(address indexed creator, uint256 indexed id, string description);

    function addTask(string memory _description) public {
        uint256 id = uint256(
            keccak256(abi.encodePacked(msg.sender, _description))
        );

        tasks[msg.sender].push(
            Task({id: id, description: _description, completed: false})
        );

        emit AddTask(msg.sender,id,_description);
    }

...
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 keser