mercredi 31 mars 2021

React Rails : How to get the results of an API Call

I need the result of an api call to another server for a react component: let's say for now I just need to display either true or false as text

I have the following component:

// some_random_thing.coffee

    { div, h5 } = DOM
    actions = require '../../actions'
    
    class MfaQRCode extends React.Component
      @propTypes: {}
    
    
      constructor: ->
        super arguments...
        @validationTimeout = null
  shouldComponentUpdate: -> true

  render:->
    div {},
      @renderStatus()

  renderStatus: ->
    h5 {className: 'column-header'},
      "BOOLEAN::"
        @props.resource.boolean_record

Which is a child component of something else; Then to populate this boolean_record I have in my actions.coffee file

    api = require('./api')
    AddUserPopupActions = ReactUtils.actions.creators.popup
      somethingExists: { asyncResult: true }

AddUserPopupActions.somethingExists.listen ->
  @promise(api.somethingExists(arguments...))

In api.coffee

    api = ReactUtils.api
    
    class Api
    
    somethingExists: ->
      api.get "/servers/users/something/exists"

This get request is handled in a rails controller which I believe to be fine. My question is, how can I populate a variable so that I can use the result of the request which returns true or false from another server.

There is a method in a file named store.coffee where the resource component of props is populated I need to populate this with the result of the api request: have tried:

  onSomethingExists: (response) ->   
    @resource.boolean_record = response.responseJSON.result   
    @_update()          

But this method is not getting triggered at all it seems upon debugging. I'm very new to react and do not fully understand how it all works and given the fact coffeescript and rails are in the mix there is little information to be found on how to do this.

Aucun commentaire:

Enregistrer un commentaire