Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

167
Vistas
handling API request in views, from alpine to possibly stimulus

I have currently set up alpine to make a POST request to a certain api in my views with the x-init method. The method is quite simple using fetch and then handling the response.

function getDocumentStatus() {
   return {
   isLoading: false,
   status: null,
   fetchStatus() {
      this.isLoading = true;
      fetch('API_URL', {
         method: 'POST',
         headers: {
            'Content-Type': 'application/json',
            'Access-Control-Allow-Origin': '*'
         },
         body: JSON.stringify({document_uuid: '<%= @document_uuid %>'}),
         })
         .then(response => response.json())
         .then(data => {
         this.isLoading = false;
         if (data === 'pending') {
            this.status = 'Waiting for results'
         } else if (data === 'signed') {
            this.status = 'Results signed'
         } else if (data === 'new') {
            this.status = 'Sent'
         }
      })
   }
   }
}

However I've recently found a ruby wrapper for this api and since I'm doing also quite a lot more with the particular api I've started to incorporate the wrapper to my app.

Using this wrapper I can simply call

client.document.status(document_id: xxx)

to fetch the status for a document. I'm trying to figure out how can I use this to replace the current logic in the views and get rid of alpine to possibly something like a stimulus controller? Any suggestions on how to approach this would be appreciated!

Something I've looked at is https://stimulus.hotwired.dev/handbook/working-with-external-resources but it seems that I would need to have something like a controller action

def status
    client.document.status(document_id: xxx)
end

and then a partial for this action which I would then render via stimulus on the view I want to show the results? Seems to me a bit that this would just again be something that wouldn't really belong to the controller(?).

about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You can try using Sidekiq for processing stuff in the background. Then in your front-end you can update the page as soon as it's done. This sounds simple enough, however Sidekiq doesn't provide anything to monitor statuses of your jobs. In pro version you can use batches and there, you have a callback functionality, however you can check out recommended gems to implement that.

about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda