import { Component, OnInit, ViewEncapsulation, AfterViewInit } from '@angular/core'; import { DomSanitizer } from '@angular/platform-browser'; import { Router } from '@angular/router'; import Web3 from 'web3'; import { Helpers } from '../../../../../helpers'; import { AllProposalsService } from './all-proposals.service'; import { DetailsService } from '../../details.service'; import ipfsAPI from 'ipfs-api'; import { GlobalsComponent } from '../../globals/globals.component'; // import Readable from 'readable-stream'; // import Buffer = require('buffer/'); @Component({ selector: '.m-grid__item.m-grid__item--fluid.m-wrapper', templateUrl: './all-proposals.component.html', styleUrls: ['./all-proposals.component.css'], encapsulation: ViewEncapsulation.None }) export class AllProposalsComponent implements OnInit { p: number = 1; // public data; router: any; public web3; public proposals = []; public startIndex; public lengthOfProposal; public categoryName; public proposalCount; public proposalIndex; public stakes; public userObj; public noProposals = false; public metamaskLogin = true; public statusColour = { 0: 'metal', 1: 'orange', 2: 'warning', 3: 'success', 4: 'danger', 5: 'success', 6: 'danger', 7: 'success' }; public iframeURl = ''; constructor( private details: DetailsService, private globals: GlobalsComponent, private sanitized: DomSanitizer, public detailService: DetailsService ) { if (window.location.href.indexOf('iframe') >= 0) { this.iframeURl = '/iframe'; } } ngOnInit() { this.details.setTitle('All Proposals'); 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.memberAddress = '0x0000000000000000000000000000000000000000'; this.metamaskLogin = false; } else this.memberAddress = this.web3.eth.accounts[0]; this.allProposalsInitiate(); } public memberAddress; allProposalsInitiate() { var urlPath = window.location.href; var index = urlPath.lastIndexOf('/'); var urlCompanyName = urlPath.substring(index + 1, urlPath.length); console.log(urlCompanyName); if (this.userObj.companyName != urlCompanyName) { var data = { masterAddress: '', userName: this.userObj.userName, companyName: urlCompanyName, GBMasterAddress: this.userObj.GBMasterAddress, allAbis: this.userObj.allAbis, email: this.userObj.email, token: 'token', authorized: true }; localStorage.setItem('currentUser', JSON.stringify(data)); import('../../globals/globals.component').then(module => { module.generator(); }); let user1 = localStorage.getItem('currentUser'); this.userObj = JSON.parse(user1); console.log(this.userObj); GlobalsComponent.gbmaster.getdAppDetails( this.userObj.companyName, function(error, result) { this.companyMasterAddress = result[1]; this.userObj.masterAddress = this.companyMasterAddress; GlobalsComponent.gbmaster.getdAppContractAddresses( this.userObj.companyName, function(error2, result2) { if (!error2) { var mrAddress = result2['MemberRoles']; var mrAbi = this.userObj.allAbis[2]; GlobalsComponent.gbmaster.isMemberAuthorized( this.userObj.companyName, this.memberAddress, function(errorMR1, resultMR1) { var authorized = false; if (resultMR1.authorized) { authorized = true; } var timeoutVar1 = setInterval( function() { console.log('insideTimeout'); let user = localStorage.getItem('currentUser'); this.userObj = JSON.parse(user); if ( this.userObj.companyName in GlobalsComponent.governance ) { this.userObj.authorized = authorized; user = localStorage.getItem('currentUser'); this.userObj = JSON.parse(user); var data = { masterAddress: this.companyMasterAddress, userName: this.userObj.userName, companyName: urlCompanyName, GBMasterAddress: this.userObj.GBMasterAddress, allAbis: this.userObj.allAbis, email: this.userObj.email, token: 'token', authorized: authorized }; localStorage.setItem( 'currentUser', JSON.stringify(data) ); user = localStorage.getItem('currentUser'); this.userObj = JSON.parse(user); this.web3.eth.defaultAccount = this.web3.eth.coinbase; this.memberAddress = this.memberAddress; this.getInitialAddress(); clearInterval(timeoutVar1); } }.bind(this), 500 ); }.bind(this) ); } }.bind(this) ); }.bind(this) ); } else { this.web3.eth.defaultAccount = this.web3.eth.coinbase; this.memberAddress = this.memberAddress; import('../../globals/globals.component').then(module => { module.generator(); }); var timeoutVar1 = setInterval( function() { console.log('insideTimeout'); let user = localStorage.getItem('currentUser'); this.userObj = JSON.parse(user); if (this.userObj.companyName in GlobalsComponent.governance) { this.getInitialAddress(); clearInterval(timeoutVar1); } }.bind(this), 500 ); } } public decimals = {}; public authorizeddApp; getInitialAddress() { this.detailService.setdAppName(this.userObj.companyName); this.initialCalls(); GlobalsComponent.gbmaster.getdAppTokenDecimals( 'GOVBLOCKS', function(errGBTdecimals, resGBTdecimals) { this.decimals['GOVBLOCKS'] = resGBTdecimals; }.bind(this) ); GlobalsComponent.gbmaster.getdAppTokenDecimals( this.userObj.companyName, function(errTokendecimals, resTokendecimals) { this.decimals[this.userObj.companyName] = resTokendecimals; }.bind(this) ); } initialCalls() { this.getProposalLength(); } getProLength() { GlobalsComponent.proposalData[this.userObj.companyName].getProposalLength( function(error2, resultlength) { if (!error2) { this.lengthOfProposal = resultlength; var i: number; this.proposals = []; this.proposalCount = this.lengthOfProposal - this.startIndex; if (this.lengthOfProposal == 0) { this.noProposals = true; $('#loader').hide(); } for ( i = this.lengthOfProposal - 1; i > this.lengthOfProposal - 11; i-- ) { let proposal: any = {}; this.getProposalDetailsById3(i, proposal); } } }.bind(this) ); } gotoProposal() { this.router = Router; this.router.navigate(['/proposals/' + this.userObj.companyName]); return; } getProposalLength() { let ans = GlobalsComponent.proposalData[ this.userObj.companyName ].getProposalLength( function(error2, resultlength) { if (!error2) { this.lengthOfProposal = resultlength; var i: number; this.proposals = []; this.proposalCount = this.lengthOfProposal - this.startIndex; if (this.lengthOfProposal == 1) { this.noProposals = true; $('#loader').hide(); } console.log('STarted'); for ( i = this.lengthOfProposal - 1; i > (this.lengthOfProposal > 10 ? this.lengthOfProposal - 11 : 0); i-- // for(i=0;i b[prop]) { return -1; } return 0; }; } getProposalDetailsById3(i, proposal) { let ans = GlobalsComponent.proposalData[ this.userObj.companyName ].getProposalDetails( i, function(errorId3, resultOfgetProposal) { if (!errorId3) { this.totalSolutions = resultOfgetProposal[5]; proposal.index = resultOfgetProposal[0].toNumber(); proposal.categoryId = resultOfgetProposal[2].toNumber(); proposal.proposalStatusIndex = resultOfgetProposal[3].toNumber(); GlobalsComponent.categoryData[ this.userObj.companyName ].isSubCategoryExternal( proposal.categoryId, function(errCid, resCid) { if (resCid) { this.authorizeddApp = 'GOVBLOCKS'; } else { this.authorizeddApp = this.userObj.companyName; } proposal.authorizedApp = this.authorizeddApp; let totVotes = GlobalsComponent.proposalData[ this.userObj.companyName ].getAllVoteIdsLength_byProposal( i, function(errorTotalVotes, resultTotalVotes) { proposal.totalVotes = resultTotalVotes; // proposal.totalVotes = 5; let proData = GlobalsComponent.proposalData[ this.userObj.companyName ].getProposalEventData( i, function(errorProposalData, resultProposalData) { proposal.title = resultProposalData.proposalTitle; proposal.description = resultProposalData.proposalSD; proposal.totalOptions = resultOfgetProposal[5]; if (proposal.totalOptions - 1 == -1) { proposal.ptotalOptions = 0; } else { proposal.ptotalOptions = proposal.totalOptions - 1; } proposal.status = resultProposalData.status; var d = new Date(resultProposalData.dateAdd * 1000); var options = { // weekday: "short", year: 'numeric', month: 'short', day: 'numeric', hour: '2-digit', minute: '2-digit', second: '2-digit' }; proposal.dateAdded = d.toLocaleDateString('en', options); GlobalsComponent.proposalData[ this.userObj.companyName ].getProposalIncentive( resultProposalData.proposalId, function(errGetInc, resGetInc) { if (!errGetInc) { proposal.incentives = resGetInc / Math.pow( 10, this.decimals[proposal.authorizedApp] ); let ans = GlobalsComponent.categoryData[ this.userObj.companyName ].getSubCategoryName( proposal.categoryId, function(errorCategory, resultCategory) { if (!errorCategory) { proposal.categoryName = resultCategory; this.proposals.push(proposal); if ( this.proposals.length == 10 || this.proposals.length == this.lengthOfProposal - 1 ) { console.log('Sorting entered...'); this.proposals.sort( this.predicateBy('index') ); $('#loader').hide(); console.log(this.proposals.length); for ( let i = this.lengthOfProposal - 11; i > 0 && this.proposals.length < this.lengthOfProposal - 1; i-- ) { let proposal: any = {}; this.getProposalDetailsById3(i, proposal); } } this.proposals.sort( this.predicateBy('index') ); $('#loader').hide(); } else { console.log( 'Error of getCategoryName function: ' + errorCategory ); } }.bind(this) ); } else { console.log('Error in getProposalIncentive'); } }.bind(this) ); }.bind(this) ); }.bind(this) ); }.bind(this) ); } else { console.log('Here in error of getProposalDetailsById3'); console.log(errorId3); } }.bind(this) ); // } // else // { // console.log("Error in getProposalStatus"); // } // }.bind(this)); } public totalSolutions; public ProposalStake; public pStake; isStringDesc(description) { if (typeof description === 'string') { return true; } else return false; } // displayArray() // { // console.log("Displaying the array: "); // for(var j=this.startIndex; j