SHARE
TWEET

Untitled

a guest Nov 12th, 2019 64 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React from 'react';
  2.  
  3. import BadgeDetails from './BadgeDetails';
  4. import PageLoading from '../components/PageLoading';
  5. import PageError from '../components/PageError';
  6. import api from '../api';
  7.  
  8. class BadgeDetailsContainer extends React.Component {
  9.   state = {
  10.     loading: true,
  11.     error: null,
  12.     data: undefined,
  13.     modalIsOpen: false,
  14.   };
  15.  
  16.   componentDidMount() {
  17.     this.fetchData();
  18.   }
  19.  
  20.   fetchData = async () => {
  21.     this.setState({ loading: true, error: null });
  22.  
  23.     try {
  24.       const data = await api.badges.read(this.props.match.params.badgeId);
  25.       this.setState({ loading: false, data: data });
  26.     } catch (error) {
  27.       this.setState({ loading: false, error: error });
  28.     }
  29.   };
  30.  
  31.   handleOpenModal = e => {
  32.     this.setState({ modalIsOpen: true });
  33.   };
  34.  
  35.   handleCloseModal = e => {
  36.     this.setState({ modalIsOpen: false });
  37.   };
  38.  
  39.   handleDeleteBadge = async e => {
  40.     this.setState({ loading: true, error: null });
  41.  
  42.     try {
  43.       await api.badges.remove(this.props.match.params.badgeId);
  44.       this.setState({ loading: false });
  45.  
  46.       this.props.history.push('/badges');
  47.     } catch (error) {
  48.       this.setState({ loading: false, error: error });
  49.     }
  50.   };
  51.  
  52.   render() {
  53.     if (this.state.loading) {
  54.       return <PageLoading />;
  55.     }
  56.  
  57.     if (this.state.error) {
  58.       return <PageError error={this.state.error} />;
  59.     }
  60.  
  61.     return (
  62.       <BadgeDetails
  63.         onCloseModal={this.handleCloseModal}
  64.         onOpenModal={this.handleOpenModal}
  65.         modalIsOpen={this.state.modalIsOpen}
  66.         onDeleteBadge={this.handleDeleteBadge}
  67.         badge={this.state.data}
  68.       />
  69.     );
  70.   }
  71. }
  72.  
  73. export default BadgeDetailsContainer;
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top