Rizel Scarlett
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    import PackageJson from '@site/src/components/PackageJson'; # Build a Collaborative Todo App with Nuxt and Web5 In the [Build a Todo App](/docs/web5/build/apps/todo-app-tutorial) tutorial, we learnt how to build a basic single-user decentralized todo app with `web5.js` and `Vue.js`. For this tutorial, we will be expanding on the core features introduced with the todo app and build out a more collaborative app that allows multiple users work on the same, shared todo list. ## What we will learn We are going to learn how to; - Enforce permissions or authorization on records through protocols. - Send and update records across multiple users' Decentralized Web Nodes. By the end of this tutorial, we'll have an understanding of protocols, DWN to DWN communication and how to build a multi-user app on the Decentralized Web. ## What we will build Bob and Alice are two friends that love doing things together. They often have specific goals and a bunch of tasks each of them have to complete to achieve this goal. They have decided to build a todo app that allows them to collaborate on their tasks and keep track of their progress while also being able to have these tasks on other apps they use if needed - A decentralized shared todo app. ## Our toolkit We will be using the following tools to build our app: - [NuxtJS](https://nuxtjs.org/) - A VueJS framework for building web applications. - [Web5 SDK](https://www.npmjs.com/package/@web5/api) - A JavaScript SDK for building decentralized web apps. - [TailwindCSS](https://tailwindcss.com/) - A utility-first CSS framework for rapidly building custom designs. ## Getting the Skeleton App You can follow along with this tutorial using your own UI layout, or you can use the starter project. As you go through the tutorial, you can add logic to the methods and update the UI. To access the starter code, follow the instructions below or explore the project directly in CodeSandbox. [![Play in CodeSandbox](https://assets.codesandbox.io/github/button-edit-lime.svg)](https://codesandbox.io/p/sandbox/github/TBD54566975/developer.tbd.website/tree/main/examples/tutorials/shared-todo-starter) <details><summary>Finished Shared Todo App</summary> <p> If you’d like to skip ahead and see the finished version of this tutorial, you can check out the running app on CodeSandbox. [![Play in CodeSandbox](https://assets.codesandbox.io/github/button-edit-lime.svg)](https://codesandbox.io/p/sandbox/github/TBD54566975/developer.tbd.website/tree/main/examples/tutorials/shared-todo-completed) </p> </details> ## Setting up the App Add `Web5` to package.json in the `dependencies` section: <PackageJson /> Install the dependencies and get the project running: ```bash npm install npm run dev ``` You should now have the app running on [http://localhost:3000](http://localhost:3000). ## App Architecture We are building a [NuxtJS App](https://nuxt.com/) using file-based routing through the `pages` folder. We have the following key files in our app: - `pages/index.vue` - The home page of the app. - `pages/todos/_id.vue` - The page for viewing a todo list. - `assets/shared-todo-protocol.json`: A JSON file defining the protocol used for sharing to-do lists. Our shared todo app will have the following features that we need to build out: - Creating a todo list with another user. - Displaying all todo lists with different users. - Adding tasks to a todo list. - Marking tasks as completed. ## Initialize Constants and Variables Open `pages/index.vue` and add the following code to the other imports: ```js import { Web5 } from "@web5/api"; ``` Copy the following block and paste under all `import` statements in `pages/index.vue`: ```js let web5; let myDid; // The lists of existing todo lists const sharedList = ref([]); ``` The `web5` object is the single entry point for all Web5 operations. You've also set up a `sharedList` array to hold our shared lists, and a variable to remember the app user's decentralized identifier (DID) as `myDid`. ## Initialize Web5 The first time a user accessess your Shared Todo app, you’ll need to handle creating an “account” for them - this means ensuring they have a DID and DWN available to access their app data. Once they come back for subsequent sessions, you’ll want to fetch and load that data for them. Add to `pages/index.vue`: ```js onBeforeMount(async () => { ({ web5, did: myDID } = await Web5.connect()); }); ``` ## Setting Permissions and authorizations - Configuring protocols [Protocols](docs/web5/learn/protocols) help define both the data schema and the data permissions as it relates to a certain application or use case. It details what objects are stored as part of the application and who has what permissions on these objects. Create a new file called `shared-todo-protocol.json` under the `assets/` folder. ```json // assets/shared-todo-protocol.json { "protocol": "https://didcomm.org/shared-todo", "published": true, "types": { "list": { "schema": "https://didcomm.org/shared-todo/schemas/list.json", "dataFormats": ["application/json"] }, "todo": { "schema": "https://didcomm.org/shared-todo/schemas/todo.json", "dataFormats": ["application/json"] } }, "structure": { "list": { "$actions": [ { "who": "anyone", "can": "read" }, { "who": "anyone", "can": "write" } ], "todo": { "$actions": [ { "who": "author", "of": "list", "can": "write" }, { "who": "recipient", "of": "list", "can": "write" } ] } } } } ``` Here we are defining the objects that we want to use in our app - `list` and `todo` with who has what permissions: - Anyone can read and write a list. - The author and recipient of a list can write to the todo list. ### Installing the protocol To implement this protocol, we have to install it in our app for any of the users. In `pages/index.vue`, Add the protocol from the json. ```js import sharedTodoProtocol from "~/assets/shared-todo-protocol.json"; ``` Now we create the function that checks if the protocol is installed and installs it if it isn't. ```js const configureProtocol = async () => { const { protocols, status } = await web5.dwn.protocols.query({ message: { filter: { protocol: protocolDefinition.protocol, } } }); if(status.code !== 200) { alert('Error querying protocols'); console.error('Error querying protocols', status); return; } if(protocols.length > 0) { console.log('Protocol already exists'); return; } // configure protocol on local DWN const { status: configureStatus, protocol } = await web5.dwn.protocols.configure({ message: { definition: protocolDefinition, } }); console.log('Protocol configured', configureStatus, protocol); // configuring protocol on remote DWN const { status: configureRemoteStatus } = protocol.send(myDID); console.log('Protocol configured on remote DWN', configureRemoteStatus); } ``` - First we query the list of existing protocols on the DWN. - If the protocol already exists, we return. - If the protocol does not exist, we configure it on the local DWN and send it to the remote DWN. Once this is done, we add the `configureProtocol` function to the `onBeforeMount` function. ```js onBeforeMount(async () => { ({ web5, did: myDID } = await Web5.connect()); await configureProtocol() }); ``` ## Create new Shared Lists Right now, we have no existing lists, so we need to create one. We can add the following variable declarations to `pages/index.vue`: ```js const newTodo = ref({ title: '', description: '', recipientDID: '', }); const sharedList = ref([]); ``` Now we proceed to create the `createSharedList` function. ```js // pages/index.vue <script setup> ... const createSharedList = async () => { let recipientDID = newTodo.value.recipientDID; const sharedListData = { "@type": "list", "title": newTodo.value.title, "description": newTodo.value.description, "author": myDID, "recipient": newTodo.value.recipientDID, } newTodo.value = { title: '', description: '', userId: '', alias: '' } try { const { record } = await web5.dwn.records.create({ data: sharedListData, message: { protocol: protocolDefinition.protocol, protocolPath: 'list', schema: protocolDefinition.types.list.schema, dataFormat: protocolDefinition.types.list.dataFormats[0], recipient: recipientDID } }); const data = await record.data.json(); const list = {record, data, id: record.id}; sharedList.value.push(list); showForm.value = false const { status: sendStatus } = await record.send(recipientDID); if (sendStatus.code !== 202) { console.log("Unable to send to target did:" + sendStatus); return; } else { console.log("Shared list sent to recipient"); } } catch (e) { console.error(e); return; } } ... </script> ``` To break this down, we are creating a new list with the following properties: - `@type`: The type of object we are creating. In this case, it is a list. - `title`: The title of the list. - `description`: The description of the list. - `author`: The DID of the author of the list. - `recipient`: The DID of the recipient of the list. Then we pass all this to our `web5.dwn.records.create` function to create the list. Along with the data, we have to pass the protocol definition to the function. This ensures all our permissions and structure is enforced on the data we pass. Now we have to ensure that the recipient also receives this new list data. And for that, we use `record.send()`. ```js const { status: sendStatus } = await record.send(recipientDID); ``` But how do users get to easily copy and share their own DIDs with other users? We are going to implement a Copy DID feature. ```js const copyDID = async() => { await navigator.clipboard.writeText(myDID); alert('DID copied to clipboard'); } ``` And in the `<template>` section, we update the header to include the copy DID button. ```html <header class="flex-col text-center mb-4"> <h1 class="text-2xl font-bold mb-4">Shared Todo</h1> <p>Manage a set of todos towards your goals with friends.</p> <button v-if="myDID" class="btn" id="copy-did" @click="copyDID">Copy your DID</button> </header> ``` ## Displaying all todo lists with different users Using our `onBeforeMount()` method, we've been able to initialize web5, and configure our protocol. We also use this method to fetch existing lists from the DWN. ```js // pages/index.vue onBeforeMount(async () => { ... // Fetch shared todo lists. const{ records } = await web5.dwn.records.query({ message: { filter: { schema: protocolDefinition.types.list.schema, }, dateSort: 'createdAscending' } }); console.log("Saved records", records); // add entry to sharedList for (let record of records) { const data = await record.data.json(); const list = {record, data, id: record.id}; sharedList.value.push(list); } ... }); ``` We are querying the DWN for all records that match the schema of our protocol. We then add each record to our `sharedList` array. We have completed the first feature of our app. We can now create todolists and share them with another user. ## Adding new tasks The next part of our project involves adding specific todos to each list. We want both participants of the list to be able to add todos, but only the author of each task can mark it as done. This behaviour is enforced by the protocols that we added earlier. Using NuxtLink, we are able to navigate to our list page by passing the `listId` as a url paramete e.g `localhost:3000/todos/[id]`. This is why we have `pages/todos/[id].vue` in our project to dynamically render based on the list we open. ### Fetching specific list details In our dynamic todo page, we can reconnect to Web5 by importing both the sdk and protocolDefinition document again so they can be reused. ```js import { Web5 } from "@web5/api"; import sharedTodoProtocol from "~/assets/shared-todo-protocol.json"; ``` We need to fetch the list details from the DWN. We get the list Id from the url parameter, connect to Web5 and use the listId to query the DWN for the list. ```js const route = useRoute(); const listId = ref(route.params.id); let web5; let myDID; onBeforeMount(async () => { ({ web5, did: myDID } = await Web5.connect({ sync: '5s' })); console.log("this is your DID", myDID); // fetch shared list details. const { record } = await web5.dwn.records.read({ message: { recordId: listId.value } }) todoList = await record.data.json(); fetchingListInfo.value = false; }); ``` We update the `todoList` variable with the data from the DWN which we already have showing up on our page. ### Fetching tasks from a todo list Every list comes with it's own tasks. And we can fetch this also in the `onBeforeMount()` method. ```js onBeforeMount(async () => { ({ web5, did: myDID } = await Web5.connect({ sync: '5s' })); console.log("this is your DID", myDID); // fetch shared list details. const { record } = await web5.dwn.records.read({ message: { recordId: listId.value } }) // fetch todos under list. const { records: todoRecords } = await web5.dwn.records.query({ message: { filter: { parentId: listId.value }, } }) todoList = await record.data.json(); // Add entry to ToDos array for (let record of todoRecords) { const data = await record.data.json(); const todo = { record, data, id: record.id }; todoItems.value.push(todo); } fetchingListInfo.value = false; }); ``` ### Adding tasks to the todolist To add a task, we want to connect it to the list as the parent, identify who authored it, store it with the schema matching the protocol and then also send it to the other party's DWN. To get correct DID of which party to send the record to, since either of the author or recipient of the list could be the one creating the todo, we use a `getTodoRecipient()` to help us out. ```js // pages/todos/[id].vue let todoRecipient; const getTodoRecipient = (list) => { if (list.author === myDID) { return list.recipient; } else { return list.author; } } ``` Then in our `onBeforeMount() method`, we update the `todoRecipient` variable so that it's only updated when we've fetched the list details and gotten the participating DIDs. ```js onBeforeMount(async () => { ... todoList = await record.data.json(); todoRecipient = await getTodoRecipient(); ... }); ``` ### Creating the new Todo We create a new todo by calling the `web5.dwn.records.create()` method. We pass the data we want to store, the protocol definition and the recipient DID. ```js async function addTodo() { const todoData = { completed: false, description: newTodoDescription.value, author: myDID, parentId: listId.value }; newTodoDescription.value = ''; const { record: todoRecord, status: createStatus } = await web5.dwn.records.create({ data: todoData, message: { protocol: protocolDefinition.protocol, protocolPath: 'list/todo', schema: protocolDefinition.types.todo.schema, dataFormat: protocolDefinition.types.todo.dataFormats[0], parentId: listId.value, contextId: listId.value, } }); const data = await todoRecord.data.json(); const todo = { todoRecord, data, id: todoRecord.id }; todoItems.value.push(todo); const { status: sendStatus } = await todoRecord.send(todoRecipient); if (sendStatus.code !== 202) { console.log("Unable to send to target did:" + sendStatus); return; } else { console.log("Sent todo to recipient"); } } ``` ## Toggling Todo status as completed We want to be able to toggle the status of a todo as completed. We can do this by updating the `completed` property of the todo and sending it to the other party. ```js async function toggleTodoComplete(todoItem) { let toggledTodo; let updatedTodoData; for (let todo of todoItems.value) { if (todoItem.id === todo.id) { toggledTodo = todo; todo.data.completed = !todo.data.completed; updatedTodoData = { ...toRaw(todo.data) }; break; } } // Get record in DWN const { record } = await web5.dwn.records.read({ message: { recordId: toggledTodo.id, } }); // Update the record in DWN await record.update({ data: updatedTodoData }); const { status: sendStatus } = await record.send(todoRecipient); if (sendStatus.code !== 202) { console.log("Unable to send updated data to target did:", sendStatus); return; } else { console.log("Sent todo update to recipient"); } } ``` Congratulations! We've just built a multi-paged, collaborative and decentralized web app that can be used by multiple users. You can check out the finished version of the app running on CodeSandbox. [![Play in CodeSandbox](https://assets.codesandbox.io/github/button-edit-lime.svg)](https://codesandbox.io/p/sandbox/github/TBD54566975/developer.tbd.website/tree/main/examples/tutorials/shared-todo-completed)

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully