manniru
6/17/2018 - 11:28 PM

Mascoin Local test for Functions

import React, { Component } from 'react'
import IconButton from 'material-ui/IconButton'
import { injectIntl, intlShape } from 'react-intl'
import { GitHubIcon } from 'rmw-shell/lib/components/Icons'
import { Activity } from 'rmw-shell'
import ReactMarkdown from 'react-markdown'
import Scrollbar from 'rmw-shell/lib/components/Scrollbar/Scrollbar'

import Paper from '@material-ui/core/Paper';
import TextField from '@material-ui/core/TextField';
import Button from '@material-ui/core/Button';
import Typography from '@material-ui/core/Typography';
import moment from 'moment';
import faker from 'faker';
import { withFirebase } from 'firekit-provider';
import categories from './json/categories.json';
// import {transferFrom} from './mascoin';

var categories_in = categories.filter((a) => a.operation === 'income');
var categories_out = categories.filter((a) => a.operation === 'outcome');

const txt1 = {width: '400', margin: 10}

const sendsms = (phone, message) => {
  var request = require('request');
  var url = 'http://www.smslive247.com/http/index.aspx?cmd=sendquickmsg&owneremail=manniru@gmail.com&subacct=MASCOIN&subacctpwd=MASCOIN&message=' + message + '&sender=MASCOIN&sendto=' + phone +'&msgtype=0'
  return request(url, function (error, response, body) {
  if (!error) {
      return body;
  }
  });
}

class Balance extends Component {
  // Sorry for using setState here but I have to remove 'marked' from the dependencies
  // because of a vulnerability issue
  constructor (props) {
    super(props)
    this.state = {
        value: '',
        text: '',
        phone: '08033099431',
        message: 'This is Sample Message from MASCOIN SERVER for Testing SMS Functions!',
        phone1: '08033099431',
        phone2: '08069354904',
        from_address: '0xa0a5939d4dd3c097953772b02f42646f1bb5e19e',
        to_address: '0x20ed20a004560e757b129cf3fd515ebd9da414ad',
        amount: 2,
        addr: '0xa0a5939d4dd3c097953772b02f42646f1bb5e19e',
    }
  }

  componentWillMount () {
    // fetch(README)
    //   .then(response => response.text())
    //   .then(text => {
    //     this.setState({ text: text })
    //   })
  }

  handleClick = (e, id) => {
      const { auth, firebaseApp } = this.props;
      const reg = {
        fullname: faker.name.firstName() + " " + faker.name.lastName(),
        phone: faker.helpers.replaceSymbolWithNumber("080########"), // faker.phone.phoneNumberFormat(),
        // userId: auth.uid,
        photo: faker.image.imageUrl(400,400,"people"),
        timestamp: + new Date(),
        status: false
      }

      //random transactions builder
      var opr = faker.random.arrayElement(["income","outcome"]);
      var amt = faker.finance.amount(9000,10000,2);
      var cat = faker.random.objectElement(categories).id;

    //   const tx = {
    //     operation: opr,
    //     entry: amt,
    //     date: + new Date(),
    //     category: cat
    // };

    //   if (this.state.value.length > 0) {
        
    //   }

    switch (id) {
        case 'register':
            firebaseApp.database().ref('mascoin/users').push(reg).then(() => { this.setState({ value: '' }) })
             console.log(reg);
             break;
        case 'login':
        case 'purchase':
            console.log(JSON.stringify(reg))
          break;
        case 'balance':
          var addr = this.state.addr;
          var url = 'http://localhost:8010/mascoinnet/us-central1/onBalance'

          // fetch(url).then(response => response.json()).then(data => console.log(data.message));
          var data = {address: addr};

          fetch(url, {
            method: 'POST', // or 'PUT'
            body: JSON.stringify(data), // data can be `string` or {object}!
            headers:{
              'Content-Type': 'application/json'
            }
          }).then(res => res.json())
          .catch(error => console.error('Error:', error))
          .then(response => console.log('Success:', response));

          console.log('balance: '+ addr);
          break;
        case 'transfer':
            var from_address = this.state.from_address;
            var to_address = this.state.to_address;
            var amount = parseInt(this.state.amount);
            var tx = {
                from_address: from_address,
                to_address: to_address,
                amount: amount,
                timestamp: + new Date(),
            }

            console.log(tx);
          
            // firebaseApp.database().ref('mascoin/transactions').push(tx).then(() => { this.setState({ value: '' }) })
          ///console.log(tx);
          ///console.log(JSON.stringify(tx));
          break;
        case 'sms':
        var phone1 = this.state.phone1;
        var message = this.state.message;
        sendsms(phone1, message);
        console.log('SMS Sent');
        break;
        default:
          console.log('default');
      }

  }

