react-rails icon indicating copy to clipboard operation
react-rails copied to clipboard

How to specify methods post and delete?

Open morenoh149 opened this issue 11 years ago • 2 comments

I have the following navbar

var Navbar = ReactBootstrap.Navbar,
Nav = ReactBootstrap.Nav,
NavItem = ReactBootstrap.NavItem,
DropdownButton = ReactBootstrap.DropdownButton,
Button = ReactBootstrap.Button,
MenuItem = ReactBootstrap.MenuItem;

var NavbarInstance = React.createClass({
  propTypes: {
    userSignedIn: React.PropTypes.bool
  },

  render: function() {
    var message;
    if (this.props.userSignedIn) {
      message = (
        <NavItem href='/logout'>
          <Button
            bsStyle="secondary">
            Log me out!
          </Button>
        </NavItem>
      );
    } else {
      message = (
        <NavItem href='/auth/linkedin' data-method='post'>
          <Button
            bsStyle="primary">
            Login with LinkedIn
          </Button>
        </NavItem>
      );
    }

    return (
      <Navbar>
        <Nav>
          <NavItem eventKey={1} href="#">Home</NavItem>
          <DropdownButton eventKey={3} title="Dropdown">
            <MenuItem eventKey="1">Action</MenuItem>
            <MenuItem eventKey="2">Another action</MenuItem>
            <MenuItem eventKey="3">Something else here</MenuItem>
            <MenuItem divider />
            <MenuItem eventKey="4">Separated link</MenuItem>
          </DropdownButton>
        </Nav>
        <Nav className="navbar-right">
            { message }
        </Nav>
      </Navbar>
    );
  }
});

I'm just curious if there are any helpers for dealing with http methods the same way rails itself does? or perhaps I should ask in react-bootstrap?

Re https://github.com/react-bootstrap/react-bootstrap/issues/393

morenoh149 avatar Feb 18 '15 04:02 morenoh149

Unless I'm misunderstanding your question, react supports the following standard html attributes:

accept acceptCharset accessKey action allowFullScreen allowTransparency alt
async autoComplete autoFocus autoPlay cellPadding cellSpacing charSet checked classID
className cols colSpan content contentEditable contextMenu controls coords
crossOrigin data dateTime defer dir disabled download draggable encType form
formAction formEncType formMethod formNoValidate formTarget frameBorder height
hidden href hrefLang htmlFor httpEquiv icon id label lang list loop manifest
marginHeight marginWidth max maxLength media mediaGroup method min multiple
muted name noValidate open pattern placeholder poster preload radioGroup
readOnly rel required role rows rowSpan sandbox scope scrolling seamless
selected shape size sizes span spellCheck src srcDoc srcSet start step style
tabIndex target title type useMap value width wmode

So your navItem would need to return a <form> with whatever method you needed. I'm not sure I understand your React rendering, as it seems like the rendering should happen in each subcomponent, as opposed to a top level html-like structure. E.g. Navbar should render Nav, Nav should render NavItem, etc. But I also don't use react-bootstrap, so I have no idea if that's what they suggest to do.

jmagoon avatar Feb 19 '15 19:02 jmagoon

I ended up solving it like this

// app/assets/javascripts/components/logout_form.js.jsx
var Button = ReactBootstrap.Button;

var LogoutForm = React.createClass({
  render: function() {
    return (
      <form ref="form"
        className='form'
        action={ this.props.form.action }
        method='post'
        onSubmit={this.handleSubmit}>
        <input
          type="hidden"
          name={ this.props.form.csrf_param }
          value={ this.props.form.csrf_token } />
        <Button bsStyle="default" type='submit'>Log out</Button>
      </form>
    );
  },

  handleSubmit: function(e) {
    e.preventDefault();
    var formData = $( this.refs.form.getDOMNode() ).serialize();
    this.props.onLogoutSubmit( formData, this.props.form.action );
  }
});
// app/assets/javascripts/components/navbar.js.jsx
var Navbar = ReactBootstrap.Navbar,
Nav = ReactBootstrap.Nav,
NavItem = ReactBootstrap.NavItem,
DropdownButton = ReactBootstrap.DropdownButton,
Button = ReactBootstrap.Button,
MenuItem = ReactBootstrap.MenuItem;

var NavbarInstance = React.createClass({
  propTypes: {
    userSignedIn: React.PropTypes.number,
    presenter: React.PropTypes.string
  },

  getInitialState: function() {
    var state = JSON.parse(this.props.presenter);
    state.userSignedIn = this.props.userSignedIn; // add userSignedIn to state
    return state;
  },

  handleLogoutSubmit: function(formData, action) {
    var handler = $.ajax({
      data: formData,
      url: action,
      type: "DELETE",
      dataType: "json",
      success: function ( data ) {
        this.setState({ userSignedIn: 0 });
      }.bind(this)
    });
    handler.fail(function() {
      console.log('error');
    });
  },

  render: function() {
    var message;
    if (this.state.userSignedIn) {
      message = (
        <LogoutForm
          form={ this.state.form }
          onLogoutSubmit={ this.handleLogoutSubmit } />
      );
    } else {
      message = (
        <NavItem href='/auth/linkedin'>
          <Button
            bsStyle="primary">
            Login with LinkedIn
          </Button>
        </NavItem>
      );
    }

    return (
      <Navbar>
        <Nav>
          <NavItem eventKey={1} href="#">Home</NavItem>
          <DropdownButton eventKey={3} title="Dropdown">
            <MenuItem eventKey="1">Action</MenuItem>
            <MenuItem eventKey="2">Another action</MenuItem>
            <MenuItem eventKey="3">Something else here</MenuItem>
            <MenuItem divider />
            <MenuItem eventKey="4">Separated link</MenuItem>
          </DropdownButton>
        </Nav>
        <Nav className="navbar-right">
            { message }
        </Nav>
      </Navbar>
    );
  }
});
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8" />
  <meta name="ROBOTS" content="NOODP" />
  <meta name="viewport" content="initial-scale=1" />
  <%#
    Configure default and controller-, and view-specific titles in
    config/locales/en.yml. For more see:
    https://github.com/calebthompson/title#usage
  %>
  <title><%= title %></title>
  <%= stylesheet_link_tag :application, media: "all" %>
  <%= csrf_meta_tags %>
</head>
<body class="<%= body_class %>">
  <%= react_component('NavbarInstance',
                      {userSignedIn: user_signed_in?,
                       presenter: @presenter.to_json}) %>
  <div class="container">
    <%= render "flashes" -%>
    <%= yield %>
    <%= render "javascript" %>
  </div>
</body>
</html>

morenoh149 avatar Feb 19 '15 22:02 morenoh149