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: './proposals-raised.component.html', encapsulation: ViewEncapsulation.None }) export class ProposalsRaisedComponent implements OnInit, AfterViewInit { public web3; public memberAddress; public masterAddresses = []; public proposalDetails = []; public finalDetails = []; public proposalDetails1 = {}; public masterAddress; public companyNameArray = []; public dAppNameArray = []; public cSize; public cCount = 0; public dAppArray = []; public companyName; public noRecord = false; public userObj; public masterDappArray = {}; public showFilter = false; public dAppMasterMap = {}; public statusArray = []; public metamaskLogin = true; constructor( private _script: ScriptLoaderService, private detailService: DetailsService, private globals: GlobalsComponent, private router: Router ) {} ngOnInit() { this.detailService.setTitle('Proposals Raised'); $('#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) { for ( let i = 0; i < GlobalsComponent.gbmaster.statusNameArray.length; i++ ) { this.statusArray.push({ name: GlobalsComponent.gbmaster.statusNameArray[i] }); } 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) { var companyMasterAddress = result[1]; this.memberAddress = this.memberAddress; if ( result[5].toNumber() > 0 && !this.hex_to_ascii(result[0]).startsWith('TEST') ) { var cname = this.hex_to_ascii(result[0]); this.dAppArray.push(cname); this.masterAddresses.push(companyMasterAddress); this.masterDappArray[companyMasterAddress] = cname; this.dAppMasterMap[cname] = companyMasterAddress; this.companyNameArray.push({ name: cname }); this.proposalDetails1[cname] = []; if (this.masterAddresses.length == this.cSize) { this.initialCalls(cname, index); } } else { this.cSize--; if (this.masterAddresses.length == this.cSize) { this.initialCalls(cname, index); } } }.bind(this) ); } }.bind(this) ); } initialCalls(companyName, index) { for (let i = 0; i < this.masterAddresses.length; i++) { companyName = this.dAppArray[i]; GlobalsComponent.gbmaster.getProposalLedgerData( companyName, this.memberAddress, function(error, result) { this.proposalDetails = this.proposalDetails.concat(result); this.proposalDetails1[companyName] = result; this.proposalDetails.sort(this.predicateBy('timestamp')); this.cCount++; if (this.cSize == this.cCount) { if (this.proposalDetails.length > 0) { this.showFilter = true; } this.demo(); } }.bind(this) ); } } //method to convert hexadecimal to ASCII 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() {} //Get Details From IPFS getDataFromHash(hash) { if (hash == '') { return hash; } else { var XMLHttp = new XMLHttpRequest(); XMLHttp.open('GET', 'https://ipfs.govblocks.io/ipfs/' + hash, false); XMLHttp.send(null); // // // this gives the json containing the details - title, description, voting type var hashDetails = JSON.parse(XMLHttp.responseText); if ('title' in hashDetails) return hashDetails.title; return hashDetails[0].title; } } 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.companyName, this.memberAddress, function(errorMR, resultMR) { var authorized = true; if (resultMR.length < 1) { authorized = false; } var masterAddress = this.dAppMasterMap[obj.companyName]; let user = localStorage.getItem('currentUser'); var userObj = JSON.parse(user); var data = { masterAddress: masterAddress, userName: userObj.userName, companyName: obj.companyName, 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.companyName); }); this.router.navigate([ '/components/proposals/' + obj.companyName + '/' + obj.proposalId ]); }.bind(this) ); } demo() { for (let i = 0, j = -1; j < this.proposalDetails.length; i++, j++) { j = i; if (this.proposalDetails.length > 0) { $('#filters').show(); this.showFilter = true; this.finalDetails.push({ id: i + 1, timeStamp: this.proposalDetails[i].timeStamp, stake: this.proposalDetails[i].stake, // reward: this.proposalDetails[i].reward, proposalId: this.proposalDetails[i].proposalId, title: this.proposalDetails[i].title, date: this.proposalDetails[i].date, status: this.proposalDetails[i].status, totalVotes: this.proposalDetails[i].totalVotes, totalOptions: this.proposalDetails[i].totalOptions, companyName: this.proposalDetails[i].dAppName }); if (this.finalDetails.length == this.proposalDetails.length) { $('#loader').hide(); var datatable = $('.m-datatable-proposals-raised').mDatatable({ // datasource definition data: { type: 'local', source: this.finalDetails, 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, ordering: true, pagination: true, search: { input: $('#generalSearch') }, // columns definition columns: [ { field: 'id', title: 'S No.', width: 40, textAlign: 'center', sortable: true }, { field: 'companyName', title: 'Company Name', sortable: false }, { field: 'date', title: 'Date', sortable: false }, { field: 'title', title: 'Title', width: 150, sortable: false }, { field: 'status', title: 'Status', sortable: false }, // { // field: "category", // title: "Category", // width:100, // sortable: false // }, // { // field: "totalVotes", // title: "Votes", // width:60, // textAlign: 'center', // sortable:false // }, { field: 'totalOptions', title: 'Options', width: 60, textAlign: 'center', sortable: false }, // { // title: "Stake", // field: "stake", // sortable: false, // textAlign: 'center', // width: 60 // }, // { // field: 'reward', // title: 'Reward Earned', // width: 60, // textAlign: 'center', // sortable: false // }, { field: 'proposalId', title: '', width: 25, sortable: false, template: function(row) { return ( '
' ); // return 'Hell'; } } ] }); $('.m-datatable-proposals-raised').on( 'click', '.rbtn', function(args, err) { this.onSelectProposal( this.finalDetails[args.currentTarget.value - 1] ); }.bind(this) ); var query = datatable.getDataSourceQuery(); $('#m_form_Dapp') .on('change', function() { datatable.search($(this).val(), 'companyName'); }) .val( typeof query.companyName !== 'undefined' ? query.companyName : '' ); $('#status') .on('change', function() { datatable.search($(this).val(), 'status'); }) .val(typeof query.status !== 'undefined' ? query.status : ''); $(' #status,#m_form_Dapp').selectpicker(); } } else { $('#loader').hide(); this.noRecord = true; } } } }