Files

138 lines
3.6 KiB
JavaScript
Raw Permalink Normal View History

2023-12-01 16:47:54 +01:00
import { tracked } from "@glimmer/tracking";
import Controller from "@ember/controller";
2023-12-01 16:47:54 +01:00
import { action } from "@ember/object";
2024-11-19 10:20:51 +00:00
import { service } from "@ember/service";
2023-07-17 10:28:00 +10:00
import BookmarkModal from "discourse/components/modal/bookmark";
2019-09-11 09:09:41 -05:00
import { ajax } from "discourse/lib/ajax";
2023-12-01 16:47:54 +01:00
import { popupAjaxError } from "discourse/lib/ajax-error";
2024-05-22 09:18:09 +10:00
import { BookmarkFormData } from "discourse/lib/bookmark-form-data";
import { bind } from "discourse/lib/decorators";
2023-02-06 11:20:03 -06:00
import {
2022-06-14 23:07:02 +08:00
NO_REMINDER_ICON,
WITH_REMINDER_ICON,
} from "discourse/models/bookmark";
import { ParamValidationError } from "discourse/plugins/discourse-data-explorer/discourse/components/param-input-form";
2019-09-11 09:09:41 -05:00
2023-02-06 11:20:03 -06:00
export default class GroupReportsShowController extends Controller {
@service currentUser;
2023-07-17 10:28:00 +10:00
@service modal;
@service router;
2019-09-11 09:09:41 -05:00
2023-02-06 11:20:03 -06:00
@tracked showResults = false;
@tracked loading = false;
@tracked results = this.model.results;
@tracked queryGroupBookmark = this.queryGroup?.bookmark;
2019-09-11 09:09:41 -05:00
queryParams = ["params"];
form = null;
2023-02-06 11:20:03 -06:00
explain = false;
2022-06-14 23:07:02 +08:00
get parsedParams() {
return this.params ? JSON.parse(this.params) : null;
}
2023-02-06 11:20:03 -06:00
get hasParams() {
return this.model.param_info.length > 0;
}
2022-12-28 09:50:55 -06:00
2023-02-06 11:20:03 -06:00
get bookmarkLabel() {
return this.queryGroupBookmark
? "bookmarked.edit_bookmark"
: "bookmarked.title";
}
2022-06-14 23:07:02 +08:00
2023-02-06 11:20:03 -06:00
get bookmarkIcon() {
if (this.queryGroupBookmark && this.queryGroupBookmark.reminder_at) {
2022-06-14 23:07:02 +08:00
return WITH_REMINDER_ICON;
}
return NO_REMINDER_ICON;
2023-02-06 11:20:03 -06:00
}
2022-06-14 23:07:02 +08:00
2023-02-06 11:20:03 -06:00
get bookmarkClassName() {
return this.queryGroupBookmark
? ["query-group-bookmark", "bookmarked"].join(" ")
: "query-group-bookmark";
2023-02-06 11:20:03 -06:00
}
@bind
async run() {
try {
let params = null;
if (this.hasParams) {
params = await this.form.submit();
if (params == null) {
return;
}
}
this.loading = true;
this.showResults = false;
const stringifiedParams = JSON.stringify(params);
this.router.transitionTo({
queryParams: {
params: params ? stringifiedParams : null,
},
});
2023-02-06 11:20:03 -06:00
const response = await ajax(
`/g/${this.get("group.name")}/reports/${this.model.id}/run`,
{
type: "POST",
data: {
params: stringifiedParams,
2023-02-06 11:20:03 -06:00
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 if (!(error instanceof ParamValidationError)) {
2023-02-06 11:20:03 -06:00
popupAjaxError(error);
}
} finally {
this.loading = false;
}
}
@action
toggleBookmark() {
2023-07-17 10:28:00 +10:00
const modalBookmark =
2023-02-06 11:20:03 -06:00
this.queryGroupBookmark ||
2023-07-17 10:28:00 +10:00
this.store.createRecord("bookmark", {
bookmarkable_type: "DiscourseDataExplorer::QueryGroup",
bookmarkable_id: this.queryGroup.id,
user_id: this.currentUser.id,
});
return this.modal.show(BookmarkModal, {
model: {
bookmark: new BookmarkFormData(modalBookmark),
2024-05-22 09:18:09 +10:00
afterSave: (bookmarkFormData) => {
const bookmark = this.store.createRecord(
"bookmark",
bookmarkFormData.saveData
);
2023-02-06 11:20:03 -06:00
this.queryGroupBookmark = bookmark;
this.appEvents.trigger(
"bookmarks:changed",
2024-05-22 09:18:09 +10:00
bookmarkFormData.saveData,
2023-02-06 11:20:03 -06:00
bookmark.attachedTo()
);
},
2023-07-17 10:28:00 +10:00
afterDelete: () => {
2023-02-06 11:20:03 -06:00
this.queryGroupBookmark = null;
},
2023-07-17 10:28:00 +10:00
},
});
2023-02-06 11:20:03 -06:00
}
@action
onRegisterApi(form) {
this.form = form;
}
2023-02-06 11:20:03 -06:00
}