|
| 1 | +import React, { Component } from 'react'; |
| 2 | +import * as moment from 'moment'; |
| 3 | +import Line_Chart from './Line_Chart' |
| 4 | +import Bar_Chart from './Bar_Chart' |
| 5 | +import '../App.css'; |
| 6 | +var api = require('../utils/moltin.js') |
| 7 | + |
| 8 | +class Orders extends Component { |
| 9 | + |
| 10 | + constructor(props) { |
| 11 | + super(); |
| 12 | + this.state = {orders: null}; |
| 13 | + } |
| 14 | + |
| 15 | + componentDidMount() { |
| 16 | + api.GetOrders() |
| 17 | + .then((orders) => { |
| 18 | + this.setState(() => { |
| 19 | + return { |
| 20 | + orders: orders |
| 21 | + } |
| 22 | + }) |
| 23 | + }) |
| 24 | + |
| 25 | + .catch((error) => { |
| 26 | + console.log(error) |
| 27 | + }) |
| 28 | + |
| 29 | + } |
| 30 | + |
| 31 | + render() { |
| 32 | + |
| 33 | + if(this.state.orders !== null) { |
| 34 | + //console.log(this.state.orders.data) |
| 35 | + |
| 36 | + var SevenDaysAgo = moment().subtract(7, 'days').calendar(); |
| 37 | + var f = moment(SevenDaysAgo).format(); |
| 38 | + var n = f.slice(8, 10) |
| 39 | + //console.log(n); |
| 40 | + var OrdersLessThanSevenDaysAgo = this.state.orders.data.filter(function(order) { |
| 41 | + return order.meta.timestamps.created_at.slice(8,10) > n; |
| 42 | + }) |
| 43 | + console.log(OrdersLessThanSevenDaysAgo); |
| 44 | + |
| 45 | + var Today = moment().format(); |
| 46 | + var TodaySliced = Today.slice(8,10) |
| 47 | + var OneDayAgo = moment().subtract(1, 'days'); |
| 48 | + var OneDayAgoFormatted = moment(OneDayAgo).format(); |
| 49 | + var OneDayAgoSliced = OneDayAgoFormatted.slice(8,10); |
| 50 | + var TwoDaysAgo = moment().subtract(2, 'days'); |
| 51 | + var TwoDaysAgoFormatted = moment(TwoDaysAgo).format(); |
| 52 | + var TwoDaysAgoSliced = TwoDaysAgoFormatted.slice(8,10); |
| 53 | + var ThreeDaysAgo = moment().subtract(3, 'days'); |
| 54 | + var ThreeDaysAgoFormatted = moment(ThreeDaysAgo).format(); |
| 55 | + var ThreeDaysAgoSliced = ThreeDaysAgoFormatted.slice(8,10); |
| 56 | + var FourDaysAgo = moment().subtract(4, 'days'); |
| 57 | + var FiveDaysAgo = moment().subtract(5, 'days'); |
| 58 | + var SixDaysAgo = moment().subtract(6, 'days'); |
| 59 | + |
| 60 | + var Data = [0, 0, 0, 0, 0, 0, 0] |
| 61 | + var result = OrdersLessThanSevenDaysAgo.forEach(function(order) { |
| 62 | + console.log(order.meta.timestamps.created_at.slice(8,10)) |
| 63 | + // console.log(TodaySliced); |
| 64 | + switch(order.meta.timestamps.created_at.slice(8,10)) { |
| 65 | + case TodaySliced : Data[6]++; |
| 66 | + // case OneDayAgoSliced : Data[5]++; |
| 67 | + // case TwoDaysAgoSliced : Data[4]++; |
| 68 | + // case ThreeDaysAgoSliced : Data[3]++; |
| 69 | + break; |
| 70 | + } |
| 71 | + |
| 72 | + //console.log(order.meta.timestamps.created_at.slice(5,7)) |
| 73 | + //console.log(Data) |
| 74 | + }); |
| 75 | + |
| 76 | + |
| 77 | + |
| 78 | + var chartData = { |
| 79 | + labels: [moment(SixDaysAgo).format("MMM Do YYYY"), moment(FiveDaysAgo).format("MMM Do YYYY"), moment(FourDaysAgo).format("MMM Do YYYY"), moment(ThreeDaysAgo).format("MMM Do YYYY"), moment(TwoDaysAgo).format("MMM Do YYYY"), moment(OneDayAgo).format("MMM Do YYYY"), moment(Today).calendar()], |
| 80 | + datasets: [{ |
| 81 | + label: '# of Orders', |
| 82 | + data: Data, |
| 83 | + backgroundColor: [ |
| 84 | + 'rgba(255, 99, 132, 0.2)', |
| 85 | + 'rgba(54, 162, 235, 0.2)', |
| 86 | + 'rgba(255, 206, 86, 0.2)', |
| 87 | + 'rgba(75, 192, 192, 0.2)', |
| 88 | + 'rgba(153, 102, 255, 0.2)', |
| 89 | + 'rgba(255, 159, 64, 0.2)' |
| 90 | + ], |
| 91 | + borderColor: [ |
| 92 | + 'rgba(255,99,132,1)', |
| 93 | + 'rgba(54, 162, 235, 1)', |
| 94 | + 'rgba(255, 206, 86, 1)', |
| 95 | + 'rgba(75, 192, 192, 1)', |
| 96 | + 'rgba(153, 102, 255, 1)', |
| 97 | + 'rgba(255, 159, 64, 1)' |
| 98 | + ], |
| 99 | + borderWidth: 1 |
| 100 | + }] |
| 101 | + } |
| 102 | + |
| 103 | + var chartOptions = { |
| 104 | + scales: { |
| 105 | + yAxes: [{ |
| 106 | + ticks: { |
| 107 | + beginAtZero:true |
| 108 | + } |
| 109 | + }] |
| 110 | + } |
| 111 | + }; |
| 112 | + |
| 113 | + return ( |
| 114 | + <div> |
| 115 | + <div style={{float : 'left', width: 50 + '%'}}> |
| 116 | + <Line_Chart chartData={chartData} chartOptions={chartOptions}/> |
| 117 | + </div> |
| 118 | + <div style={{float : 'right', width: 50 + '%'}}> |
| 119 | + <Bar_Chart chartData={chartData} chartOptions={chartOptions}/> |
| 120 | + </div> |
| 121 | + |
| 122 | + </div> |
| 123 | + |
| 124 | + ) |
| 125 | + |
| 126 | + } |
| 127 | + |
| 128 | + else { |
| 129 | + console.log('no data') |
| 130 | + //console.log(this.state) |
| 131 | + |
| 132 | + return ( |
| 133 | + <p>no data</p> |
| 134 | + ) |
| 135 | + } |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +export default Orders; |
0 commit comments