Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div id="react-root">
</div>
<div class="footer">
  <p>by <a target="_blank" href="http://damonmyers.com">Damon Myers</a><p>
</div>

              
            
!

CSS

              
                $grey-font: #6a6a6a;
$light-grey: #e8e8e8;
$lighter-grey: #fafafa;
$default-badge: #c0eac4;

body {
  color: #080808;
  font-family: "Open Sans", sans-serif;
}

.header, .user-container {
  padding: 10px 0;
  display: flex;
  border-bottom: 1px solid #E8E8E8;
}

.header {
  justify-content: space-between;
  line-height: 14pt;
}

.title {
  order: 1;
  font-size: 14pt;
}

.links {
  order: 2;
  text-transform: uppercase;
  font-size: 10pt;
  font-weight: 300;
}

.links a {
  text-decoration: none;
  color: $grey-font;
}

.selected {
  color: #080808;
  font-weight: 600;
  text-decoration: underline;
}

.user-container {
  justify-content: flex-start;
  padding: 16px 0;
}

.user-and-rank {
  margin-left: 16px;
  flex-basis: 100px;
  vertical-align: middle;
  text-align: center;
}

.user-photo {
  position: relative;
  $img-url: attr(data-url);
  background: url($img-url) center / cover;
  content: " ";
  width: 64px;
  height: 64px;
  border: none;
  border-radius: 100%;
  overflow: hidden;
}

.rank {
  position: relative;
  margin-top: -22px;
  margin-left: 60px;
  width: 22px;
  height: 22px;
  z-index: 100;
  text-align: center;
  vertical-align: middle;
  background: $default-badge;
  border: 2px solid darken($default-badge, 20);
  color: rgba(0, 0, 0, 0.8);
  font-weight: 700;
  -webkit-touch-callout: none; /* iOS Safari */
  -webkit-user-select: none; /* Safari */
  -khtml-user-select: none; /* Konqueror HTML */
  -moz-user-select: none; /* Firefox */
  -ms-user-select: none;
  user-select: none;
}

.rank-1 {
  background: yellow;
  border: 2px solid #d9a600;
  color: #d9a600;
}

.rank-2 {
  background: #eaeaea;
  border: 2px solid rgba(40, 40, 40, 0.6);
  color: rgba(40, 40, 40, 0.6);
}

.rank-3 {
  background: #daa794;
  border: 2px solid #903a20;
  color: #903a20;
}

/* Prevent number 100 from rendering outside of box */
.rank-100 {
  width: 28px;
}

.user-info {
  flex-grow: 1;
  margin-top: 10px;
}

.username {
  font-size: 14pt;
  text-decoration: none;
  color: black;
}

.username:hover {
  text-decoration: underline;
}

.scores {
  margin-top: 0;
  margin-left: 1px;
  font-weight: 300;
}

.footer {
  height: 32px;
}

.footer p {
  font-size: 10pt;
  float: right;
}

.footer a {
  text-decoration: none;
  color: $grey-font;
}

a:focus {
  text-decoration: none;
  border: none;
}

.error {
  vertical-align: middle;
  line-height: 100px;
  text-align: center;
  height: 100px;
}

.error p{
  font-size: 12pt;
  color: red;
}

@media screen and (min-width: 769px) {
  html {
    background: $lighter-grey;
  }
  
  body {
    background: white;
    padding: 12px;
    width: 768px;
    margin: 20px auto;
    border: 1px solid $light-grey;
  }
  
  .title {
    font-size: 18pt;
  }
  
  .header {
    line-height: 18pt;
  }
}
              
            
!

JS

              
                // Root React component for managing the leaderboard UserList
// It will be responsible for passing each
// individual user, score, and image url to 
// each User component
class UserList extends React.Component {
  constructor(props) {
    super(props);  
    this.state = {
      isOverallSelected: true,
      currentList: props.topOverallUsers,
      topRecent: props.topRecentUsers,
      topOverall: props.topOverallUsers
    };
  }
  
