Git Product home page Git Product logo

koji-react-leaderboard's Introduction

๐Ÿฆ‹ koji-react-leaderboard

React Render Prop Components that fetch and save Leaderboard data to and from the Koji Backend.

Type stage NPM JavaScript Style Guide Prefers Twitter

๐Ÿ“ฆ Install

npm install -S koji-react-leaderboard

๐Ÿ“š Library

Please Note: This library is made only for those Koji users who have Koji Leaderboard API already setup on the Koji Backend.

  • GetLeaderboard React Component
  • SaveToLeaderboard React Component

๐Ÿ–ฅ๏ธ Usage

<GetLeaderboard /> Component

Props

  • kojiLeaderboardBackendUri ๐Ÿ‘‰ (required) The Koji backend URL that has Koji Leaderboard API activated. eg. Koji.config.serviceMap.backend
  • endpoint ๐Ÿ‘‰ (optional) The backend endpoint that you want to hit to get the Leaderboard Data. Default: 'leaderboard'

Example

import React, { Component } from 'react'

import { GetLeaderboard } from 'koji-react-leaderboard'

class YourComponent extends Component {
  render () {
    return (
      <GetLeaderboard kojiLeaderboardBackendUri="http://localhost:3333">
        {(data, isLoading, isError) => {
          if (data.scores && !isLoading && !isError) {
            return (
              <div className="container">
                { data.scores.map(({ name, score }, index) => (
                  <li key={index}>
                    Name: { name } | Score: { score }
                  </li>
                )) }
              </div>
            )
          }

          if (data.error && !isLoading && isError) {
            return <h2>Error occured. {data.error.message}</h2>
          }

          return <h2>Loading...</h2>
        }}
      </GetLeaderboard>
    )
  }
}

<SaveToLeaderboard /> Component

Props

  • kojiLeaderboardBackendUri ๐Ÿ‘‰ (required) The Koji backend URL that has Koji Leaderboard API activated. eg. Koji.config.serviceMap.backend
  • endpoint ๐Ÿ‘‰ (optional) The backend endpoint that you want to hit to save the Leaderboard Data. Default: 'leaderboard'

Example

import React, { Component } from 'react'

import { SaveToLeaderboard } from 'koji-react-leaderboard'

class YourComponent extends Component {
  state = { data: null }
  
  submitData = async (e, saveData) => {
    e.preventDefault()

    const response = await saveData({
      name: 'ScreamerPlays',
      score: 890,
    })

    this.setState({ data: response })
  }

  render() {
    return (
      <SaveToLeaderboard kojiLeaderboardBackendUri="http://localhost:3333">
        {(saveData, isLoading, isError) => {
          return (
            <React.Fragment>
              <button onClick={e => this.submitData(e, saveData)}>Submit data</button>
              {isLoading && <h2>Loading...</h2>}
              {isError && <h2>Error</h2>}
            </React.Fragment>
          )
        }}
      </SaveToLeaderboard>
    )
  }
}

๐Ÿ“ License

MIT ยฉ Kumar Abhirup
Follow me ๐Ÿ‘‹ on Twitter โ†’ Twitter

koji-react-leaderboard's People

Contributors

kumarabhirup avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.