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(?).
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.