Skip to content

Instantly share code, notes, and snippets.

@vaibhavsethia
Created January 11, 2021 13:46
Show Gist options
  • Save vaibhavsethia/2776ae1f711f64ec9d0a7a6d6a74563b to your computer and use it in GitHub Desktop.
Save vaibhavsethia/2776ae1f711f64ec9d0a7a6d6a74563b to your computer and use it in GitHub Desktop.
Get Games endpoint for Twitch API using Ant Design for frontend components
import React, { Component } from 'react'
import { Input, Row, Col, Button } from 'antd';
import TwitchAPI from './TwitchAPI';
export class GetGames extends Component {
constructor(props) {
super(props)
this.state = {
SearchField : '',
GameName : '',
GameID : '',
Art : '',
}
this.GetGame = this.GetGame.bind(this);
}
async GetGame(){
let Result = await TwitchAPI.get(`https://api.twitch.tv/helix/games?name=${this.state.SearchField}`)
Result.data.data.map(Game => {
let NewURL = Game.box_art_url.replace('{width}', '300').replace('{height}', '180')
Game.box_art_url = NewURL
})
this.setState({
GameID : Result.data.data[0].id,
GameName : Result.data.data[0].name,
Art : Result.data.data[0].box_art_url,
})
}
render() {
return (
<div>
<Row>
<Col md={18} lg={20}><Input type="text" placeholder="Enter Game Name" width={250} onChange={(e)=>this.setState({SearchField : e.target.value})}/></Col>
<Col md={6} lg={4}><Button onClick={this.GetGame}>Search</Button></Col>
</Row>
<div className="pa3 mt2">
<strong>ID : </strong><span>{this.state.GameID}</span>
<br />
<strong>Name : </strong><span>{this.state.GameName}</span>
<br />
<strong>Game Art : </strong>
<div>
<img src={this.state.Art} alt="Twitch Picture"></img>
</div>
</div>
</div>
)
}
}
export default GetGames
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment