Advertisement
Guest User

Untitled

a guest
Jan 18th, 2020
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. App:
  2. const App = (props) => {
  3.     return (
  4.         <BrowserRouter>
  5.             <div className='app-wrapper'>
  6.                 <Header/>
  7.                 <div className='app-wrapper-content'>
  8.                     <Route path='/accounts' component={Accounts}/>
  9.                     <Route path={'/account:id'} component={Account}/>
  10.                     <Route path='/new' component={AccountForm}/>
  11.                 </div>
  12.             </div>
  13.         </BrowserRouter>
  14.     );
  15. };
  16.  
  17.  
  18. Account:
  19. class Account extends Component {
  20.     constructor(props) {
  21.         super(props);
  22.  
  23.         this.state = {
  24.             id: this.props.match.params.id,
  25.             name: '',
  26.             email: '',
  27.             password: '',
  28.             link: ''
  29.         }
  30.  
  31.     }
  32.  
  33.     componentDidMount() {
  34.  
  35.         console.log(this.state.id)
  36.  
  37.         // eslint-disable-next-line
  38.         if (this.state.id === -1) {
  39.             return
  40.         }
  41.  
  42.         ApiService.fetchAccountById(this.state.id)
  43.             .then(response => this.setState({
  44.                 item: {
  45.                     name: response.data.name,
  46.                     email: response.data.email,
  47.                     password: response.data.password,
  48.                     link: response.data.link
  49.                 }
  50.             }))
  51.  
  52.     }
  53.  
  54.     render() {
  55.  
  56.         let acc = this.state.item;
  57.  
  58.         return (
  59.             <div>
  60.                 <h3>Account</h3>
  61.                 <div>{acc.id}</div>
  62.                 <div>{acc.name}</div>
  63.                 <div>{acc.email}</div>
  64.                 <div>{acc.password}</div>
  65.                 <div>{acc.link}</div>
  66.             </div>
  67.         )
  68.     }
  69.  
  70.  
  71. ApiService:
  72.  
  73. const ACCOUNT_API_BASE_URL = 'http://localhost:8080/api/accounts';
  74.  
  75. class ApiService {
  76.     fetchAccounts() {
  77.         return axios.get(ACCOUNT_API_BASE_URL);
  78.     }
  79.  
  80.     fetchAccountById(id) {
  81.         return axios.get(`${ACCOUNT_API_BASE_URL}/${id}`);
  82.     }
  83.  
  84.     addAccount(acc) {
  85.         return axios.post(ACCOUNT_API_BASE_URL + '/create', acc);
  86.     }
  87.  
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement