FEATURE: Add Data Explorer Params to the URL on run (#128)

FEATURE: Add Data Explorer Params to the URL on run (#128)

Co-authored-by: Mark VanLandingham markvanlan@gmail.com

diff --git a/assets/javascripts/discourse/components/param-input.js.es6 b/assets/javascripts/discourse/components/param-input.js.es6
index e6947db..97dd855 100644
--- a/assets/javascripts/discourse/components/param-input.js.es6
+++ b/assets/javascripts/discourse/components/param-input.js.es6
@@ -43,6 +43,15 @@ export default Ember.Component.extend({
     { name: I18n.t("explorer.types.bool.false"), id: "N" },
     { name: I18n.t("explorer.types.bool.null_"), id: "#null" },
   ],
+  initialValues: null,
+
+  init() {
+    this._super(...arguments);
+
+    if (this.initialValues && this.info.identifier in this.initialValues) {
+      this.set("value", this.initialValues[this.info.identifier]);
+    }
+  },
 
   value: Ember.computed("params", "info.identifier", {
     get() {
diff --git a/assets/javascripts/discourse/controllers/admin-plugins-explorer.js.es6 b/assets/javascripts/discourse/controllers/admin-plugins-explorer.js.es6
index 8075026..e119caa 100644
--- a/assets/javascripts/discourse/controllers/admin-plugins-explorer.js.es6
+++ b/assets/javascripts/discourse/controllers/admin-plugins-explorer.js.es6
@@ -12,7 +12,7 @@ import { Promise } from "rsvp";
 const NoQuery = Query.create({ name: "No queries", fake: true, group_ids: [] });
 
 export default Ember.Controller.extend({
-  queryParams: { selectedQueryId: "id" },
+  queryParams: { selectedQueryId: "id", params: "params" },
   selectedQueryId: null,
   editDisabled: false,
   showResults: false,
@@ -32,6 +32,11 @@ export default Ember.Controller.extend({
   sortBy: ["last_run_at:desc"],
   sortedQueries: Ember.computed.sort("model", "sortBy"),
 
+  @computed("params")
+  parsedParams(params) {
+    return params ? JSON.parse(params) : null;
+  },
+
   @computed
   acceptedImportFileTypes() {
     return ["application/json"];
@@ -207,10 +212,12 @@ export default Ember.Controller.extend({
         order: null,
         showResults: false,
         editDisabled: false,
+        showRecentQueries: true,
         selectedQueryId: null,
+        params: null,
         sortBy: ["last_run_at:desc"],
       });
-      this.transitionToRoute({ queryParams: { id: null } });
+      this.transitionToRoute({ queryParams: { id: null, params: null } });
     },
 
     showHelpModal() {
@@ -303,7 +310,11 @@ export default Ember.Controller.extend({
         return;
       }
 
-      this.setProperties({ loading: true, showResults: false });
+      this.setProperties({
+        loading: true,
+        showResults: false,
+        params: JSON.stringify(this.selectedItem.params),
+      });
       ajax(
         "/admin/plugins/explorer/queries/" +
           this.get("selectedItem.id") +
diff --git a/assets/javascripts/discourse/templates/admin/plugins-explorer.hbs b/assets/javascripts/discourse/templates/admin/plugins-explorer.hbs
index 5923e92..d05debe 100644
--- a/assets/javascripts/discourse/templates/admin/plugins-explorer.hbs
+++ b/assets/javascripts/discourse/templates/admin/plugins-explorer.hbs
@@ -149,7 +149,11 @@
           {{#if selectedItem.hasParams}}
             <div class="query-params">
               {{#each selectedItem.param_info as |pinfo|}}
-                {{param-input params=selectedItem.params info=pinfo}}
+                {{param-input
+                  params=selectedItem.params
+                  initialValues=parsedParams
+                  info=pinfo
+                }}
               {{/each}}
             </div>
           {{/if}}
diff --git a/test/javascripts/acceptance/run-query-test.js.es6 b/test/javascripts/acceptance/run-query-test.js.es6
index 5b51fae..5e0c656 100644
--- a/test/javascripts/acceptance/run-query-test.js.es6
+++ b/test/javascripts/acceptance/run-query-test.js.es6
@@ -4,6 +4,7 @@ import {
   query,
   queryAll,
 } from "discourse/tests/helpers/qunit-helpers";
+import { click, fillIn, visit } from "@ember/test-helpers";
 import { clearPopupMenuOptionsCallback } from "discourse/controllers/composer";
 import I18n from "I18n";
 
@@ -188,4 +189,15 @@ acceptance("Data Explorer Plugin | Run Query", function (needs) {
 
     assert.ok(exists("canvas"), "the chart was rendered");
   });
+
+  test("it puts params for the query into the url", async function (assert) {
+    await visit("admin/plugins/explorer?id=-6");
+    const monthsAgoValue = "2";
+    await fillIn(".query-params input", monthsAgoValue);
+    await click("form.query-run button");
+
+    let searchParams = new URLSearchParams(currentURL());
+    let paramsMonthsAgo = JSON.parse(searchParams.get("params")).months_ago;
+    assert.equal(paramsMonthsAgo, monthsAgoValue);
+  });
 });

GitHub sha: 286b5b5fe73e21bca38c645b31db67af650750b8

This commit appears in #128 which was approved by markvanlan. It was merged by markvanlan.