Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ 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

Auto Save

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="content"></div>
              
            
!

CSS

              
                @import url(https://fonts.googleapis.com/css?family=Inconsolata);

$small-screen: "only screen and (max-width: 479px)";

* {
    margin: 0px;
    padding: 0px;
}

body {
    background-image: url(https://www.dropbox.com/s/8l3mwap0o8u44v0/campfire.jpg?raw=1);
    background-repeat: no-repeat;
    background-attachment: fixed;
    background-size: cover;
    font-family: "Inconsolata", monospace;
}

#content {
    width: 85%;
    max-width: 960px;
    margin: 0px auto;
    padding: 0px 8px;
    background-color: rgba(223, 223, 223, 0.8);

    @media #{$small-screen} {
        width: 95%;
    }

    h1 {
        text-align: center;
        margin-bottom: 8px;
        padding: 16px 0px;
        font-size: 40px;
        border-bottom: 2px solid black;

        @media #{$small-screen} {
            font-size: 22px;
            border: none;
        }
    }

    h2 {
        font-style: italic;
        text-align: center;
        padding: 16px 0px;

        @media #{$small-screen} {
            font-size: 20px;
        }
    }

    h6 {
        text-align: center;
        padding: 32px 0px;
    }

    p {
        text-align: center;

        @media #{$small-screen} {
            font-size: 14px;
        }
    }

    a {
        color: #004400;
        text-decoration: none;
        font-weight: bold;

        &:hover {
            text-decoration: underline;
        }
    }

    table {
        width: 100%;

        thead tr th {
            padding: 16px 0px;
            border-bottom: 1px solid black;

            @media #{$small-screen} {
                font-size: 14px;
            }
        }

        tbody tr td {
            text-align: center;

            @media #{$small-screen} {
                font-size: 14px;
            }

            img {
                margin: 0px auto;
            }
        }
    }
}
              
            
!

JS

              
                // The URL we need to fetch our campers.
const URL = "https://fcctop100.herokuapp.com/api/fccusers/top/recent";

///
/// \class  CamperComponent
/// \brief  Displays a record of a single camper.
///
/// This class expects four properties:
///
/// name:           The username of the camper.
/// img:            The camper's profile picture.
/// recent:         The amount of Brownie Points (BP) the user has
///                 earned over the last 30 days.
/// alltime:        The amount of BP the user has earned overall.
///
class CamperComponent extends React.Component {
    ///
    /// \brief  The constructor.
    ///
    /// \param  props       The expected properties.
    ///
    constructor (props) {
        super(props);
    }

    ///
    /// \fn     render
    /// \brief  Renders the component.
    ///
    /// This camper's record will be rendered in a table row, so
    /// render this inside the body of a table.
    ///
    render () {
        // Check to see if the camper's name is too long. If so, truncate
        // it with the ellipsis.
        const renderedName = (
            this.props.name.length > 13 ?
                this.props.name.slice(0, 10) + "..." :
                this.props.name
        );

        return (
            <tr>
                <td>
                    <img src={this.props.img} alt={this.props.name} width="32px" />
                </td>
                <td>
                    <a href={"https://freecodecamp.com/" + this.props.name} target="_blank">
                        {renderedName}
                    </a>
                </td>
                <td>
                    {this.props.recent}
                </td>
                <td>
                    {this.props.alltime}
                </td>
            </tr>
        );
    }
};

///
/// \class  BoardComponent
/// \brief  Displays a list of leading Campers.
///
class BoardComponent extends React.Component {
    ///
    /// \fn     toggleSorting
    /// \brief  Toggles the sorting of our leaderboard.
    ///
    /// \param  score       Which score shall be toggled?
    ///
    toggleSorting (score) {
        // Store a temporary copy of the campers array.
        let camperArray = this.state.campers.array.slice();

        // Find out what score we are sorting, and in what direction,
        // if any at all.
        const currentDirection = this.state.campers.sorting.direction;
        const currentScore = this.state.campers.sorting.score;

        // Find out what kind of sorting we'll have to do, and store it
        // here.
        let newSorting = null;

        // Determine the sorting order here.
        if (score === currentScore) {
            // If we are not sorting the other score, then find out how
            // we are sorting it here. If we are sorting in ascending order, 
            // then sort descending. Otherwise, sort ascending.
            if (currentDirection === "ascending") {
                newSorting = "descending";
            }
            else if (currentDirection === "descending") {
                newSorting = "ascending";
            }
        } else {
            // If we are sorting out the other score (we were sorting recent, but
            // now we want to sort alltime, for instance), then default to descending.
            newSorting = "descending";
        }
        
        // Sort our array according to the new sorting order.
        if (newSorting === "ascending") {
            camperArray.sort((a, b) => a[score] - b[score]);
        }
        else if (newSorting === "descending") {
            camperArray.sort((a, b) => b[score] - a[score]);
        }

        // Update our state.
        this.setState({
            campers: {
                array: camperArray,
                sorting: {
                    score: score,
                    direction: newSorting
                }
            }
        });
    }

