Skip to content

Commit 30167b9

Browse files
committed
Add ReloadableController and modify one page to use it
1 parent 5d24ff2 commit 30167b9

7 files changed

Lines changed: 98 additions & 25 deletions

File tree

app/controllers/application_controller/wait_for_task.rb

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,29 @@ def wait_for_task
2323
end
2424
end
2525

26-
def browser_refresh_task(task_id, async_interval, should_flash: false)
26+
def browser_refresh_task(task_id, async_interval, should_flash: false, response_type: nil)
27+
response_type ||= :script
28+
2729
async_interval = 1000 if async_interval.to_i < 1000 # if it is not an integer, assign to 1 second
2830
async_interval += 250 if async_interval.to_i < 5000 # Slowly move up to 5 second retries
29-
render :update do |page|
30-
page << javascript_prologue
31-
ajax_call = remote_function(:url => {:action => 'wait_for_task', :task_id => task_id, :async_interval => async_interval})
32-
page << "setTimeout(\"#{ajax_call}\", #{async_interval});"
33-
page.replace("flash_msg_div", :partial => "layouts/flash_msg") if should_flash
34-
page << "miqScrollTop();" if @flash_array.present?
31+
32+
case response_type
33+
when :script
34+
render :update do |page|
35+
page << javascript_prologue
36+
ajax_call = remote_function(:url => {:action => 'wait_for_task', :task_id => task_id, :async_interval => async_interval})
37+
page << "setTimeout(\"#{ajax_call}\", #{async_interval});"
38+
page.replace("flash_msg_div", :partial => "layouts/flash_msg") if should_flash
39+
page << "miqScrollTop();" if @flash_array.present?
40+
end
41+
when :json
42+
render :json => {
43+
:async_interval => async_interval,
44+
:task_id => task_id,
45+
:should_flash => should_flash
46+
}
47+
else
48+
raise "Invalid response type: #{response_type}"
3549
end
3650
end
3751
private :browser_refresh_task
@@ -65,7 +79,7 @@ def initiate_wait_for_task(options = {})
6579
task.context_data = (task.context_data || {}).merge(:async_params => async_params)
6680
task.save!
6781

68-
browser_refresh_task(task_id, async_interval, :should_flash => !!options[:flash])
82+
browser_refresh_task(task_id, async_interval, :should_flash => !!options[:flash], :response_type => options[:response_type])
6983
end
7084
private :initiate_wait_for_task
7185

app/controllers/report_controller/reports.rb

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ def miq_report_run
2222

2323
def show_preview
2424
assert_privileges(session.fetch_path(:edit, :rpt_id) ? "miq_report_edit" : "miq_report_new")
25-
2625
unless params[:task_id] # First time thru, kick off the report generate task
2726
@rpt = create_report_object # Build a report object from the latest edit fields
2827
initiate_wait_for_task(:task_id => @rpt.async_generate_table(:userid => session[:userid],
2928
:session_id => request.session_options[:id],
3029
:limit => 50,
31-
:mode => "adhoc"))
30+
:mode => "adhoc"),
31+
:response_type => :json)
3232
return
3333
end
3434
miq_task = MiqTask.find(params[:task_id]) # Not first time, read the task record
@@ -49,11 +49,7 @@ def show_preview
4949
end
5050
end
5151
miq_task.destroy
52-
render :update do |page|
53-
page << javascript_prologue
54-
page.replace_html("form_preview", :partial => "form_preview")
55-
page << "miqSparkle(false);"
56-
end
52+
render :partial => "form_preview", :layout => false
5753
end
5854

5955
def miq_report_delete

app/javascript/controllers/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@
33
// ./bin/rails generate stimulus controllerName
44

55
import { application } from "./application"
6+
7+
import ReloadableController from "./reloadable_controller"
8+
application.register("reloadable", ReloadableController)
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { Controller } from "@hotwired/stimulus"
2+
import { API } from '../http_api'
3+
import { miqFetch, addSearchParams } from '../http_api/fetch'
4+
5+
export default class extends Controller {
6+
static targets = ["content"]
7+
static values = {url: String}
8+
9+
async reload() {
10+
try {
11+
window.miqSparkleOn()
12+
13+
// request the new content and get back a task id
14+
const data = await miqFetch({
15+
url: this.urlValue,
16+
method: 'POST',
17+
cookieAndCsrf: true
18+
})
19+
20+
const task_id = data.task_id
21+
if (!task_id) {
22+
throw new Error('No task_id returned from server')
23+
}
24+
25+
// poll for the task status
26+
await API.wait_for_task(task_id)
27+
28+
// fetch the task content
29+
const resultsUrl = addSearchParams(this.urlValue, {task_id})
30+
const taskContent = await miqFetch({
31+
url: resultsUrl,
32+
method: 'POST',
33+
cookieAndCsrf: true,
34+
skipJsonParsing: true
35+
})
36+
37+
if (taskContent && this.hasContentTarget) {
38+
if (typeof taskContent === 'string') {
39+
this.contentTarget.innerHTML = taskContent
40+
} else if (taskContent.text) {
41+
this.contentTarget.innerHTML = await taskContent.text()
42+
}
43+
}
44+
45+
window.miqSparkleOff()
46+
} catch (error) {
47+
console.error('Error in reloadable#reload:', error)
48+
window.miqSparkleOff()
49+
50+
if (window.add_flash) {
51+
window.add_flash(__('Error reloading content'), 'error')
52+
}
53+
}
54+
}
55+
}

app/javascript/http_api/api.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import { miqFetch } from './fetch';
22

3-
const { miqDeferred } = window;
4-
53
/*
64
* API.get(url, options) - use API.get('/api'), returns a Promise
75
* API.delete - (the same)
@@ -63,7 +61,7 @@ API.ws_init = () => API.get('/api/auth?requester_type=ws').then((response) => {
6361
});
6462

6563
API.wait_for_task = (taskId) => {
66-
const deferred = miqDeferred();
64+
const deferred = window.miqDeferred();
6765

6866
const retry = () => {
6967
API.get(`/api/tasks/${taskId}?attributes=task_results`)

app/javascript/http_api/fetch.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,3 +141,12 @@ function errorModal(err, skipErrors, backendName) {
141141
console.error('API: Server returned a non-200 response:', err.status, err.statusText, err);
142142
}
143143
}
144+
145+
export function addSearchParams(path, params) {
146+
const tempUrl = new URL(path, 'http://dummy')
147+
for (const key of Object.keys(params)) {
148+
const value = params[key]
149+
tempUrl.searchParams.set(key, value)
150+
}
151+
return tempUrl.pathname + tempUrl.search
152+
}

app/views/report/_form_preview.html.haml

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#form_preview
1+
#form_preview{"data-controller" => "reloadable", "data-reloadable-url-value" => url_for(:action => "show_preview", :id => "#{@edit[:rpt_id] || 'new'}"), "data-reloadable-target" => "content"}
22
- if @html
33
- unless @edit[:chart_data].nil?
44
%fieldset
@@ -15,10 +15,8 @@
1515
= _('Generate Report Preview')
1616
- t = _("Generate Report preview")
1717
&nbsp;
18-
= link_to({:action => "show_preview", :id => "#{@edit[:rpt_id] || 'new'}"},
19-
:alt => t,
20-
"data-miq_sparkle_on" => true,
21-
:remote => true,
22-
"data-method" => :post,
23-
:title => t) do
18+
= link_to("#",
19+
:alt => t,
20+
:title => t,
21+
"data-action" => "click->reloadable#reload") do
2422
%i.fa.fa-refresh

0 commit comments

Comments
 (0)