Skip to content

Commit

Permalink
feat(templates): Start template draft in template edit mode
Browse files Browse the repository at this point in the history
  • Loading branch information
shadowbas authored and bas080 committed Nov 8, 2024
1 parent 0647dc4 commit 3f0b333
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
28 changes: 23 additions & 5 deletions src/app/compose/compose.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,15 @@ export class ComposeComponent implements AfterViewInit, OnDestroy, OnInit {
'replying',
]);
}))
.subscribe(() => this.submit(false));
.subscribe(() => {
// Disable auto-save when in template edit mode. This prevents
// creating a draft while the user might be editing the template.
// The side-effect is that a draft created from a template won't
// have autosave until it was saved as a draft.
if (this.model.tid) return;

this.submit(false)
});

this.formGroup.controls.from.valueChanges
.pipe(debounceTime(1000))
Expand Down Expand Up @@ -461,6 +469,11 @@ export class ComposeComponent implements AfterViewInit, OnDestroy, OnInit {
}

public loadDraft(msgObj) {
if (msgObj.errors) {
this.snackBar.open(msgObj.errors[0], 'Ok')
throw msgObj
}

const model = new DraftFormModel();
model.mid = typeof msgObj.mid === 'string' ? parseInt(msgObj.mid, 10) : msgObj.mid;
this.draftDeskservice.isEditing = model.mid;
Expand Down Expand Up @@ -674,7 +687,7 @@ export class ComposeComponent implements AfterViewInit, OnDestroy, OnInit {
}

public submit(send: boolean = false) {
const { isTemplate } = this;
const isTemplate = Boolean(this.isTemplate ?? this.model.tid);

if (this.savingInProgress) {
return;
Expand Down Expand Up @@ -785,7 +798,14 @@ export class ComposeComponent implements AfterViewInit, OnDestroy, OnInit {
} else {
this.rmmapi.me.pipe(mergeMap((me) => {
return this.http.post('/rest/v1/draft', {
type: isTemplate ? 'template' : 'draft',
...(isTemplate ? {
type: 'template',
mid: this.model.tid ?? this.model.mid,
tid: this.model.tid ?? this.model.mid,
} : {
type: 'draft',
mid: this.model.mid
}),
username: me.username,
from: from && from.id ? from.from_name + '%' + from.email + '%' + from.id : from ? from.email : undefined,
from_email: from ? from.email : '',
Expand All @@ -800,8 +820,6 @@ export class ComposeComponent implements AfterViewInit, OnDestroy, OnInit {
tags: [],
ctype: this.model.useHTML ? 'html' : null,
save: send ? 'Send' : 'Save',
mid: this.model.mid,
...(isTemplate ? {tid: this.model.mid} : {}),
attachments: this.model.attachments ?
this.model.attachments
.filter((att) => att.file !== 'UTF-8Q')
Expand Down
4 changes: 3 additions & 1 deletion src/app/compose/draftdesk.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export class DraftFormModel {

from: string = null;
mid: number = (DraftFormModel.newDraftCount--);
tid: number = null;
to: MailAddressInfo[] = [];
cc: MailAddressInfo[] = [];
bcc: MailAddressInfo[] = [];
Expand Down Expand Up @@ -157,7 +158,7 @@ export class DraftFormModel {
}

public static trimmedPreview(preview: string): string {
let ret = preview.substring(0, DraftFormModel.MAX_DRAFT_PREVIEW_LENGTH);
let ret = (preview ?? '').substring(0, DraftFormModel.MAX_DRAFT_PREVIEW_LENGTH);
if (ret.length === DraftFormModel.MAX_DRAFT_PREVIEW_LENGTH) {
ret += '...';
}
Expand Down Expand Up @@ -329,6 +330,7 @@ export class DraftDeskService {
subject
)

draftFormModel.tid = messageId;
draftFormModel.msg_body = contents.text.text;
draftFormModel.html = contents.text.html;

Expand Down

0 comments on commit 3f0b333

Please sign in to comment.