    ///
    /// \brief  The constructor.
    ///
    /// \param  props       The expected properties.
    ///
    constructor (props) {
        super(props);

        this.state = {
            campers: {
                array: [],
                sorting: {
                    score: "recent",
                    direction: "none"
                }
            },
            fetchError: null
        };
    }

    ///
    /// \fn     componentDidMount
    /// \brief  Called when the component is rendered for the first time.
    ///
    componentDidMount () {
        // Fetch our campers from the server.
        axios.get(URL).then(response => {
            // Update the state with our new array.
            this.setState({
                campers: {
                    array: response.data,
                    sorting: {
                        score: "recent",
                        direction: "descending"
                    },
                    fetchError: null
                }
            });
        }).catch(error => {
            // If the request returned a response other than 200...
            if (error.response) {
                // Adjust our state accordingly.
                this.setState({
                    fetchError: `Could not retrieve the data (Status Code ${error.response.status}).`
                });
            }
            else {
                this.setState({
                    fetchError: `Could not retrieve the data (${error.message}).`
                });
            }
        });
    }

    ///
    /// \fn     onRecentClicked
    /// \brief  Handled when the "Last 30 Days" button is clicked.
    ///
    onRecentClicked () {
        this.toggleSorting("recent");
    }

    ///
    /// \fn     onAlltimeClicked
    /// \brief  Handled when the "All Time" button is clicked.
    ///
    onAlltimeClicked () {
        this.toggleSorting("alltime");
    }

    ///
    /// \fn     renderTable
    /// \brief  Renders the table inside which our leaderboard will reside.
    ///
    renderTable () {
        // Map our campers into an array of renderable elements.
        const mapped = this.state.campers.array.map((val, index) => {
            return (
                <CamperComponent key={index}
                                 name={val.username}
                                 img={val.img}
                                 recent={val.recent}
                                 alltime={val.alltime} />
            );
        });

        // Render a table with our campers in it.
        return (
            <table>
                <thead>
                    <tr>
                        <th>Image</th>
                        <th>Camper</th>
                        <th>
                            <a href="#" onClick={this.onRecentClicked.bind(this)}>
                                {
                                    this.state.campers.sorting.score == "recent" ?
                                        (this.state.campers.sorting.direction === "descending" ? 
                                            `Recent (d)` :
                                            `Recent (a)`) :
                                        `Recent`
                                }
                            </a>
                        </th>
                        <th>
                            <a href="#" onClick={this.onAlltimeClicked.bind(this)}>
                                {
                                    this.state.campers.sorting.score == "alltime" ?
                                        (this.state.campers.sorting.direction === "descending" ? 
                                            `Overall (d)` :
                                            `Overall (a)`) :
                                        `Overall`
                                }
                            </a>
                        </th>
                    </tr>
                </thead>
                <tbody>
                    {mapped}
                </tbody>
            </table>
        );
    }

    ///
    /// \fn     render
    /// \brief  Renders the component.
    ///
    render () {
        // Check to see if there was an error fetching the data.
        if (this.state.fetchError) {
            // Let the user know what went wrong.
            return (
                <div className="errorMessage">
                    <h2>There was an error retriving the data.</h2>
                    <p>{this.state.fetchError}</p>
                </div>
            );
        } else {
            // Check to see if we have fetched our campers yet.
            if (this.state.campers.array.length === 0) {
                // Let us know that we are still fetching.
                return (
                    <h2>Fetching Campers. Please Wait...</h2>
                );
            } else {
                return this.renderTable();
            }
        }
    }
};

///
/// \class  MainComponent
/// \brief  The main component of our application.
///
class MainComponent extends React.Component {
    ///
    /// \brief  The constructor.
    ///
    constructor () {
        super();
    }

    ///
    /// \fn     render
    /// \brief  Renders the component.
    ///
    render () {
        return (
            <div>
                <h1>Camper Leaderboard</h1>
                <BoardComponent />
                <h6>
                    Coded by Dennis Griffin. 
                    Leaderboard powered by freeCodeCamp.
                </h6>
            </div>
        );
    }
};

const content = document.getElementById("content");
ReactDOM.render(<MainComponent />, content);
              
            
!
999px

Console