  handleClick(e) {
    e.preventDefault();
    const switchText = this.state.isOverallSelected ?
          "Recent" : "Overall";
    // We only want to toggle which list we are using
    // If we click on the opposite button
    if (e.target.text == switchText) {
      const newCurrentList = (this.state.isOverallSelected ? 
            this.state.topRecent : this.state.topOverall);

      this.setState({
        isOverallSelected: !this.state.isOverallSelected,
        currentList: newCurrentList
      });
    }
  }
  
  render() {
    // Map each JSON user to a React User component
    // Pass the username, image-url,
    const usersList = this.state.currentList.map(
      (element, index) => <User
                      username={element.username}
                      imgUrl={element.img}
                      overall={element.alltime}
                      recent={element.recent}
                      rank={index + 1} />
      );
    
    return (
      <div className="react-container">
        <Header handleClick={this.handleClick.bind(this)}
          isOverallSelected={this.state.isOverallSelected} />
        <div className="user-list">
          {usersList}
        </div>
      </div>
    );
  }
}

// Renders the header and handles click events
function Header(props) {
  return (
    <div className="header">
      <div className="title">
        Camper Leaderboard
      </div>
      <div className="links">
        <a href="#" onClick={props.handleClick}
          className={props.isOverallSelected ? "selected" : ""}>
          Overall
        </a>
        |
        <a href="#" onClick={props.handleClick}
          className={props.isOverallSelected ? "" : "selected"}>
          Recent
        </a>
      </div>
    </div>
  );
}

// Simple component for rendering each entry in the UserList
function User(props) {
  const userUrl = "https://www.freecodecamp.com/" + props.username;
  return (
    <div className="user-container">
      <div className="user-and-rank">
        <a target="_blank" href={userUrl}>
          <img className="user-photo" src={props.imgUrl}></img>
        </a>
        <div className={"rank rank-" + props.rank}>
          {props.rank}
        </div>
      </div>
      <div className="user-info">
        <a target="_blank" className="username" href={userUrl}>{props.username}</a>
        <p className="scores">
          Overall: {props.overall} Recent: {props.recent}
        </p>
      </div>
    </div>
  );
}

// Simple component that renders an error message if either
// of our requests fail
function Error(props) {
  return (
    <div className="error">
      <p>{props.error_message}</p>
    </div>
  );
}

// We use promises to asynchronously fetch
// data, then call renderComponents when done
function requestAllUserData() {
  const topRecentURL = "https://fcctop100.herokuapp.com/api/fccusers/top/recent";
  const topOverallURL = "https://fcctop100.herokuapp.com/api/fccusers/top/alltime";
  
  // results[0] will be the JSON from topRecent
  // results[1] will be the JSON from topOverall
  let results = [];
  
  requestUserData(topRecentURL).then(result => {
    results[0] = result;
    return requestUserData(topOverallURL);
  })
  .then(secondResult => {
    results[1] = secondResult;
    renderComponents(results, null);
  })
  .catch(errorMsg => {
    renderComponents(null, errorMsg); 
  });
}

// Helper method for requesting either the
// top overall or recent using AJAX
// Returns a promise for async loading
function requestUserData(url) {
  return new Promise((resolve, reject) => {
    let request = new XMLHttpRequest();
    request.open('GET', url, true);
    // If we successfully get the data, resolve our promise
    request.onload = () => {
      if (request.status >= 200 && request.status < 400) {
        resolve(JSON.parse(request.responseText));
      }
      else {
        // They messed up
        reject("Unknown server error.");
      }
    }
    // We messed up
    request.onerror = () => {
      reject("Improper AJAX request.");
    }
    request.send();
  });
}

// Once our async calls are complete,
// we render all React components
function renderComponents(responses, error) {
  if (error) {
    ReactDOM.render(<Error error_message={error} />,
                   document.getElementById("react-root"));
  }
  else {
    const topRecentData = responses[0];
    const topOverallData = responses[1];
    // Pass our JSON as props to the parent component
    ReactDOM.render(<UserList topRecentUsers={topRecentData}
                      topOverallUsers={topOverallData} />,
                 document.getElementById("react-root"));
  }
}

// On load of the page, we fetch the top 100 users
// Once done loading data, call renderComponents
window.addEventListener("load", function() {
  requestAllUserData().then(renderComponents());
});
              
            
!
999px

Console