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";
|
|
|
|
import { BookmarkFormData } from "discourse/lib/bookmark";
|
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;
|
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() {
|
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),
|
|
|
|
afterSave: (savedData) => {
|
2023-02-06 12:20:03 -05:00
|
|
|
const bookmark = this.store.createRecord("bookmark", savedData);
|
|
|
|
this.queryGroupBookmark = bookmark;
|
|
|
|
this.appEvents.trigger(
|
|
|
|
"bookmarks:changed",
|
|
|
|
savedData,
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|