import { Component, OnInit, ViewEncapsulation, AfterViewInit } from '@angular/core'; import { ActivatedRoute, Router } from '@angular/router'; import { Helpers } from '../../../../../helpers'; import { ScriptLoaderService } from '../../../../../_services/script-loader.service'; import Web3 from 'web3'; import { DetailsService } from '../../details.service'; import { GlobalsComponent } from '../../globals/globals.component'; declare var $: any; @Component({ selector: '.m-grid__item.m-grid__item--fluid.m-wrapper', templateUrl: './reputation.component.html', encapsulation: ViewEncapsulation.None }) export class ReputationComponent implements OnInit, AfterViewInit { // public web3; // public memberAddress; // public masterAddresses = []; // public reputationLedger = []; // public companyMasterAddress; // public companyNameArray = []; // public dAppNameArray = []; // public masterDappArray = {}; // public dAppMasterMap = {}; // public dAppRecords = {}; // public cSize; // public cCount = 0; // public noRecord = false; // public userObj; // public dAppReputation = {}; // public dApps = []; // public showLabel = {}; // public metamaskLogin = true; public web3; public memberAddress; public masterAddresses = []; public companyNameArray = []; public finalLedger = []; public rewardLedger = []; public dAppNameArray = []; public masterDappArray = {}; public dAppMasterMap = {}; public dAppRecords = {}; public dAppArray = []; public cSize; public cCount = 0; public noRecord = false; public userObj; public metamaskLogin = true; constructor( private _script: ScriptLoaderService, private detailService: DetailsService, private globals: GlobalsComponent, private router: Router ) {} ngOnInit() { this.detailService.setTitle('Reputation'); $('#filters').hide(); let user = localStorage.getItem('currentUser'); this.userObj = JSON.parse(user); this.web3 = new Web3(window['web3'].currentProvider); this.web3.eth.defaultAccount = this.web3.eth.coinbase; if (this.web3.eth.accounts[0] == undefined) { this.metamaskLogin = false; this.memberAddress = '0x0000000000000000000000000000000000000000'; } else { this.memberAddress = this.web3.eth.accounts[0]; } import('../../globals/globals.component').then(module => { module.generator(); }); var timeoutVar = setInterval( function() { if (GlobalsComponent.gbmaster) { this.getInitialAddress(); clearInterval(timeoutVar); } }.bind(this), 500 ); } // getInitialAddress() { // var length; // GlobalsComponent.gbmaster.getAlldAppRegistered( // function(error, result1) { // length = result1.length; // var i = 0; // var companyName; // this.cSize = length; // if (length == 0) { // this.demo(''); // } // for (i = 0; i < length; i++) { // var index = i; // this.dAppNameArray.push(this.hex_to_ascii(result1[index])); // GlobalsComponent.gbmaster.getdAppDetails( // this.dAppNameArray[index], // function(error, result) { // this.companyMasterAddress = result[1]; // if ( // result[5] > 0 && // !this.hex_to_ascii(result[0]).startsWith('TEST') // ) { // this.masterAddresses.push(this.companyMasterAddress); // this.memberAddress = this.memberAddress; // var cname = this.hex_to_ascii(result[0]); // this.dApps.push(cname); // this.companyNameArray.push({ name: cname }); // this.masterDappArray[this.companyMasterAddress] = cname; // this.dAppMasterMap[cname] = this.companyMasterAddress; // if (this.masterAddresses.length == this.cSize) // this.initialCalls(cname); // } else { // this.cSize--; // if (this.masterAddresses.length == this.cSize) // this.initialCalls(cname); // } // }.bind(this) // ); // } // }.bind(this) // ); // } // initialCalls(companyName) { // for (let i = 0; i < this.masterAddresses.length; i++) { // this.masterCall(companyName, i); // } // } // public countdAppsDisplayed = 0; // masterCall(companyName, i) { // var cname = this.masterDappArray[this.masterAddresses[i]]; // this.dAppRecords[cname] = 0; // GlobalsComponent.gbmaster.getReputationLedgerData( // cname, // this.memberAddress, // function(dAppName, resRep) { // this.reputationLedger = resRep; // if (resRep.length < 1) { // this.dAppReputation[dAppName] = []; // this.dAppReputation[dAppName].push({ // timeStamp: 0, // proposalId: '-', // blockNumber: '-', // proposalStatus: '-', // id: 1, // totalReputation: 1, // date: '-', // proposalName: '-', // desc: 'Initial Reputation of member in ' + dAppName, // reputation: 1, // cname: dAppName // }); // this.dAppReputation[dAppName] = this.dAppReputation[dAppName].concat( // resRep // ); // } else { // this.dAppRecords[resRep[0].cname] = 1; // this.dAppReputation[resRep[0].cname] = []; // this.dAppReputation[resRep[0].cname].push({ // timeStamp: 0, // proposalId: '-', // blockNumber: '-', // proposalStatus: '-', // id: 1, // totalReputation: 1, // date: '-', // proposalName: '-', // desc: 'Initial Reputation of member in ' + resRep[0].cname, // reputation: 1, // cname: resRep[0].cname // }); // this.dAppReputation[resRep[0].cname] = this.dAppReputation[ // resRep[0].cname // ].concat(resRep); // } // if (this.dAppReputation[this.dApps[i]].length > 1) { // this.countdAppsDisplayed++; // this.dAppReputation[this.dApps[i]].sort( // this.predicateBy('timeStamp') // ); // this.demo(this.dApps[i]); // } // if ( // Object.keys(this.dAppReputation).length == // Object.keys(this.masterDappArray).length && // this.countdAppsDisplayed == 0 // ) { // this.dAppReputation['GOVBLOCKS'] = []; // this.dAppReputation['GOVBLOCKS'].push({ // proposalId: '-', // blockNumber: '-', // proposalStatus: '-', // id: 1, // totalReputation: 1, // date: '-', // proposalName: '-', // desc: 'Initial Reputation of member in GOVBLOCKS', // reputation: 1, // cname: 'GOVBLOCKS' // }); // this.demo('GOVBLOCKS'); // } // }.bind(this) // ); // } // public total = 1; // public totalReputation = {}; // public total1 = 0; // hex_to_ascii(str1) { // var hex = str1.toString(); // var str = ''; // for (var n = 0; n < hex.length; n += 2) { // str += String.fromCharCode(parseInt(hex.substr(n, 2), 16)); // } // str1 = ''; // for (var i = 0; i < str.length; i++) { // if (str.charCodeAt(i) != 0) { // str1 += str[i]; // } // } // return str1; // } // ngAfterViewInit() { // // this.demo(); // } // predicateBy(prop) { // return function(a, b) { // if (a[prop] > b[prop]) { // return 1; // } else if (a[prop] < b[prop]) { // return -1; // } // return 0; // }; // } // onSelectProposal(obj) { // GlobalsComponent.gbmaster.isMemberAuthorized( // obj.cname, // this.memberAddress, // function(errorMR, resultMR) { // var authorized = true; // if (resultMR[1]) { // authorized = false; // } // var masterAddress = this.dAppMasterMap[obj.cname]; // let user = localStorage.getItem('currentUser'); // var userObj = JSON.parse(user); // var data = { // masterAddress: masterAddress, // userName: userObj.userName, // companyName: obj.cname, // GBMasterAddress: userObj.GBMasterAddress, // allAbis: userObj.allAbis, // email: userObj.email, // token: 'token', // authorized: authorized // }; // localStorage.setItem('currentUser', JSON.stringify(data)); // import('../../globals/globals.component').then(module => { // module.setDappInstance(obj.cname); // }); // this.router.navigate([ // '/components/proposals/' + obj.cname + '/' + obj.proposalId // ]); // }.bind(this) // ); // } // demo(dAppName) { // var i = 0; // this.totalReputation[dAppName] = 0; // while (i < this.dAppReputation[dAppName].length) { // this.totalReputation[dAppName] += // this.dAppReputation[dAppName][i].reputation * 1; // this.dAppReputation[dAppName][i].id = i + 1; // this.dAppReputation[dAppName][i].totalReputation = this.totalReputation[ // dAppName // ]; // i++; // if (i == this.dAppReputation[dAppName].length) { // $('#filters').show(); // $('#loader').hide(); // this.showLabel[dAppName] = true; // var datatable = $('#table_' + dAppName).mDatatable({ // // datasource definition // data: { // type: 'local', // source: this.dAppReputation[dAppName], // pageSize: 10, // saveState: { // cookie: true, // webstorage: true // } // }, // // layout definition // layout: { // theme: 'default', // datatable theme // class: '', // custom wrapper class // scroll: true, // enable/disable datatable scroll both horizontal and vertical when needed. // // height: 550, // datatable's body's fixed height // footer: false // display/hide footer // }, // // column sorting // sortable: false, // pagination: true, // search: { // input: $('#generalSearch') // }, // // columns definition // columns: [ // { // field: 'id', // title: 'S No.', // sortable: true, // width: 40, // textAlign: 'center' // }, // // { // // field:"cname", // // title: "Company Name", // // sortable:false // // }, // { // field: 'date', // title: 'Date', // width: 90, // sortable: false // }, // { // field: 'desc', // title: 'Description', // width: 150, // sortable: false // }, // { // field: 'proposalName', // title: 'Proposal', // width: 150, // sortable: false // }, // { // field: 'proposalStatus', // title: 'Proposal Status', // sortable: false // }, // // { // // field:"finalVerdict", // // title:"Final Verdict", // // sortable:false // // }, // { // field: 'reputation', // title: 'Reputation', // width: 80, // textAlign: 'center', // sortable: false // }, // { // field: 'totalReputation', // title: 'Total Reputation', // width: 80, // textAlign: 'center', // sortable: false // }, // { // field: 'proposalId', // title: '', // width: 25, // sortable: false, // template: function(row) { // if (row.date != '-') // return ( // '
' // ); // else return ''; // } // } // ] // }); // $('#table_' + dAppName).on( // 'click', // '.rbtn', // function(args, err) { // this.onSelectProposal( // this.dAppReputation[dAppName][args.currentTarget.value - 1] // ); // }.bind(this) // ); // var query = datatable.getDataSourceQuery(); // } // } // if (this.dAppReputation[dAppName].length == 0) { // $('#loader').hide(); // this.noRecord = true; // } // } getInitialAddress() { var length; GlobalsComponent.gbmaster.getAlldAppRegistered( function(error, result1) { length = result1.length; var i = 0; var companyName; this.cSize = length; if (length == 0) this.demo(); for (i = 0; i < length; i++) { var index = i; this.dAppNameArray.push(this.hex_to_ascii(result1[index])); GlobalsComponent.gbmaster.getdAppDetails( this.dAppNameArray[index], function(error, result) { this.companyMasterAddress = result[1]; if ( result[5] > 0 && !this.hex_to_ascii(result[0]).startsWith('TEST') ) { this.masterAddresses.push(this.companyMasterAddress); this.dAppArray.push(this.hex_to_ascii(result[0])); var cname = this.hex_to_ascii(result[0]); this.companyNameArray.push({ name: cname }); this.masterDappArray[this.companyMasterAddress] = cname; this.dAppMasterMap[cname] = this.companyMasterAddress; if (this.masterAddresses.length == this.cSize) this.initialCalls(cname); } else { this.cSize--; if (this.masterAddresses.length == this.cSize) this.initialCalls(cname); } }.bind(this) ); } }.bind(this) ); } initialCalls(companyName) { for (let i = 0; i < this.masterAddresses.length; i++) { this.masterCall(companyName, i); } } masterCall(companyName, i) { var cName = this.masterDappArray[this.masterAddresses[i]]; GlobalsComponent.gbmaster.getRewardLedgerData( cName, this.memberAddress, function(error, result) { this.rewardLedger = this.rewardLedger.concat(result); this.cCount++; if (this.cCount == this.cSize) this.demo(); }.bind(this) ); } hex_to_ascii(str1) { var hex = str1.toString(); var str = ''; for (var n = 0; n < hex.length; n += 2) { str += String.fromCharCode(parseInt(hex.substr(n, 2), 16)); } str1 = ''; for (var i = 0; i < str.length; i++) { if (str.charCodeAt(i) != 0) { str1 += str[i]; } } return str1; } ngAfterViewInit() {} predicateBy(prop) { return function(a, b) { if (a[prop] > b[prop]) { return 1; } else if (a[prop] < b[prop]) { return -1; } return 0; }; } public balance = 0; onSelectProposal(obj) { GlobalsComponent.gbmaster.isMemberAuthorized( obj.cname, this.memberAddress, function(errorMR, resultMR) { var authorized = true; if (!resultMR.authorized) { authorized = false; } var masterAddress = this.dAppMasterMap[obj.cname]; let user = localStorage.getItem('currentUser'); var userObj = JSON.parse(user); var data = { masterAddress: masterAddress, userName: userObj.userName, companyName: obj.cname, GBMasterAddress: userObj.GBMasterAddress, allAbis: userObj.allAbis, email: userObj.email, token: 'token', authorized: authorized }; localStorage.setItem('currentUser', JSON.stringify(data)); import('../../globals/globals.component').then(module => { module.setDappInstance(obj.cname); }); this.router.navigate([ '/components/proposals/' + obj.cname + '/' + obj.proposalId ]); }.bind(this) ); } demo() { this.rewardLedger.sort(this.predicateBy('timestamp')); var i = -1; var balance = 0; while (i < this.rewardLedger.length) { if (i == -1) i++; if (this.rewardLedger.length > 0) { // if (i == 0) { // balance = this.rewardLedger[i].reward + this.rewardLedger[i].penalty; // } // else { balance = balance + this.rewardLedger[i].dAppReward; // } this.finalLedger.push({ proposalId: this.rewardLedger[i].proposalId, // blockNumber: this.rewardLedger[i].blockNumber, // type: this.rewardLedger[i].type, // penalty: 0, id: this.finalLedger.length + 1, // totalReward: balance, date: this.rewardLedger[i].date, proposalName: this.rewardLedger[i].title, desc: this.rewardLedger[i].description, // reward: this.rewardLedger[i].dAppReward, cname: this.rewardLedger[i].dAppName }); i++; if (this.finalLedger.length == this.rewardLedger.length) { $('#filters').show(); $('#loader').hide(); var datatable = $('.m-datatable-rewards').mDatatable({ // datasource definition data: { type: 'local', source: this.finalLedger, pageSize: 10, saveState: { cookie: true, webstorage: true } }, // layout definition layout: { theme: 'default', // datatable theme class: '', // custom wrapper class scroll: true, // enable/disable datatable scroll both horizontal and vertical when needed. // height: 550, // datatable's body's fixed height footer: false // display/hide footer }, // column sorting sortable: false, pagination: true, search: { input: $('#generalSearch') }, // columns definition columns: [ { field: 'id', title: 'S No.', sortable: true, textAlign: 'center', width: 40 }, { field: 'cname', title: 'Company Name', width: 150, sortable: false }, { field: 'date', title: 'Date', sortable: false }, { field: 'proposalName', title: 'Proposal', width: 150, sortable: false }, { field: 'desc', title: 'Description', width: 150, sortable: false }, // { // field:"finalVerdict", // title:"Final Verdict", // sortable:false // }, // { // field: 'reward', // title: 'Reward', // textAlign: 'center', // width: 60, // sortable: false // }, // { // field: 'penalty', // title: 'Penalty', // textAlign: 'center', // width: 60, // sortable: false // }, // { // field: 'totalReward', // title: 'Total Reward', // textAlign: 'center', // width: 100, // sortable: false // }, { field: 'proposalId', title: '', width: 25, sortable: false, template: function(row) { return ( '' ); } } ] }); $('.m-datatable-rewards').on( 'click', '.rbtn', function(args, err) { this.onSelectProposal( this.finalLedger[args.currentTarget.value - 1] ); }.bind(this) ); var query = datatable.getDataSourceQuery(); var type = { r: { title: 'Reward' }, p: { title: 'Penalty' } }; $('#m_form_Dapp') .on('change', function() { datatable.search($(this).val(), 'cname'); }) .val(typeof query.cname !== 'undefined' ? query.cname : ''); $('#m_form_type') .on('change', function() { datatable.search($(this).val(), 'type'); }) .val(typeof query.type !== 'undefined' ? query.type : ''); $('#m_form_Dapp, #m_form_type').selectpicker(); } } else { $('#loader').hide(); this.noRecord = true; } } } }