Created
November 18, 2019 23:15
-
-
Save Myrrel/3c128be84ffe725b8579692f92d52fb1 to your computer and use it in GitHub Desktop.
React Get
This file contains hidden or 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 from 'react'; | |
| import { Link } from 'react-router-dom'; | |
| import './styles/Badges.css'; | |
| import confLogo from '../images/badge-header.svg'; | |
| import BadgesList from '../components/BadgesList'; | |
| import api from '../api'; | |
| class Badges extends React.Component { | |
| state = { | |
| loading: true, | |
| error: null, | |
| data: undefined, | |
| }; | |
| componentDidMount() { | |
| this.fetchData(); | |
| } | |
| fetchData = async () => { | |
| this.setState({ loading: true, error: null }); | |
| try { | |
| const data = await api.badges.list(); | |
| this.setState({ loading: false, data: data }); | |
| } catch (error) { | |
| this.setState({ loading: false, error: error }); | |
| } | |
| }; | |
| render() { | |
| if (this.state.loading === true) { | |
| return 'Loading...'; | |
| } | |
| if (this.state.error) { | |
| return `Error: ${this.state.error.message}`; | |
| } | |
| return ( | |
| <React.Fragment> | |
| <div className="Badges"> | |
| <div className="Badges__hero"> | |
| <div className="Badges__container"> | |
| <img | |
| className="Badges_conf-logo" | |
| src={confLogo} | |
| alt="Conf Logo" | |
| /> | |
| </div> | |
| </div> | |
| </div> | |
| <div className="Badges__container"> | |
| <div className="Badges__buttons"> | |
| <Link to="/badges/new" className="btn btn-primary"> | |
| New Badge | |
| </Link> | |
| </div> | |
| <BadgesList badges={this.state.data} /> | |
| </div> | |
| </React.Fragment> | |
| ); | |
| } | |
| } | |
| export default Badges; | |
| //--------------------------------------- | |
| api.js | |
| const BASE_URL = 'http://localhost:3001'; | |
| const delay = ms => new Promise(resolve => setTimeout(resolve, ms)); | |
| const randomNumber = (min = 0, max = 1) => | |
| Math.floor(Math.random() * (max - min + 1)) + min; | |
| const simulateNetworkLatency = (min = 30, max = 1500) => | |
| delay(randomNumber(min, max)); | |
| async function callApi(endpoint, options = {}) { | |
| await simulateNetworkLatency(); | |
| options.headers = { | |
| 'Content-Type': 'application/json', | |
| Accept: 'application/json', | |
| }; | |
| const url = BASE_URL + endpoint; | |
| const response = await fetch(url, options); | |
| const data = await response.json(); | |
| return data; | |
| } | |
| const api = { | |
| badges: { | |
| list() { | |
| return callApi('/badges'); | |
| }, | |
| create(badge) { | |
| return callApi(`/badges`, { | |
| method: 'POST', | |
| body: JSON.stringify(badge), | |
| }); | |
| }, | |
| read(badgeId) { | |
| return callApi(`/badges/${badgeId}`); | |
| }, | |
| update(badgeId, updates) { | |
| return callApi(`/badges/${badgeId}`, { | |
| method: 'PUT', | |
| body: JSON.stringify(updates), | |
| }); | |
| }, | |
| // Lo hubiera llamado `delete`, pero `delete` es un keyword en JavaScript asi que no es buena idea :P | |
| remove(badgeId) { | |
| return callApi(`/badges/${badgeId}`, { | |
| method: 'DELETE', | |
| }); | |
| }, | |
| }, | |
| }; | |
| export default api; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment