communication: save email as drafts to localStorage (#6251)

currently, there is no way to store an email draft. so suppose if someone would like to _not_ lose their email progress, we should store a draft as a safety measure.

a cheap way of doing this is by using localStorage. in this, we use <doctype>-<docname> key format in localStorage with the draft mail as the value. the draft is also auto deleted on mail send.

Signed-off-by: Chinmay Pai <chinmaydpai@gmail.com>

Co-authored-by: Faris Ansari <netchamp.faris@gmail.com>
Co-authored-by: Kenneth Sequeira <kenneth@frappe.io>
This commit is contained in:
Chinmay Pai 2018-10-18 07:07:06 +00:00 committed by Rushabh Mehta
parent 80b3710f3a
commit b1aee1477a

View file

@ -78,8 +78,14 @@ frappe.views.CommunicationComposer = Class.extend({
{label:__("Subject"), fieldtype:"Data", reqd: 1,
fieldname:"subject", length:524288},
{fieldtype: "Section Break"},
{label:__("Message"), fieldtype:"Text Editor", reqd: 1,
fieldname:"content"},
{
label:__("Message"),
fieldtype:"Text Editor", reqd: 1,
fieldname:"content",
onchange: frappe.utils.debounce(this.save_as_draft.bind(this), 300),
default: localStorage.getItem(this.frm.doctype + this.frm.docname) || ''
},
{fieldtype: "Section Break"},
{fieldtype: "Column Break"},
{label:__("Send me a copy"), fieldtype:"Check",
@ -113,7 +119,7 @@ frappe.views.CommunicationComposer = Class.extend({
},
prepare: function() {
this.setup_subject_and_recipients();
this.setup_print_language()
this.setup_print_language();
this.setup_print();
this.setup_attach();
this.setup_email();
@ -128,7 +134,10 @@ frappe.views.CommunicationComposer = Class.extend({
this.dialog.fields_dict.sender.set_value(this.sender || '');
}
this.dialog.fields_dict.subject.set_value(this.subject || '');
this.setup_earlier_reply();
if(!localStorage.getItem(this.frm.doctype + this.frm.docname)) {
this.setup_earlier_reply();
}
},
setup_subject_and_recipients: function() {
@ -488,6 +497,18 @@ frappe.views.CommunicationComposer = Class.extend({
return form_values;
},
save_as_draft: function() {
if (this.dialog) {
try {
localStorage.setItem(this.frm.doctype + this.frm.docname, this.dialog.get_value('content'));
} catch (e) {
// silently fail
console.log(e);
console.warn('[Communication] localStorage is full. Cannot save message as draft');
}
}
},
send_email: function(btn, form_values, selected_attachments, print_html, print_format) {
var me = this;
me.dialog.hide();
@ -549,6 +570,16 @@ frappe.views.CommunicationComposer = Class.extend({
cur_frm.reload_doc();
}
if (localStorage.getItem(this.frm.doctype + this.frm.docname)) {
try {
localStorage.removeItem(this.frm.doctype + this.frm.docname);
} catch (e) {
// silently fail
console.log(e);
console.warn('[Communication] Failed to delete draft.');
}
}
// try the success callback if it exists
if (me.success) {
try {