Skip to content

Commit

Permalink
fix: allow user to choose the name on copy
Browse files Browse the repository at this point in the history
when we create copy when user updates the formats with old schema.
it was implicitly setting the name with copy + number suffix.
now we set that name as default and prompt user if they want to change the name.
  • Loading branch information
maharshivpatel committed Apr 3, 2024
1 parent a1e112a commit 6afc817
Showing 1 changed file with 75 additions and 31 deletions.
106 changes: 75 additions & 31 deletions print_designer/public/js/print_designer/store/ElementStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -614,56 +614,100 @@ export const useElementStore = defineStore("ElementStore", {
return wrapperContainers.childrens.map((el) => this.childrensSave(el));
},
async printFormatCopyOnOlderSchema(objectToSave) {
const MainStore = useMainStore();
let nextFormatCopyNumber = 0;
for (let i = 0; i < 100; i++) {
const pf_exists = await frappe.db.exists(
"Print Format",
MainStore.printDesignName + " ( Copy " + (i ? i : "") + " )"
);
if (pf_exists) continue;
nextFormatCopyNumber = i;
break;
}
const newName =
MainStore.printDesignName +
" ( Copy " +
(nextFormatCopyNumber ? nextFormatCopyNumber : "") +
" )";
// TODO: have better message.
let message = __(
"<b>This Print Format was created from older version of Print Designer.</b>"
);
message += "<hr />";
message += __(
"It is not compatible with current version so instead we will make copy of this format for you using new version"
"It is not compatible with current version so instead make copy of this format using new version"
);
message += "<hr />";
message += __(`Do you want to save it as <b>${newName}</b> ?`);
message += __(`Do you want to save copy of it ?`);

frappe.confirm(
message,
async () => {
await frappe.db.insert({
doctype: "Print Format",
name: newName,
doc_type: MainStore.doctype,
print_designer: 1,
print_designer_header: objectToSave.print_designer_header,
print_designer_body: objectToSave.print_designer_body,
print_designer_after_table: null,
print_designer_footer: objectToSave.print_designer_footer,
print_designer_print_format: objectToSave.print_designer_print_format,
print_designer_settings: objectToSave.print_designer_settings,
});
frappe.set_route("print-designer", newName);
this.promptUserForNewFormatName(objectToSave);
},
async () => {
frappe.show_alert(
{
message: `Print Format not saved`,
indicator: "red",
},
5
);
// intentionally throwing error to stop the saving the format
throw new Error(__("Print Format not saved"));
}
);
},
async promptUserForNewFormatName(objectToSave) {
const MainStore = useMainStore();
let nextFormatCopyNumber = 0;
for (let i = 0; i < 100; i++) {
const pf_exists = await frappe.db.exists(
"Print Format",
MainStore.printDesignName + " ( Copy " + (i ? i : "") + " )"
);
if (pf_exists) continue;
nextFormatCopyNumber = i;
break;
}
// This is just default value for the new print format name
const print_format_name =
MainStore.printDesignName +
" ( Copy " +
(nextFormatCopyNumber ? nextFormatCopyNumber : "") +
" )";

let d = new frappe.ui.Dialog({
title: "New Print Format",
fields: [
{
label: "Name",
fieldname: "print_format_name",
fieldtype: "Data",
reqd: 1,
default: print_format_name,
},
],
size: "small",
primary_action_label: "Save",
static: true,
async primary_action(values) {
try {
await frappe.db.insert({
doctype: "Print Format",
name: values.print_format_name,
doc_type: MainStore.doctype,
print_designer: 1,
print_designer_header: objectToSave.print_designer_header,
print_designer_body: objectToSave.print_designer_body,
print_designer_after_table: null,
print_designer_footer: objectToSave.print_designer_footer,
print_designer_print_format: objectToSave.print_designer_print_format,
print_designer_settings: objectToSave.print_designer_settings,
});
d.hide();
frappe.set_route("print-designer", values.print_format_name);
} catch (error) {
console.error(error);
}
},
});
d.get_close_btn().on("click", () => {
frappe.show_alert(
{
message: `Print Format not saved`,
indicator: "red",
},
5
);
});
d.show();
},
handleDynamicContent(element) {
const MainStore = useMainStore();
if (
Expand Down

0 comments on commit 6afc817

Please sign in to comment.