From c29c29bf82237b81810a92964940d09e5a8f4af4 Mon Sep 17 00:00:00 2001 From: 18alantom <2.alan.tom@gmail.com> Date: Thu, 28 Jul 2022 14:29:04 +0530 Subject: [PATCH] fix(ux): allow delete when saved --- fyo/model/doc.ts | 26 +++++++++++++++++++++++++- src/utils/ui.ts | 6 ++---- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/fyo/model/doc.ts b/fyo/model/doc.ts index a29530fa..c3d57060 100644 --- a/fyo/model/doc.ts +++ b/fyo/model/doc.ts @@ -126,6 +126,30 @@ export class Doc extends Observable { return this._syncing; } + get canDelete() { + if (this.notInserted) { + return false; + } + + if (this.schema.isSingle) { + return false; + } + + if (!this.schema.isSubmittable) { + return true; + } + + if (this.schema.isSubmittable && this.isCancelled) { + return true; + } + + if (this.schema.isSubmittable && !this.isSubmitted) { + return true; + } + + return false; + } + _setValuesWithoutChecks(data: DocValueMap) { for (const field of this.schema.fields) { const fieldname = field.fieldname; @@ -694,7 +718,7 @@ export class Doc extends Observable { } async delete() { - if (this.schema.isSubmittable && !this.isCancelled) { + if (!this.canDelete) { return; } diff --git a/src/utils/ui.ts b/src/utils/ui.ts index b3a832e8..5e9bc5f3 100644 --- a/src/utils/ui.ts +++ b/src/utils/ui.ts @@ -150,7 +150,7 @@ export async function routeTo(route: string | RouteLocationRaw) { export async function deleteDocWithPrompt(doc: Doc) { const schemaLabel = fyo.schemaMap[doc.schemaName]!.label; let detail = t`This action is permanent.`; - if (doc.isTransactional) { + if (doc.isTransactional && doc.isSubmitted) { detail = t`This action is permanent and will delete associated ledger entries.`; } @@ -291,9 +291,7 @@ function getDeleteAction(doc: Doc): Action { component: { template: '{{ t`Delete` }}', }, - condition: (doc: Doc) => - (!doc.notInserted && !doc.schema.isSubmittable && !doc.schema.isSingle) || - doc.isCancelled, + condition: (doc: Doc) => doc.canDelete, async action() { const res = await deleteDocWithPrompt(doc); if (res) {