Hacker News new | ask | show | jobs
by basicallybones 1211 days ago
// frontbackend engineering tutorial

import React from 'react'; import { UsersService } from './users.service'; import { registerRoute } from 'react-backend-router';

export class UsersController extends React.Component {

constructor(props) { super(props);

  this.state = {
    createUserPayload: null,
    updateUserPayload: null
  }

  this.createUser = this.createUser.bind(this);
  this.updateUser = this.updateUser.bind(this);
 }

 componentDidMount() {
  registerRoute({
    path: '/users',
    method: 'POST',
    handler: this.createUser
  });

  registerRoute({
    path: '/users',
    method: 'PUT',
    handler: this.updateUser
  });
 }

 createUser() {
    const { req } = this.props;
    const { body } = req;
    
    this.setState({
      createUserPayload: { name: body.name }
    });
 }

 updateUser() {
  const { req } = this.props;
  const { body } = req;
  
  this.setState({
    updateUserPayload: { name: body.name }
  });
}

render() { const { createUserPayload, updateUserPayload } = this.state;

  return (
    <UsersService
      create={createUserPayload}
      update={updateUserPayload}
    />
  )
 }
}