2023-12-01 10:47:54 -05:00
|
|
|
import { tracked } from "@glimmer/tracking";
|
2022-06-17 09:01:34 -04:00
|
|
|
import Controller from "@ember/controller";
|
2023-12-01 10:47:54 -05:00
|
|
|
import { action } from "@ember/object";
|
|
|
|
import { inject as service } from "@ember/service";
|
2023-07-16 20:28:00 -04:00
|
|
|
import BookmarkModal from "discourse/components/modal/bookmark";
|
2019-09-11 10:09:41 -04:00
|
|
|
import { ajax } from "discourse/lib/ajax";
|
2023-12-01 10:47:54 -05:00
|
|
|
import { popupAjaxError } from "discourse/lib/ajax-error";
|
2024-05-21 19:18:09 -04:00
|
|
|
import { BookmarkFormData } from "discourse/lib/bookmark-form-data";
|
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";
|
2023-02-06 12:20:03 -05:00
|
|
|
import { bind } from "discourse-common/utils/decorators";
|
2019-09-11 10:09:41 -04:00
|
|
|
|
2023-02-06 12:20:03 -05:00
|
|
|
export default class GroupReportsShowController extends Controller {
|
|
|
|
@service currentUser;
|
2023-07-16 20:28:00 -04:00
|
|
|
@service modal;
|
2024-07-05 05:49:18 -04:00
|
|
|
@service router;
|
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
|
|
|
|
2024-07-05 05:49:18 -04:00
|
|
|
queryParams = ["params"];
|
|
|
|
|
2023-02-06 12:20:03 -05:00
|
|
|
explain = false;
|
2022-12-28 10:50:55 -05:00
|
|
|
|
2024-07-05 05:49:18 -04:00
|
|
|
get parsedParams() {
|
|
|
|
return this.params ? JSON.parse(this.params) : null;
|
|
|
|
}
|
|
|
|
|
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 {
|
2024-07-05 05:49:18 -04:00
|
|
|
const stringifiedParams = JSON.stringify(this.model.params);
|
|
|
|
this.router.transitionTo({
|
|
|
|
queryParams: {
|
|
|
|
params: this.model.params ? stringifiedParams : null,
|
|
|
|
},
|
|
|
|
});
|
2023-02-06 12:20:03 -05:00
|
|
|
const response = await ajax(
|
|
|
|
`/g/${this.get("group.name")}/reports/${this.model.id}/run`,
|
|
|
|
{
|
|
|
|
type: "POST",
|
|
|
|
data: {
|
2024-07-05 05:49:18 -04:00
|
|
|
params: stringifiedParams,
|
2023-02-06 12:20:03 -05: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 {
|
|
|
|
popupAjaxError(error);
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
this.loading = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
toggleBookmark() {
|
2023-07-16 20:28:00 -04:00
|
|
|
const modalBookmark =
|
2023-02-06 12:20:03 -05:00
|
|
|
this.queryGroupBookmark ||
|
2023-07-16 20:28:00 -04: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-21 19:18:09 -04:00
|
|
|
afterSave: (bookmarkFormData) => {
|
|
|
|
const bookmark = this.store.createRecord(
|
|
|
|
"bookmark",
|
|
|
|
bookmarkFormData.saveData
|
|
|
|
);
|
2023-02-06 12:20:03 -05:00
|
|
|
this.queryGroupBookmark = bookmark;
|
|
|
|
this.appEvents.trigger(
|
|
|
|
"bookmarks:changed",
|
2024-05-21 19:18:09 -04:00
|
|
|
bookmarkFormData.saveData,
|
2023-02-06 12:20:03 -05:00
|
|
|
bookmark.attachedTo()
|
|
|
|
);
|
|
|
|
},
|
2023-07-16 20:28:00 -04:00
|
|
|
afterDelete: () => {
|
2023-02-06 12:20:03 -05:00
|
|
|
this.queryGroupBookmark = null;
|
|
|
|
},
|
2023-07-16 20:28:00 -04:00
|
|
|
},
|
|
|
|
});
|
2023-02-06 12:20:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
}
|