Ex11: Widgets Iterator

The function that we pass to augment the context can return either a single widget, or an array of widgets, or a null. The widgets in the array can be of the same or different types. The function itself can be either synchronous or asynchronous.

The initial code for this example is in master.

Let's create a few label-s for POST and button-s for PROFILE. We will use data.json as data. We will fetch nfts, which contain an image and text, in an asynchronous activate function.

1// /dapplet/src/index.ts > TwitterFeature
2async activate() {
3 const url = await Core.storage.get('dataUrl');
4 let nfts: NftMetadata[];
5 try {
6 nfts = await this._fetchNfts(url);
7 } catch (err) {
8 console.log('Error getting NFTs.', err)
9 }
10
11 const { label, button } = this.adapter.exports;
12 this.adapter.attachConfig({
13 POST: (ctx) =>
14 nfts && nfts.map((nft) =>
15 label({
16 DEFAULT: {
17 img: nft.image,
18 basic: true,
19 tooltip: ctx.authorFullname + "'s " + nft.text,
20 },
21 }),
22 ),
23 PROFILE: (ctx) =>
24 nfts && nfts.map((nft) =>
25 button({
26 DEFAULT: {
27 img: nft.image,
28 tooltip: ctx.authorFullname + "'s " + nft.text,
29 },
30 }),
31 ),
32 });
33}

Also add _fetchNfts function:

1private async _fetchNfts(url: string): Promise<NftMetadata[]> {
2 let res: any;
3 try {
4 res = await fetch(url);
5 } catch (err) {
6 console.log('Error fetching NFTs.', err)
7 }
8 return res.json();
9}

The complete code for this example can be found here: ex11-widgets-iterator.

Run the dapplet:

npm i
npm start