React FCC Leaderboard

In this example below you will see how to do a React FCC Leaderboard with some HTML / CSS and Javascript

Free Code Camp Camper leaderboard for React Projects - Data Visualization Certification. Using React and Sass and fetching JSON data from fcc api

Thumbnail
This awesome code was written by chrisparker, you can see more from this user in the personal repository.
You can find the original code on Codepen.io
Copyright chrisparker ©

Technologies

  • HTML
  • CSS
  • JavaScript
<!DOCTYPE html>
<html lang="en" >

<head>
  <meta charset="UTF-8">
  <title>React FCC Leaderboard</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://fonts.googleapis.com/css?family=Bree+Serif" rel="stylesheet">
  
  <link rel='stylesheet prefetch' href='https://cdnjs.cloudflare.com/ajax/libs/foundation/6.3.0/css/foundation.min.css'>
<link rel='stylesheet prefetch' href='https://cdn.jsdelivr.net/foundation-icons/3.0/foundation-icons.min.css'>

      <link rel="stylesheet" href="css/style.css">

  
</head>

<body>

  <div id="container" class="container">
</div>
  <script src='https://cdnjs.cloudflare.com/ajax/libs/react/15.4.2/react.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/react/15.4.2/react-dom.min.js'></script>

  

    <script  src="js/index.js"></script>




</body>

</html>

/*Downloaded from https://www.codeseek.co/chrisparker/react-fcc-leaderboard-jBgebm */
table {
  min-width: 320px;
  max-width: 720px;
  margin: 0 auto;
}

body {
  background-color: #f7f7f7;
}

caption {
  color: #006400;
  font-size: 2em;
  font-family: "Bree Serif", serif;
}

.clickable {
  cursor: pointer;
  color: #1971AA;
}

.clickable:hover {
  border-bottom: 2px solid #1971AA;
}

img {
  margin-right: 10px;
}

th {
  position: relative;
  background-color: #ffffff;
  border-bottom: 2px solid #f7f7f7;
}

table thead th {
  font-weight: 400;
}

.active {
  font-weight: 800;
}

i {
  position: absolute;
  right: 5%;
  top: 22%;
}

@media only screen and (min-width: 550px) {
  table {
    font-size: 1em;
  }
}
@media only screen and (max-width: 550px) and (min-width: 0px) {
  table {
    font-size: 0.75em;
  }
}


/*Downloaded from https://www.codeseek.co/chrisparker/react-fcc-leaderboard-jBgebm */
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

var App = function (_React$Component) {
  _inherits(App, _React$Component);

  function App(props) {
    _classCallCheck(this, App);

    var _this = _possibleConstructorReturn(this, (App.__proto__ || Object.getPrototypeOf(App)).call(this, props));

    _this.state = {
      camperData: [],
      tS: "recent"
    };
    return _this;
  }

  _createClass(App, [{
    key: "actRecent",
    value: function actRecent() {
      if (this.state.tS === "recent") {
        return "active";
      } else {
        return "clickable";
      }
    }
  }, {
    key: "actAllTime",
    value: function actAllTime() {
      if (this.state.tS === "alltime") {
        return "active";
      } else {
        return "clickable";
      }
    }
  }, {
    key: "sortData",
    value: function sortData(timeSpan) {
      var component = this;
      this.setState({ tS: timeSpan });
      fetch("https://fcctop100.herokuapp.com/api/fccusers/top/" + timeSpan).then(function (response) {
        return response.json();
      }).then(function (camper) {
        component.setState({ camperData: camper });
      });
    }
  }, {
    key: "componentDidMount",
    value: function componentDidMount() {
      this.sortData("recent");
    }
  }, {
    key: "render",
    value: function render() {
      var _this2 = this;

      return React.createElement(
        "table",
        { className: "unstriped hover" },
        React.createElement(
          "caption",
          null,
          "freeCodeCamp Camper Leaderboard"
        ),
        React.createElement(
          "thead",
          null,
          React.createElement(
            "tr",
            null,
            React.createElement(
              "th",
              { width: "5%", id: "num", className: "text-center" },
              "#"
            ),
            React.createElement(
              "th",
              { width: "45%", id: "name" },
              "Name"
            ),
            React.createElement(
              "th",
              _defineProperty({ id: "recenthead", className: this.actRecent(), onClick: function onClick() {
                  return _this2.sortData("recent");
                }, width: "25%" }, "id", "pointslast30"),
              "Last 30 Days ",
              React.createElement("i", { className: "fi-arrow-down right" })
            ),
            React.createElement(
              "th",
              _defineProperty({ id: "alltimehead", className: this.actAllTime(),
                onClick: function onClick() {
                  return _this2.sortData("alltime");
                }, width: "25%" }, "id", "pointsalltime"),
              "All Time",
              React.createElement("i", { className: "fi-arrow-down right" })
            )
          )
        ),
        React.createElement(
          "tbody",
          { id: "tableData" },
          this.state.camperData.map(function (c, i) {
            return React.createElement(
              "tr",
              null,
              React.createElement(
                "td",
                { className: "text-center" },
                i + 1
              ),
              React.createElement(
                "td",
                null,
                React.createElement("img", { src: c.img, height: "20", width: "20" }),
                c.username
              ),
              React.createElement(
                "td",
                null,
                c.recent
              ),
              React.createElement(
                "td",
                null,
                c.alltime
              )
            );
          })
        )
      );
    }
  }]);

  return App;
}(React.Component);

ReactDOM.render(React.createElement(App, null), document.getElementById("container"));

Comments