github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/sawtooth-supply-chain-master/fish_client/src/views/list_agents.js (about)

     1  /**
     2   * Copyright 2017 Intel Corporation
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   * ----------------------------------------------------------------------------
    16   */
    17  'use strict'
    18  
    19  const m = require('mithril')
    20  const sortBy = require('lodash/sortBy')
    21  const truncate = require('lodash/truncate')
    22  const {Table, FilterGroup, PagingButtons} = require('../components/tables.js')
    23  const api = require('../services/api')
    24  
    25  const PAGE_SIZE = 50
    26  
    27  const AgentList = {
    28    oninit (vnode) {
    29      vnode.state.agents = []
    30      vnode.state.filteredAgents = []
    31      vnode.state.currentPage = 0
    32  
    33      const refresh = () => {
    34        api.get('agents').then((agents) => {
    35          vnode.state.agents = sortBy(agents, 'name')
    36          vnode.state.filteredAgents = vnode.state.agents
    37        })
    38          .then(() => { vnode.state.refreshId = setTimeout(refresh, 2000) })
    39      }
    40  
    41      refresh()
    42    },
    43  
    44    onbeforeremove (vnode) {
    45      clearTimeout(vnode.state.refreshId)
    46    },
    47  
    48    view (vnode) {
    49      let publicKey = api.getPublicKey()
    50      return [
    51        m('.agent-list',
    52          m('.row.btn-row.mb-2', _controlButtons(vnode, publicKey)),
    53          m(Table, {
    54            headers: [
    55              'Name',
    56              'Key',
    57              'Owns',
    58              'Custodian',
    59              'Reports'
    60            ],
    61            rows: vnode.state.filteredAgents.slice(
    62                vnode.state.currentPage * PAGE_SIZE,
    63                (vnode.state.currentPage + 1) * PAGE_SIZE)
    64              .map((agent) => [
    65                m(`a[href=/agents/${agent.key}]`, { oncreate: m.route.link },
    66                  truncate(agent.name, { length: 32 })),
    67                truncate(agent.key, { length: 32 }),
    68                agent.owns.length,
    69                agent.custodian.length,
    70                agent.reports.length
    71              ]),
    72            noRowsText: 'No agents found'
    73          })
    74       )
    75      ]
    76    }
    77  }
    78  
    79  const _controlButtons = (vnode, publicKey) => {
    80    if (publicKey) {
    81      let filterAgents = (f) => {
    82        vnode.state.filteredAgents = vnode.state.agents.filter(f)
    83      }
    84  
    85      return [
    86        m('.col-sm-8',
    87          m(FilterGroup, {
    88            ariaLabel: 'Filter Based on Ownership',
    89            filters: {
    90              'All': () => { vnode.state.filteredAgents = vnode.state.agents },
    91              'Owners': () => filterAgents(agent => agent.owns.length > 0),
    92              'Custodians': () => filterAgents(agent => agent.custodian.length > 0),
    93              'Reporters': () => filterAgents(agent => agent.reports.length > 0)
    94            },
    95            initialFilter: 'All'
    96          })),
    97        m('.col-sm-4', _pagingButtons(vnode))
    98      ]
    99    } else {
   100      return [
   101        m('.col-sm-4.ml-auto', _pagingButtons(vnode))
   102      ]
   103    }
   104  }
   105  
   106  const _pagingButtons = (vnode) =>
   107    m(PagingButtons, {
   108      setPage: (page) => { vnode.state.currentPage = page },
   109      currentPage: vnode.state.currentPage,
   110      maxPage: Math.floor(vnode.state.filteredAgents.length / PAGE_SIZE)
   111    })
   112  
   113  module.exports = AgentList