refactor: Store prepared_report_name state on report obj

This commit is contained in:
Ankush Menat 2024-04-23 12:30:50 +05:30
parent 71a9fa4fe9
commit b5b6a52559
2 changed files with 19 additions and 28 deletions

View file

@ -41,14 +41,8 @@ frappe.ui.form.on("Prepared Report", {
if (frm.doc.status == "Completed") {
frm.page.set_primary_action(__("Show Report"), () => {
frappe.route_options = filters;
frappe.set_route(
"query-report",
frm.doc.report_name,
frappe.utils.make_query_string({
prepared_report_name: frm.doc.name,
})
);
frappe.route_options = { prepared_report_name: frm.doc.name };
frappe.set_route("query-report", frm.doc.report_name);
});
}
},

View file

@ -68,21 +68,22 @@ frappe.views.QueryReport = class QueryReport extends frappe.views.BaseList {
}
get_url_with_filters() {
const query_params = Object.entries(this.get_filter_values())
.map(([field, value], _idx) => {
let query_params = new URLSearchParams();
if (this.prepared_report_name) {
query_params.append("prepared_report_name", this.prepared_report_name);
} else {
Object.entries(this.get_filter_values()).map(([field, value], _idx) => {
// multiselects
if (Array.isArray(value)) {
if (!value.length) return "";
value = JSON.stringify(value);
}
return `${field}=${encodeURIComponent(value)}`;
})
.filter(Boolean)
.join("&");
query_params.append(field, value);
});
}
let full_url = window.location.href.replace(window.location.search, "");
if (query_params) {
full_url += "?" + query_params;
if (query_params.toString()) {
full_url += "?" + query_params.toString();
}
return full_url;
}
@ -394,6 +395,7 @@ frappe.views.QueryReport = class QueryReport extends frappe.views.BaseList {
}
refresh_report(route_options) {
this.prepared_report_name = null; // this should be set only if prepared report is EXPLICITLY requested
this.toggle_message(true);
this.toggle_report(false);
@ -596,6 +598,7 @@ frappe.views.QueryReport = class QueryReport extends frappe.views.BaseList {
const fields = Object.keys(route_options);
const filters_to_set = this.filters.filter((f) => fields.includes(f.df.fieldname));
this.prepared_report_name = route_options.prepared_report_name;
const promises = filters_to_set.map((f) => {
return () => {
@ -647,10 +650,8 @@ frappe.views.QueryReport = class QueryReport extends frappe.views.BaseList {
this.last_ajax.abort();
}
const query_params = this.get_query_params();
if (query_params.prepared_report_name) {
filters.prepared_report_name = query_params.prepared_report_name;
if (this.prepared_report_name) {
filters.prepared_report_name = this.prepared_report_name;
}
return new Promise((resolve) => {
@ -686,7 +687,7 @@ frappe.views.QueryReport = class QueryReport extends frappe.views.BaseList {
this.prepared_report_document = data.doc;
// If query_string contains prepared_report_name then set filters
// to match the mentioned prepared report doc and disable editing
if (query_params.prepared_report_name) {
if (this.prepared_report_name) {
this.prepared_report_action = "Edit";
const filters_from_report = JSON.parse(data.doc.filters);
Object.values(this.filters).forEach(function (field) {
@ -1531,12 +1532,8 @@ frappe.views.QueryReport = class QueryReport extends frappe.views.BaseList {
: value,
])
);
let query_params = this.get_query_params();
if ("prepared_report_name" in query_params) {
filters = Object.assign(
{ prepared_report_name: query_params["prepared_report_name"] },
filters
);
if (this.prepared_report_name) {
filters.prepared_report_name = this.prepared_report_name;
}
const visible_idx = this.datatable?.bodyRenderer.visibleRowIndices || [];