  handleChange = name => event => {
    this.setState({
      [name]: event.target.value,
    });
  };

  render () {
    const { intl } = this.props

    return (
      <Activity
        appBarContent={
          <IconButton

            href='https://github.com/TarikHuber/react-most-wanted'
            target='_blank'
            rel='noopener'
          >
            <GitHubIcon />
          </IconButton>
        }
        title="Account Balance">

        <Scrollbar>
          {/* <div style={{ backgroundColor: 'white', padding: 12 }}> */}
          
            <Paper style={{margin: 10, padding: 10}} elevation={4}>
                <Typography variant="headline" component="h3">Mascoin Account Balance</Typography>
                <Typography component="p">Your Account Balance is: 0</Typography>
            </Paper>


            <Paper style={{margin: 10, padding: 10}} elevation={4}>
                <Typography variant="headline" component="h3">Test Balance</Typography>
                <TextField
                id="addr"
                label="Address"
                stlye={txt1}
                value={this.state.addr}
                onChange={this.handleChange('addr')}
            />
            <br />   
            <Button variant="contained" color="primary" style={{margin: 10, width: 200}} onClick={((e) => this.handleClick(e, 'balance'))}>Get Balance</Button> <br />
           
            </Paper>

            <Paper style={{margin: 10, padding: 10}} elevation={4}>
                <Typography variant="headline" component="h3">Mascoin Transfer</Typography>
            
            <TextField
                id="phone1"
                label="Phone Number 1"
                stlye={txt1}
                value={this.state.phone1}
                onChange={this.handleChange('phone1')}
            />
            <br />

            <TextField
                id="phone2"
                label="Phone Number 2"
                stlye={txt1}
                value={this.state.phone2}
                onChange={this.handleChange('phone2')}
            />
            <br />

            <TextField
                id="from_address"
                label="From Address"
                stlye={txt1}
                value={this.state.from_address}
                onChange={this.handleChange('from_address')}
            />
            <br />

            <TextField
                id="to_address"
                label="To Address"
                stlye={txt1}
                value={this.state.to_address}
                onChange={this.handleChange('to_address')}
            />
            <br />
            <TextField
                id="amount"
                label="Amount"
                stlye={txt1}
                value={this.state.amount}
                onChange={this.handleChange('amount')}
            />
            <br />
            <Button variant="contained" color="primary" style={{margin: 10, width: 200}} onClick={((e) => this.handleClick(e, 'transfer'))}>Test Transfer</Button> <br />

            </Paper>


            <Paper style={{margin: 10, padding: 10}} elevation={4}>
                <Typography variant="headline" component="h3">Mascoin SMS Message</Typography>
                <TextField
                id="phone"
                label="Phone Number"
                stlye={txt1}
                value={this.state.phone}
                onChange={this.handleChange('phone')}
            />
            <br />
            <TextField
                id="message"
                label="Message"
                stlye={txt1}
                value={this.state.message}
                onChange={this.handleChange('message')}
            />
            <br />   
            <Button variant="contained" color="primary" style={{margin: 10, width: 200}} onClick={((e) => this.handleClick(e, 'sms'))}>Send SMS</Button> <br />
           
            </Paper>
            
            <br />

            <Button variant="contained" color="primary" style={{margin: 10, width: 200}} onClick={((e) => this.handleClick(e, 'register'))}>Test Register</Button> <br />
            <Button variant="contained" color="primary" style={{margin: 10, width: 200}} onClick={((e) => this.handleClick(e, 'login'))}>Test Login</Button> <br />
            <Button variant="contained" color="primary" style={{margin: 10, width: 200}} onClick={((e) => this.handleClick(e, 'purchase'))}>Test Purchase</Button> <br />

            <Button variant="contained" color="primary" style={{margin: 10}}>Refresh</Button>
            <Button variant="contained" color="secondary" style={{margin: 10}}>Reload</Button>

          {/* </div> */}
        </Scrollbar>

      </Activity>
    )
  }
}

Balance.propTypes = {
  intl: intlShape.isRequired
}

export default withFirebase(Balance)