3box-profile-edit-react 中文文档教程

发布于 3年前 浏览 48 更新于 3年前

不和谐”></a> 
  <a href=npm Twitter Follow

3Box Profile Edit Plugin ????

3box-profile-edit-react 节点包是一个嵌入式 React 组件,它为 Web3 开发人员提供 UI 和逻辑,用于在他们的用户配置文件中编辑用户配置文件以太坊应用。 使用 texttextareaselect 选项,在基本输入字段之上快速添加与您的 dapp 相关的可自定义字段(下拉菜单) 样式输入。 3Box Edit Profile 插件是使用 3Box 基础架构构建的。 阅读 docs.3box.io 上的文档。

Try the demo here

示例截图

How it Works

Architecture

Edit Profile 插件实现了简单的set & Profiles APIget 方法.3box.io/api/storage">Spaces API 用于标准字段(nameimagecoverPhoto表情符号description)取决于用户选择的默认配置文件(3BoxApp),但只会使用开发人员添加的自定义字段的 Spaces API 作为 prop 传递给组件。 这两种方法都可以通过 3Box.js SDK 获得。 在用户设置了他们想要使用的默认配置文件后,通过在空间的公共存储中为键 isAppProfileDefault 设置一个布尔值,dapp 应该从 dapp 的其他地方适当地获取该配置文件。

Authentication

在第一次渲染 Edit Profile 组件之前,dapp 应该已经运行了 const box = await Box.openBox(userAddress, ethereumProvider)const space = await box.openSpace(spaceName)< /code> 并将 boxspace 对象作为同名的道具传递。

Getting Started

  1. Install the component
  2. Configure application settings and props to component
  3. Usage
  4. Display correct profile in your app

1. Install the component

npm i -S 3box-profile-edit-react

2. Configure application settings and props to component

首先,为您的应用程序的 3Box 空间选择一个名称。

虽然您可以自由地为您的应用程序空间选择您喜欢的任何名称,但我们建议使用您的应用程序的名称。 如果您的应用程序已经有一个 3Box 空间,欢迎您将同一个空间用于聊天框。

然后,配置要添加到组件的自定义字段

customFields 属性必须接收一个数组以下结构中的 任意 个对象:

    { // for a field with a text input
        inputType: 'text',
        key: 'backupAddress', // the key used to save the value
        field: 'Backup Address' // how to display the key in the UI
      }, 

      { // for a field with a textarea input
        inputType: 'textarea',
        key: 'spiritCryptoKittie',
        field: 'Spirit CryptoKitty'
      },

      { // for a field with a dropdown input
        inputType: 'dropdown',
        key: 'preferredCoin',
        field: 'Preferred Coin',
        options: [{ // dropdown input requires an array of objects
          value: 'eth', // value passed after selection
          display: 'Ethereum' // what the user will see in the dropdown
        }, {
          value: 'btc',
          display: 'Bitcoin'
        }, {
          value: 'ltc',
          display: 'Litecoin'
        }]
      }

Edit Profile 组件标配 NameEmojiDescription 字段。 该组件默认使用 3Box 配置文件(因为空间中的 isAppProfileDefault 布尔值仍未定义)。 然而,传递给 customFields 的字段将始终保存到应用程序的空间中。



3. Usage

编辑配置文件组件当然可以以开发人员喜欢的任何方式使用,但我们建议将其用于开发人员设计的专用编辑配置文件页面或弹出模式(未包含)中。 请记住,boxspace 实例都必须在安装之前传递给组件。

Example

import EditProfile from '3box-profile-edit-react';

const MyComponent = ({ customFields, box, space, myAddress, myProfile, redirectFn }) => (
    <EditProfile
        // required
        box={box}
        space={space}
        currentUserAddr={myAddress}

        // optional
        customFields={customFields}
        currentUser3BoxProfile={myProfile}
        redirectFn={redirectFn}
    />
);

4. Display correct profile in your app

一旦用户选择了默认配置文件(3Box 或应用程序),该决定应反映在您的 dapp 的其他地方。 为此,您应该检查 isAppProfileDefault 标志,然后在字段 nameimage 的相应配置文件上调用 getcoverPhotoemojidescription。 但是,添加到“编辑配置文件”组件的自定义字段都保存到您的 dapp 使用的 space 中的应用程序配置文件中,应该从那里获取

检查并从适当的默认配置文件中获取应该看起来像这样:

  const isAppProfileDefault = await space.public.get('isAppProfileDefault');

  let profile;
  if (isAppProfileDefault) {
    profile = await Box.getProfile(currentUserAddr);
  } else {
    profile = await space.public.all();
  }

  this.setState({
    name: profile.name,
    image: profile.image,
    coverPhoto: profile.coverPhoto,
    emoji: profile.emoji,
    description: profile.description,
  })

Prop Types

Property Type Required Description
box Object True The box instance returned from running await Box.openBox(address, web3) somewhere in your dApp.
space Object True The space instance returned from running await box.openSpace(spaceName) somewhere in your dApp.
currentUserAddr String (Ethereum Address) True The current user's Ethereum address. Used to fetch the user's profile if it's not provided and for other various uses in the component.
customFields Array An array of any amount of objects structured in one of three ways outlined above.
currentUser3BoxProfile Object If passed, it must be the object returned from calling const currentUser3BoxProfile = await Box.getProfile(myAddress);. If this is not passed, the component runs the same static method using the currentUserAddr prop
redirectFn Function A function that runs after the user hits cancel. If no redirect function has been provided, then the cancel button will not be present in the form. The user's ethereum address will be passed as a param in the event that the dapp uses this in the route
onSaveComplete Function A function that runs after all updated fields in the form have been saved. You may also pass your redirect function to this prop should you choose to. If no function is passed to this prop, the form will save as usual.

License

麻省理工学院

Discord npm Twitter Follow

3Box Profile Edit Plugin ????

The 3box-profile-edit-react node package is a drop-in react component that provides Web3 developers with UI and logic for editing user profiles in their Ethereum application. Quickly add customizable fields that are pertinent to your dapp on top of basic input fields, with the option of text, textarea, and select (dropdown) style inputs. The 3Box Edit Profile plugin is built using 3Box infrastructure. Read the docs on docs.3box.io.

Try the demo here

Example Screenshot

How it Works

Architecture

The Edit Profile plugin implements simple set & get methods on both the Profiles API and the Spaces API for standard fields (name, image, coverPhoto, emoji, description) depending on which profile the user chooses as their default (3Box or App), but will use only the Spaces API for custom fields added by the developer passed as a prop to the component. Both methods are made available via the 3Box.js SDK. After a user has set which default profile they'd like to use, by setting a boolean to the key isAppProfileDefault in the space's public store, the dapp should appropriately fetch from that profile elsewhere in the dapp.

Authentication

Prior to first render of the Edit Profile component, the dapp should have already run const box = await Box.openBox(userAddress, ethereumProvider) and const space = await box.openSpace(spaceName) and passed both the box and space objects as props of the same name.

Getting Started

  1. Install the component
  2. Configure application settings and props to component
  3. Usage
  4. Display correct profile in your app

1. Install the component

npm i -S 3box-profile-edit-react

2. Configure application settings and props to component

First, choose a name for your application's 3Box space.

Although you are free to choose whichever name you'd like for your app's space, we recommend using the name of your app. If your application already has a 3Box space, you are welcome to use that same one for the chatbox.

Then, configure the custom fields you'd like to add to the component

The customFields prop must receive an array of any number of objects in the following structures:

    { // for a field with a text input
        inputType: 'text',
        key: 'backupAddress', // the key used to save the value
        field: 'Backup Address' // how to display the key in the UI
      }, 

      { // for a field with a textarea input
        inputType: 'textarea',
        key: 'spiritCryptoKittie',
        field: 'Spirit CryptoKitty'
      },

      { // for a field with a dropdown input
        inputType: 'dropdown',
        key: 'preferredCoin',
        field: 'Preferred Coin',
        options: [{ // dropdown input requires an array of objects
          value: 'eth', // value passed after selection
          display: 'Ethereum' // what the user will see in the dropdown
        }, {
          value: 'btc',
          display: 'Bitcoin'
        }, {
          value: 'ltc',
          display: 'Litecoin'
        }]
      }

The Edit Profile component comes standard with Name, Emoji, and Description fields. The component defaults to using the 3Box profile (as the isAppProfileDefault boolean in the space is still undefined). Fields passed to customFields however will always be saved to the app's space.



3. Usage

The Edit Profile component can of course be used in any way the developer pleases, though we recommend it be used on a dedicated edit profile page or in a popup modal (not included) of the developer's design. Remember, both the box and space instance must be passed to the component before it mounts.

Example

import EditProfile from '3box-profile-edit-react';

const MyComponent = ({ customFields, box, space, myAddress, myProfile, redirectFn }) => (
    <EditProfile
        // required
        box={box}
        space={space}
        currentUserAddr={myAddress}

        // optional
        customFields={customFields}
        currentUser3BoxProfile={myProfile}
        redirectFn={redirectFn}
    />
);

4. Display correct profile in your app

Once a user selects a default profile (3Box or app) the decision should reflect elsewhere in your dapp. To do this, you should check isAppProfileDefault flag, then call get on the respective profile for fields name, image, coverPhoto, emoji, and description. Custom fields added to the Edit Profile component, however, are all saved to the application profile in the space used by your dapp and should get from there.

Checking and getting from the appropriate default profile should look something like this:

  const isAppProfileDefault = await space.public.get('isAppProfileDefault');

  let profile;
  if (isAppProfileDefault) {
    profile = await Box.getProfile(currentUserAddr);
  } else {
    profile = await space.public.all();
  }

  this.setState({
    name: profile.name,
    image: profile.image,
    coverPhoto: profile.coverPhoto,
    emoji: profile.emoji,
    description: profile.description,
  })

Prop Types

Property Type Required Description
box Object True The box instance returned from running await Box.openBox(address, web3) somewhere in your dApp.
space Object True The space instance returned from running await box.openSpace(spaceName) somewhere in your dApp.
currentUserAddr String (Ethereum Address) True The current user's Ethereum address. Used to fetch the user's profile if it's not provided and for other various uses in the component.
customFields Array An array of any amount of objects structured in one of three ways outlined above.
currentUser3BoxProfile Object If passed, it must be the object returned from calling const currentUser3BoxProfile = await Box.getProfile(myAddress);. If this is not passed, the component runs the same static method using the currentUserAddr prop
redirectFn Function A function that runs after the user hits cancel. If no redirect function has been provided, then the cancel button will not be present in the form. The user's ethereum address will be passed as a param in the event that the dapp uses this in the route
onSaveComplete Function A function that runs after all updated fields in the form have been saved. You may also pass your redirect function to this prop should you choose to. If no function is passed to this prop, the form will save as usual.

License

MIT

更多

友情链接

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文