github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/sawtooth-supply-chain-master/asset_client/src/views/list_assets.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 truncate = require('lodash/truncate')
    21  const {Table, FilterGroup, PagingButtons} = require('../components/tables')
    22  const api = require('../services/api')
    23  const { formatTimestamp } = require('../services/parsing')
    24  const {getPropertyValue, getLatestPropertyUpdateTime, getOldestPropertyUpdateTime, countPropertyUpdates} = require('../utils/records')
    25  
    26  const PAGE_SIZE = 50
    27  
    28  const AssetList = {
    29    oninit (vnode) {
    30      vnode.state.records = []
    31      vnode.state.filteredRecords = []
    32  
    33      vnode.state.currentPage = 0
    34  
    35      const refresh = () => {
    36        api.get('records?recordType=asset').then((records) => {
    37          vnode.state.records = records
    38          vnode.state.records.sort((a, b) => {
    39            return getLatestPropertyUpdateTime(b) - getLatestPropertyUpdateTime(a)
    40          })
    41          vnode.state.filteredRecords = vnode.state.records
    42        })
    43          .then(() => { vnode.state.refreshId = setTimeout(refresh, 2000) })
    44      }
    45  
    46      refresh()
    47    },
    48  
    49    onbeforeremove (vnode) {
    50      clearTimeout(vnode.state.refreshId)
    51    },
    52  
    53    view (vnode) {
    54      let publicKey = api.getPublicKey()
    55      return [
    56        m('.asset-table',
    57          m('.row.btn-row.mb-2', _controlButtons(vnode, publicKey)),
    58          m(Table, {
    59            headers: [
    60              'Serial Number',
    61              'Type',
    62              'Added',
    63              'Updated',
    64              'Updates'
    65            ],
    66            rows: vnode.state.filteredRecords.slice(
    67              vnode.state.currentPage * PAGE_SIZE,
    68              (vnode.state.currentPage + 1) * PAGE_SIZE)
    69                  .map((rec) => [
    70                    m(`a[href=/assets/${rec.recordId}]`, {
    71                      oncreate: m.route.link
    72                    }, truncate(rec.recordId, { length: 32 })),
    73                    getPropertyValue(rec, 'type'),
    74                    // This is the "created" time, synthesized from properties
    75                    // added on the initial create
    76                    formatTimestamp(getOldestPropertyUpdateTime(rec)),
    77                    formatTimestamp(getLatestPropertyUpdateTime(rec)),
    78                    countPropertyUpdates(rec)
    79                  ]),
    80            noRowsText: 'No records found'
    81          })
    82        )
    83      ]
    84    }
    85  }
    86  
    87  const _controlButtons = (vnode, publicKey) => {
    88    if (publicKey) {
    89      let filterRecords = (f) => {
    90        vnode.state.filteredRecords = vnode.state.records.filter(f)
    91      }
    92  
    93      return [
    94        m('.col-sm-8',
    95          m(FilterGroup, {
    96            ariaLabel: 'Filter Based on Ownership',
    97            filters: {
    98              'All': () => { vnode.state.filteredRecords = vnode.state.records },
    99              'Owned': () => filterRecords((record) => record.owner === publicKey),
   100              'Custodian': () => filterRecords((record) => record.custodian === publicKey),
   101              'Reporting': () => filterRecords(
   102                (record) => record.properties.reduce(
   103                  (owned, prop) => owned || prop.reporters.indexOf(publicKey) > -1, false))
   104            },
   105            initialFilter: 'All'
   106          })),
   107        m('.col-sm-4', _pagingButtons(vnode))
   108      ]
   109    } else {
   110      return [
   111        m('.col-sm-4.ml-auto', _pagingButtons(vnode))
   112      ]
   113    }
   114  }
   115  
   116  const _pagingButtons = (vnode) =>
   117    m(PagingButtons, {
   118      setPage: (page) => { vnode.state.currentPage = page },
   119      currentPage: vnode.state.currentPage,
   120      maxPage: Math.floor(vnode.state.filteredRecords.length / PAGE_SIZE)
   121    })
   122  
   123  module.exports = AssetList