handle chess.com req failure

This commit is contained in:
2023-01-23 18:59:05 -06:00
parent 9187cdb3fd
commit 5de2a2b2dd

View File

@@ -16,6 +16,7 @@ export default class ChessBed extends React.Component {
bullet: "Loading", bullet: "Loading",
tactics: "Loading", tactics: "Loading",
}, },
loaded: "hidden"
}; };
this.diamonds = { this.diamonds = {
background: "black", background: "black",
@@ -28,10 +29,24 @@ export default class ChessBed extends React.Component {
} }
async addChessEmbed(username) { async addChessEmbed(username) {
let user = await fetch(`https://api.chess.com/pub/player/${username}`); let user, stats;
let stats = await fetch( try {
`https://api.chess.com/pub/player/${username}/stats` user = await fetch(`https://api.chess.com/pub/player/${username}`);
); stats = await fetch(
`https://api.chess.com/pub/player/${username}/stats`
);
} catch (e) {
this.setState({
name: "Chess.com request failed",
ratings: {
rapid: "Site may",
blitz: "be blocked",
bullet: "by client.\n\n",
tactics: "not my fault :(",
},
});
return;
}
if (user.status === 200) { if (user.status === 200) {
user = await user.json(); user = await user.json();
stats = await stats.json(); stats = await stats.json();
@@ -44,10 +59,16 @@ export default class ChessBed extends React.Component {
bullet: stats.chess_bullet.last.rating, bullet: stats.chess_bullet.last.rating,
tactics: stats.tactics.highest.rating, tactics: stats.tactics.highest.rating,
}, },
loaded: ''
});
} else if (user === null || user.status === 403 || user.status === null) {
this.setState({
name: "Chess.com request failed"
}); });
} else { } else {
this.setState({ this.setState({
name: "User Not Found", name: "User Not Found",
loaded: ''
}); });
} }
} }
@@ -79,19 +100,19 @@ export default class ChessBed extends React.Component {
</div> </div>
<div className="vContainer chessInfo"> <div className="vContainer chessInfo">
<div className="vItem"> <div className="vItem">
<div className="chessIcon rapid"></div> <div className={"chessIcon rapid " + this.state.loaded}></div>
<p>{this.state.ratings.rapid}</p> <p>{this.state.ratings.rapid}</p>
</div> </div>
<div className="vItem"> <div className="vItem">
<div className="chessIcon blitz"></div> <div className={"chessIcon blitz " + this.state.loaded}></div>
<p>{this.state.ratings.blitz}</p> <p>{this.state.ratings.blitz}</p>
</div> </div>
<div className="vItem"> <div className="vItem">
<div className="chessIcon bullet"></div> <div className={"chessIcon bullet " + this.state.loaded}></div>
<p>{this.state.ratings.bullet}</p> <p>{this.state.ratings.bullet}</p>
</div> </div>
<div className="vItem"> <div className="vItem">
<div className="chessIcon puzzles"></div> <div className={"chessIcon puzzles " + this.state.loaded}></div>
<p>{this.state.ratings.tactics}</p> <p>{this.state.ratings.tactics}</p>
</div> </div>
</div> </div>