Created
January 11, 2021 13:46
-
-
Save vaibhavsethia/2776ae1f711f64ec9d0a7a6d6a74563b to your computer and use it in GitHub Desktop.
Get Games endpoint for Twitch API using Ant Design for frontend components
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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