Giter Club home page Giter Club logo

substrate-kities-ui's Introduction

Substrate Kities

核心代码

https://github.com/zacksleo/substrate-kities-ui/blob/advanced-term-03/03_polkadot-js-api/kitties/frontend/src/Kitties.js#L16

  // 猫咪的 DNA
  const [dnas, setDnas] = useState([])
  // 猫咪的所有者
  const [owners, setOwners] = useState([])

  // 格式化 DNA··
  const formatDna = (dna) => dna.isNone ? {} : dna.value.toU8a();

  const fetchKitties = () => {
    // TODO: 在这里调用 `api.query.kittiesModule.*` 函数去取得猫咪的信息。
    // 你需要取得:
    //   - 共有多少只猫咪
    //   - 每只猫咪的主人是谁
    //   - 每只猫咪的 DNA 是什么,用来组合出它的形态
    // 查询猫咪的数量

    let unsubCount = null;
    let unsubKitty = null;
    let unsubOwner = null;

    const asyncFetch = async () => {
      unsubCount = await api.query.kittiesModule.kittiesCount(async (res) => {
        const total = res.isNone ? 0 : res.value.toNumber();
        const indexes = [...Array(total).keys()];

        unsubKitty = await api.query.kittiesModule.kitties.multi(indexes, kitties => {
          setDnas(kitties.map((dna, id) => formatDna(dna)));
        });

        unsubOwner = await api.query.kittiesModule.owner.multi(indexes, owners => {
          setOwners(owners.map(owner => owner.toHuman()));
        });
      });
    }

    asyncFetch();

    return () => {
      unsubCount && unsubCount() && unsubKitty && unsubKitty() && unsubOwner && unsubOwner()
    }
  }

  const populateKitties = () => {
    // TODO: 在这里添加额外的逻辑。你需要组成这样的数组结构:
    //  ```javascript
    //  const kitties = [{
    //    id: 0,
    //    dna: ...,
    //    owner: ...
    //  }, { id: ..., dna: ..., owner: ... }]
    //  ```
    // 这个 kitties 会传入 <KittyCards/> 然后对每只猫咪进行处理
    setKitties(dnas.map((dna, id) => ({ id, dna, owner: owners[id] })))
  }

  useEffect(fetchKitties, [api, keyring])
  useEffect(populateKitties, [dnas, owners])

Screenshots

Alice create kities

Alice transfer kity to bob

Kities of Bob

substrate-kities-ui's People

Contributors

jimmychu0807 avatar kaichaosun avatar sekisamu avatar zacksleo avatar xlc avatar miketang84 avatar juniuszhou avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.