2022-06-17 09:01:34 -04:00
|
|
|
import Controller from "@ember/controller";
|
2019-09-11 10:09:41 -04:00
|
|
|
import { popupAjaxError } from "discourse/lib/ajax-error";
|
|
|
|
import { ajax } from "discourse/lib/ajax";
|
2023-02-06 12:20:03 -05:00
|
|
|
import {
|
2022-06-14 11:07:02 -04:00
|
|
|
NO_REMINDER_ICON,
|
|
|
|
WITH_REMINDER_ICON,
|
|
|
|
} from "discourse/models/bookmark";
|
|
|
|
import { openBookmarkModal } from "discourse/controllers/bookmark";
|
2023-02-06 12:20:03 -05:00
|
|
|
import { action } from "@ember/object";
|
|
|
|
import { bind } from "discourse-common/utils/decorators";
|
|
|
|
import { tracked } from "@glimmer/tracking";
|
|
|
|
import { inject as service } from "@ember/service";
|
2019-09-11 10:09:41 -04:00
|
|
|
|
2023-02-06 12:20:03 -05:00
|
|
|
export default class GroupReportsShowController extends Controller {
|
|
|
|
@service currentUser;
|
2019-09-11 10:09:41 -04:00
|
|
|
|
2023-02-06 12:20:03 -05:00
|
|
|
@tracked showResults = false;
|
|
|
|
@tracked loading = false;
|
|
|
|
@tracked results = this.model.results;
|
|
|
|
@tracked queryGroupBookmark = this.queryGroup?.bookmark;
|
2022-06-14 11:07:02 -04:00
|
|
|
|
2023-02-06 12:20:03 -05:00
|
|
|
explain = false;
|
2022-12-28 10:50:55 -05:00
|
|
|
|
2023-02-06 12:20:03 -05:00
|
|
|
get hasParams() {
|
|
|
|
return this.model.param_info.length > 0;
|
|
|
|
}
|
2022-06-14 11:07:02 -04:00
|
|
|
|
2023-02-06 12:20:03 -05:00
|
|
|
get bookmarkLabel() {
|
|
|
|
return this.queryGroupBookmark
|
|
|
|
? "bookmarked.edit_bookmark"
|
|
|
|
: "bookmarked.title";
|
|
|
|
}
|
2022-06-14 11:07:02 -04:00
|
|
|
|
2023-02-06 12:20:03 -05:00
|
|
|
get bookmarkIcon() {
|
|
|
|
if (this.queryGroupBookmark && this.queryGroupBookmark.reminder_at) {
|
2022-06-14 11:07:02 -04:00
|
|
|
return WITH_REMINDER_ICON;
|
|
|
|
}
|
|
|
|
return NO_REMINDER_ICON;
|
2023-02-06 12:20:03 -05:00
|
|
|
}
|
2022-06-14 11:07:02 -04:00
|
|
|
|
2023-02-06 12:20:03 -05:00
|
|
|
get bookmarkClassName() {
|
|
|
|
return this.queryGroupBookmark
|
2022-08-18 12:07:35 -04:00
|
|
|
? ["query-group-bookmark", "bookmarked"].join(" ")
|
|
|
|
: "query-group-bookmark";
|
2023-02-06 12:20:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@bind
|
|
|
|
async run() {
|
|
|
|
this.loading = true;
|
|
|
|
this.showResults = false;
|
|
|
|
|
|
|
|
try {
|
|
|
|
const response = await ajax(
|
|
|
|
`/g/${this.get("group.name")}/reports/${this.model.id}/run`,
|
|
|
|
{
|
|
|
|
type: "POST",
|
|
|
|
data: {
|
|
|
|
params: JSON.stringify(this.model.params),
|
|
|
|
explain: this.explain,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
this.results = response;
|
|
|
|
if (!response.success) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.showResults = true;
|
|
|
|
} catch (error) {
|
|
|
|
if (error.jqXHR?.status === 422 && error.jqXHR.responseJSON) {
|
|
|
|
this.results = error.jqXHR.responseJSON;
|
|
|
|
} else {
|
|
|
|
popupAjaxError(error);
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
this.loading = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
toggleBookmark() {
|
|
|
|
return openBookmarkModal(
|
|
|
|
this.queryGroupBookmark ||
|
|
|
|
this.store.createRecord("bookmark", {
|
2023-03-22 17:29:08 -04:00
|
|
|
bookmarkable_type: "DiscourseDataExplorer::QueryGroup",
|
2023-02-06 12:20:03 -05:00
|
|
|
bookmarkable_id: this.queryGroup.id,
|
|
|
|
user_id: this.currentUser.id,
|
|
|
|
}),
|
|
|
|
{
|
|
|
|
onAfterSave: (savedData) => {
|
|
|
|
const bookmark = this.store.createRecord("bookmark", savedData);
|
|
|
|
this.queryGroupBookmark = bookmark;
|
|
|
|
this.appEvents.trigger(
|
|
|
|
"bookmarks:changed",
|
|
|
|
savedData,
|
|
|
|
bookmark.attachedTo()
|
|
|
|
);
|
|
|
|
},
|
|
|
|
onAfterDelete: () => {
|
|
|
|
this.queryGroupBookmark = null;
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is necessary with glimmer's one way data stream to get the child's
|
|
|
|
// changes of 'params' to bubble up.
|
|
|
|
@action
|
|
|
|
updateParams(identifier, value) {
|
|
|
|
this.set(`model.params.${identifier}`, value);
|
|
|
|
}
|
|
|
|
}
|