Skip to content

Commit

Permalink
simple refactor to promote reuse
Browse files Browse the repository at this point in the history
  • Loading branch information
johrstrom committed Apr 17, 2024
1 parent 7b7501c commit aabeeb3
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 16 deletions.
17 changes: 2 additions & 15 deletions apps/dashboard/app/javascript/batch_connect_sessions.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,7 @@
'use strict';

import { bcIndexUrl, bcPollDelay } from './config';
import { replaceHTML } from './turbo_shim';

function poll() {
url = bcIndexUrl();
fetch(url, { headers: { Accept: "text/vnd.turbo-stream.html" } })
.then(response => response.ok ? Promise.resolve(response) : Promise.reject(response.text()))
.then((r) => r.text())
.then((html) => replaceHTML("batch_connect_sessions", html))
.then(setTimeout(poll, bcPollDelay()))
.catch((err) => {
console.log('Cannot not retrive batch connect sessions due to error:');
console.log(err);
});
}
import { pollAndReplace } from './turbo_shim';

function settingKey(id) {
return id + '_value';
Expand Down Expand Up @@ -52,7 +39,7 @@ jQuery(function (){
$('#full-page-spinner').removeClass('d-none');
}

poll();
pollAndReplace(bcIndexUrl(), bcPollDelay(), "batch_connect_sessions");

$('button.relaunch').each((index, element) => {
$(element).on('click', showSpinner);
Expand Down
12 changes: 12 additions & 0 deletions apps/dashboard/app/javascript/turbo_shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,15 @@ export function replaceHTML(id, html) {
ele.innerHTML = newHTML;
}
}

export function pollAndReplace(url, delay, id) {
fetch(url, { headers: { Accept: "text/vnd.turbo-stream.html" } })
.then(response => response.ok ? Promise.resolve(response) : Promise.reject(response.text()))
.then((r) => r.text())
.then((html) => replaceHTML(id, html))
.then(setTimeout(pollAndReplace, delay, url, delay, id))
.catch((err) => {
console.log('Cannot not retrive partial due to error:');
console.log(err);
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ locals: {
</div>
<div class="col-md-9">
<div id="batch_connect_sessions">
<%= render(partial: 'panels', locals: { use_template: false }) %>
<%= render(partial: 'panels') %>
</div>
</div>
<%- else -%>
Expand Down

0 comments on commit aabeeb3

Please sign in to comment.