Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

160
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda