Polka宠物项目5 UI界面--by Skyh0805

现在编写前端部分, 首先第一篇https://www.jianshu.com/p/cef8e73f103d 创建了substratekitties-ui, 现在就用这个ui 实现前端展示,并熟悉polkadot的js接口,

1 前端运行

安装依赖和运行

yarn install
yarn run dev
image.png

浏览器运行http://localhost:8000

image.png

熟悉开发者工具的可以在命令行运行如下


image.png

其实跟浏览器一样


image.png

2. 第一步尝试

找到app.jsx 在末尾加入

class SubstratekittiesSegment extends React.Component {
    constructor() {
        super()

        this.skAccount = new Bond
        this.skKitty1 = new Bond
        this.skKitty2 = new Bond
    }

    render() {
        return <Segment style={{ margin: '1em' }} padded>
            <Header as='h2'>
                <Icon name='paw' />
                <Header.Content>
                    Substrate Kitties
                    <Header.Subheader>There are <Pretty value={runtime.substratekitties.allKittiesCount} /> kitties purring.</Header.Subheader>
                </Header.Content>
            </Header>
            <div style={{ paddingBottom: '1em' }}></div>
        </Segment>
    }
}

然后在前面加入组件


image.png

重新打开网页就看到链上内容了


image.png

我们可以创建一个 Call 来创建一个新的 kitty。为此,我们可以向 runtime 发出 post() 请求,如下所示:

post({
    sender: '5GoKvZWG5ZPYL1WUovuHW3zJBWBP5eT8CbqjdRY4Q6iMaDtZ',
    call: calls.substratekitties.createKitty()
}).tie(console.log)

查看有什么方法


image.png

decl_module! 宏中创建的所有函数的列表。请注意,我们的私有函数如 mint() 和 _transfer() 不在此处,因为它们不应该是我们公共 API 的一部分。

3 UI组件

SignerBond 创建一个输入区域,在里面每个人可以写入他们想要签名的帐户名称。该账户被置于 bond 内
TransactButton 提供支持,我们将提供存储在 bond 中的值给交易的 sender 字段

可修改app.jsx组件为

class SubstratekittiesSegment extends React.Component {
    constructor() {
        super()

        this.skAccount = new Bond
    }

    render() {
        return <Segment style={{ margin: '1em' }} padded>
            <Header as='h2'>
                <Icon name='paw' />
                <Header.Content>
                    Substrate Kitties
                <Header.Subheader>There are <Pretty value={runtime.substratekitties.allKittiesCount} /> kitties purring.</Header.Subheader>
                </Header.Content>
            </Header>
            <div style={{ paddingBottom: '1em' }}></div>
            <SignerBond bond={this.skAccount} />
            <TransactButton
                content="Create Kitty"
                icon='paw'
                tx={{
                    sender: runtime.indices.tryIndex(this.skAccount),
                    call: calls.substratekitties.createKitty()
                }}
            />
        </Segment>
    }

可看到ui修改为


image.png

另外下载一个组件 https://github.com/substrate-developer-hub/substrate-collectables-workshop/raw/master/4/assets/KittyCards.zip
放在src处, 然后引入组件, 放在源码内

//在 constructor() { 加入
...
addCodecTransform('Kitty<Hash,Balance>', {
            id: 'Hash',
            dna: 'Hash',
            price: 'Balance',
            gen: 'u64'
        });
...

就可以看到

注意, 里面名字必须出现在wallet内, alice没有就不可以, 比如创建一个new, 然后转入unit, 就可以在这个账户创建kitty了

image.png

这集讲了如何在ui加入, 使用react, 其他项目类似, 具体源码可以参考
polka runtime创建宠物项目就告一段落了

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • React.js - 第1天 备注说明:该笔记来自于黑马培训课笔记,因为质量好,为方便学习和 查阅。 1. Rea...
    达魔学院阅读 525评论 0 0
  • 1. React简介 React 起源于 Facebook 的内部项目,因为该公司对市场上所有 JavaScrip...
    午夜阳光5021阅读 334评论 0 1
  • 1. 添加模块 分析项目结构 另外要在lib.rs三处加入模块, 我看到有类似template模块就在下面加了 这...
    skyh25阅读 630评论 1 0
  • 原教程内容详见精益 React 学习指南,这只是我在学习过程中的一些阅读笔记,个人觉得该教程讲解深入浅出,比目前大...
    leonaxiong阅读 2,945评论 1 18
  • 为了清明节苏州的骑行 今天持续骑行8小时 膝盖痛的啪啪响 最后又累又痛在白塔公园的樱花树下睡了好久 醒来脸上身上全...
    毛欣与小李阅读 173评论 0 0

友情链接更多精彩内容