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: './solutions-provided.component.html', encapsulation: ViewEncapsulation.None }) export class SolutionsProvidedComponent implements OnInit, AfterViewInit { public web3; public memberAddress; public masterAddresses = []; public optionData = []; public companyNameArray = []; public companyMasterAddress; public dAppNameArray = []; public Ccount = 0; public Csize; public dAppArray = []; public optionData1 = {}; public cData = {}; public finalDetails = []; public noRecord = false; public userObj; public masterDappArray = {}; public dAppMasterMap = {}; public statusArray = []; public metamaskLogin = true; constructor( private _script: ScriptLoaderService, private detailService: DetailsService, private globals: GlobalsComponent, private router: Router ) {} ngOnInit() { this.detailService.setTitle('Solutions Provided'); $('#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 cname = this.hex_to_ascii(result[0]); if (result[5].toNumber() > 0 && !cname.startsWith('TEST')) { this.companyMasterAddress = result[1]; this.masterAddresses.push(this.companyMasterAddress); this.dAppArray.push(this.hex_to_ascii(result[0])); import('../../globals/globals.component').then(module => { module.setDappInstance(cname); }); this.masterDappArray[this.companyMasterAddress] = cname; this.dAppMasterMap[cname] = this.companyMasterAddress; this.companyNameArray.push({ name: cname }); 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 i1 = 0; i1 < this.masterAddresses.length; i1++) { companyName = this.dAppArray[i1]; GlobalsComponent.gbmaster.getSolutionLedgerData( companyName, this.memberAddress, function(error, result) { this.optionData = this.optionData.concat(result); this.optionData1[companyName] = result; this.Ccount++; this.optionData.sort(this.predicateBy('timeStamp')); if (this.Csize == this.Ccount) { 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; }; } onSelectProposal(obj) { GlobalsComponent.gbmaster.isMemberAuthorized( obj.cname, this.memberAddress, function(errorMR, resultMR) { var authorized = true; if (resultMR.length < 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() { for (let i = 0, j = -1; j < this.optionData.length; j++, i++) { j = i; if (this.optionData.length > 0) { $('#filters').show(); this.finalDetails.push({ proposalId: this.optionData[i].proposalId, id: this.finalDetails.length + 1, status: this.optionData[i].status, date: this.optionData[i].date, stake: this.optionData[i].optionStake, // reward: this.optionData[i].reward, proposalName: this.optionData[i].proposalName, cname: this.optionData[i].companyName }); if (this.finalDetails.length == this.optionData.length) { $('#loader').hide(); var datatable = $('.m-datatable-solutions-provided').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, pagination: true, search: { input: $('#generalSearch') }, // columns definition columns: [ { field: 'id', title: 'S No.', sortable: true, width: 50 }, { field: 'cname', title: 'Company Name', sortable: false }, { field: 'date', title: 'Date', sortable: false }, { field: 'proposalName', title: 'Proposal Title', width: 150, sortable: false }, // { // field: "desc", // title: "Option Description", // sortable: false // }, { field: 'status', title: 'Proposal Status', sortable: false }, // { // field: "finalVerdict", // title: "Final Verdict", // sortable: false // }, // { // field: "stake", // title: "Stake", // sortable: false, // width: 75 // }, // { // field: 'reward', // title: 'Reward', // sortable: false, // width: 75 // }, { field: 'proposalId', title: '', width: 25, sortable: false, template: function(row) { return ( '
  • ' ); } } ] }); $('.m-datatable-solutions-provided').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(), 'cname'); }) .val(typeof query.cname !== 'undefined' ? query.cname : ''); $('#m_form_status') .on('change', function() { datatable.search($(this).val(), 'status'); }) .val(typeof query.status !== 'undefined' ? query.status : ''); $('#m_form_Dapp,#m_form_status').selectpicker(); } } else { $('#loader').hide(); this.noRecord = true; } } } }