github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/sawtooth-supply-chain-master/fish_client/src/main.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  // These requires inform webpack which styles to build
    20  require('bootstrap')
    21  require('../styles/main.scss')
    22  
    23  const m = require('mithril')
    24  
    25  const api = require('./services/api')
    26  const transactions = require('./services/transactions')
    27  const navigation = require('./components/navigation')
    28  
    29  const AddFishForm = require('./views/add_fish_form')
    30  const AgentDetailPage = require('./views/agent_detail')
    31  const AgentList = require('./views/list_agents')
    32  const FishList = require('./views/list_fish')
    33  const FishDetail = require('./views/fish_detail')
    34  const Dashboard = require('./views/dashboard')
    35  const LoginForm = require('./views/login_form')
    36  const PropertyDetailPage = require('./views/property_detail')
    37  const SignupForm = require('./views/signup_form')
    38  
    39  /**
    40   * A basic layout component that adds the navbar to the view.
    41   */
    42  const Layout = {
    43    view (vnode) {
    44      return [
    45        vnode.attrs.navbar,
    46        m('.content.container', vnode.children)
    47      ]
    48    }
    49  }
    50  
    51  const loggedInNav = () => {
    52    const links = [
    53      ['/create', 'Add Fish'],
    54      ['/fish', 'View Fish'],
    55      ['/agents', 'View Agents']
    56    ]
    57    return m(navigation.Navbar, {}, [
    58      navigation.links(links),
    59      navigation.link('/profile', 'Profile'),
    60      navigation.button('/logout', 'Logout')
    61    ])
    62  }
    63  
    64  const loggedOutNav = () => {
    65    const links = [
    66      ['/fish', 'View Fish'],
    67      ['/agents', 'View Agents']
    68    ]
    69    return m(navigation.Navbar, {}, [
    70      navigation.links(links),
    71      navigation.button('/login', 'Login/Signup')
    72    ])
    73  }
    74  
    75  /**
    76   * Returns a route resolver which handles authorization related business.
    77   */
    78  const resolve = (view, restricted = false) => {
    79    const resolver = {}
    80  
    81    if (restricted) {
    82      resolver.onmatch = () => {
    83        if (api.getAuth()) return view
    84        m.route.set('/login')
    85      }
    86    }
    87  
    88    resolver.render = vnode => {
    89      if (api.getAuth()) {
    90        return m(Layout, { navbar: loggedInNav() }, m(view, vnode.attrs))
    91      }
    92      return m(Layout, { navbar: loggedOutNav() }, m(view, vnode.attrs))
    93    }
    94  
    95    return resolver
    96  }
    97  
    98  /**
    99   * Clears user info from memory/storage and redirects.
   100   */
   101  const logout = () => {
   102    api.clearAuth()
   103    transactions.clearPrivateKey()
   104    m.route.set('/')
   105  }
   106  
   107  /**
   108   * Redirects to user's agent page if logged in.
   109   */
   110  const profile = () => {
   111    const publicKey = api.getPublicKey()
   112    if (publicKey) m.route.set(`/agents/${publicKey}`)
   113    else m.route.set('/')
   114  }
   115  
   116  /**
   117   * Build and mount app/router
   118   */
   119  document.addEventListener('DOMContentLoaded', () => {
   120    m.route(document.querySelector('#app'), '/', {
   121      '/': resolve(Dashboard),
   122      '/agents/:publicKey': resolve(AgentDetailPage),
   123      '/agents': resolve(AgentList),
   124      '/create': resolve(AddFishForm, true),
   125      '/fish/:recordId': resolve(FishDetail),
   126      '/fish': resolve(FishList),
   127      '/login': resolve(LoginForm),
   128      '/logout': { onmatch: logout },
   129      '/profile': { onmatch: profile },
   130      '/properties/:recordId/:name': resolve(PropertyDetailPage),
   131      '/signup': resolve(SignupForm)
   132    })
   133  })