import React, { Component, PropTypes } from 'react';

export default class Stats extends Component {

  static propTypes = {
    categories: PropTypes.array.isRequired,
    locations: PropTypes.array.isRequired,
  }

  getTopSold(amount) {
    const counters = this.props.counters;
    const table = {};
    if (Object.keys(counters).length > 0) {
      Object.keys(counters).forEach(function(index) {
        var count = 0;
        Object.keys(counters[index].counts).forEach(function(_index) {
          count += counters[index].counts[_index][1];
        });
        if (count) {
          table[count] = counters[index];
          table[count].count = count;
        }
      });
      var result = [];
      for (var i = 1; i <= amount; i++) {
        result[i] = {name: table[Object.keys(table)[Object.keys(table).length - i]].name,
                     count: table[Object.keys(table)[Object.keys(table).length - i]].count};
      }
      return result;
    }
    return [];
  }

  render() {
    return (<div>
      <h1>Stats</h1>
      <h2>Top Sold Products</h2>
      <ul>{::this.getTopSold(3).map(function(item) {
        return <li>{item.name} <small>{item.count}</small></li>;
      })}</ul>
    </div>);
  }
}

@@ -541,16 +541,58 @@
 ndex%5D%5B1%5D
+ %7C%7C counters%5Bindex%5D.counts%5B_index%5D.noStore
 ;%0A      
@@ -620,32 +620,37 @@
 nt) %7B%0A          
+if (!
 table%5Bcount%5D = c
@@ -649,27 +649,54 @@
 unt%5D
- = counters%5Bindex%5D;
+) %7B%0A            table%5Bcount%5D = %5B%5D;%0A          %7D
 %0A   
@@ -719,21 +719,19 @@
 nt%5D.
-count = count
+push(index)
 ;%0A  
@@ -834,20 +834,23 @@
 t%5Bi%5D = %7B
-name
+counter
 : table%5B
@@ -899,21 +899,16 @@
 th - i%5D%5D
-.name
 ,%0A      
@@ -929,22 +929,16 @@
  count: 
-table%5B
 Object.k
@@ -978,23 +978,16 @@
 gth - i%5D
-%5D.count
 %7D;%0A     
@@ -1045,24 +1045,46 @@
  render() %7B%0A
+    let _this = this;%0A
     return (
@@ -1208,24 +1208,108 @@
     
-return %3Cli%3E%7Bitem
+  %7Breturn item.counter.map(function(counter) %7B%0A            return %3Cli%3E%7B_this.props.counters%5Bcounter%5D
 .nam
@@ -1343,16 +1343,41 @@
 ll%3E%3C/li%3E
+%0A          %7D)%0A          %7D
 ;%0A      

fix getTopSold to display with same amount