From aabeeb39df3d2dae4708acfdd6cc669428d1c937 Mon Sep 17 00:00:00 2001 From: Jeff Ohrstrom Date: Wed, 17 Apr 2024 10:28:31 -0400 Subject: [PATCH] simple refactor to promote reuse --- .../app/javascript/batch_connect_sessions.js | 17 ++--------------- apps/dashboard/app/javascript/turbo_shim.js | 12 ++++++++++++ .../views/batch_connect/sessions/index.html.erb | 2 +- 3 files changed, 15 insertions(+), 16 deletions(-) diff --git a/apps/dashboard/app/javascript/batch_connect_sessions.js b/apps/dashboard/app/javascript/batch_connect_sessions.js index 890dd20a67..22e74c643e 100644 --- a/apps/dashboard/app/javascript/batch_connect_sessions.js +++ b/apps/dashboard/app/javascript/batch_connect_sessions.js @@ -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'; @@ -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); diff --git a/apps/dashboard/app/javascript/turbo_shim.js b/apps/dashboard/app/javascript/turbo_shim.js index 029f1548f5..995d65c5e1 100644 --- a/apps/dashboard/app/javascript/turbo_shim.js +++ b/apps/dashboard/app/javascript/turbo_shim.js @@ -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); + }); +} diff --git a/apps/dashboard/app/views/batch_connect/sessions/index.html.erb b/apps/dashboard/app/views/batch_connect/sessions/index.html.erb index 8b808928f0..0f4111d4b1 100644 --- a/apps/dashboard/app/views/batch_connect/sessions/index.html.erb +++ b/apps/dashboard/app/views/batch_connect/sessions/index.html.erb @@ -36,7 +36,7 @@ locals: {
- <%= render(partial: 'panels', locals: { use_template: false }) %> + <%= render(partial: 'panels') %>
<%- else -%>