From 72676c9ab5f11c18ce79ca775d2a53bf419a8a4a Mon Sep 17 00:00:00 2001 From: wrd Date: Tue, 29 Oct 2024 15:21:14 +0100 Subject: [PATCH] Allow for editing of labels Close #7755 --- .../api/worker/facades/lazy/MailFacade.ts | 14 +++++++++ src/common/misc/TranslationKey.ts | 1 + src/mail-app/mail/model/MailModel.ts | 4 +++ src/mail-app/mail/view/EditLabelDialog.ts | 31 ++++++++++++++----- src/mail-app/mail/view/MailView.ts | 18 ++++++----- src/mail-app/mail/view/MailViewModel.ts | 4 +++ src/mail-app/translations/de.ts | 3 +- src/mail-app/translations/de_sie.ts | 3 +- src/mail-app/translations/en.ts | 3 +- 9 files changed, 63 insertions(+), 18 deletions(-) diff --git a/src/common/api/worker/facades/lazy/MailFacade.ts b/src/common/api/worker/facades/lazy/MailFacade.ts index e4aeded2d68..af3c0706d96 100644 --- a/src/common/api/worker/facades/lazy/MailFacade.ts +++ b/src/common/api/worker/facades/lazy/MailFacade.ts @@ -1058,6 +1058,20 @@ export class MailFacade { }, ) } + + /* + * Update a label, if needed + * @param label existing label + * @param name possible new name for label + * @param color possible new color for label + */ + async updateLabel(label: MailFolder, name: string, color: string) { + if (name !== label.name || color != label.color) { + label.name = name + label.color = color + await this.entityClient.update(label) + } + } } export function phishingMarkerValue(type: ReportedMailFieldType, value: string): string { diff --git a/src/common/misc/TranslationKey.ts b/src/common/misc/TranslationKey.ts index 2d6b57eb377..31521368454 100644 --- a/src/common/misc/TranslationKey.ts +++ b/src/common/misc/TranslationKey.ts @@ -1794,3 +1794,4 @@ export type TranslationKeyType = | "yourMessage_label" | "you_label" | "emptyString_msg" + | "editLabel_action" diff --git a/src/mail-app/mail/model/MailModel.ts b/src/mail-app/mail/model/MailModel.ts index ed5c4d41665..4f5a8f5a2bd 100644 --- a/src/mail-app/mail/model/MailModel.ts +++ b/src/mail-app/mail/model/MailModel.ts @@ -534,6 +534,10 @@ export class MailModel { await this.mailFacade.createLabel(mailGroupId, labelData) } + async updateLabel(label: MailFolder, newData: { name: string; color: string }) { + await this.mailFacade.updateLabel(label, newData.name, newData.color) + } + async getFolderById(folderElementId: Id): Promise { const folderStructures = await this.loadMailSets() for (const folders of folderStructures.values()) { diff --git a/src/mail-app/mail/view/EditLabelDialog.ts b/src/mail-app/mail/view/EditLabelDialog.ts index 6d7e718f498..da9bb137ad0 100644 --- a/src/mail-app/mail/view/EditLabelDialog.ts +++ b/src/mail-app/mail/view/EditLabelDialog.ts @@ -3,20 +3,35 @@ import { TextField, TextFieldAttrs } from "../../../common/gui/base/TextField" import { ColorPicker } from "../../../common/gui/base/ColorPicker" import m from "mithril" import { theme } from "../../../common/gui/theme" +import type { MailBox, MailFolder } from "../../../common/api/entities/tutanota/TypeRefs" +import { isOfflineError } from "../../../common/api/common/utils/ErrorUtils" +import { LockedError } from "../../../common/api/common/error/RestError" +import { MailViewModel } from "./MailViewModel" +import { lang } from "../../../common/misc/LanguageViewModel" -export function showEditLabelDialog(): Promise<{ name: string; color: string } | null> { +export async function showEditLabelDialog(mailbox: MailBox | null, mailViewModel: MailViewModel, label: MailFolder | null): Promise { return new Promise((resolve) => { - let name = "" - let color = theme.content_accent + let name = label ? label.name : "" + let color = label && label.color ? label.color : theme.content_accent Dialog.showActionDialog({ - // FIXME translate - title: () => "Create label", + title: lang.get(label ? "editLabel_action" : "addLabel_action"), allowCancel: true, - okAction: (dialog) => { - resolve({ name, color }) + okAction: async (dialog: Dialog) => { dialog.close() + try { + if (label) { + // editing a label + await mailViewModel.editLabel(label, { name, color }) + } else if (mailbox) { + // adding a label + await mailViewModel.createLabel(mailbox, { name, color }) + } + } catch (error) { + if (isOfflineError(error) || !(error instanceof LockedError)) { + throw error + } + } }, - cancelAction: () => resolve(null), child: () => m(".flex.col.gap-vpad", [ m(TextField, { diff --git a/src/mail-app/mail/view/MailView.ts b/src/mail-app/mail/view/MailView.ts index 24c95e1178b..7f8880e8edb 100644 --- a/src/mail-app/mail/view/MailView.ts +++ b/src/mail-app/mail/view/MailView.ts @@ -787,11 +787,12 @@ export class MailView extends BaseTopLevelView implements TopLevelView void): Children { @@ -823,6 +824,9 @@ export class MailView extends BaseTopLevelView implements TopLevelView { + this.showLabelEditDialog(label) + }, }, { label: "delete_action", icon: Icons.Trash }, ], @@ -840,7 +844,7 @@ export class MailView extends BaseTopLevelView implements TopLevelView { - this.onAddLabelClicked(mailboxDetail.mailbox) + this.showLabelAddDialog(mailboxDetail.mailbox) }, }), ] @@ -860,7 +864,7 @@ export class MailView extends BaseTopLevelView implements TopLevelView { - this.onAddLabelClicked(mailboxDetail.mailbox) + this.showLabelAddDialog(mailboxDetail.mailbox) }, }) } diff --git a/src/mail-app/mail/view/MailViewModel.ts b/src/mail-app/mail/view/MailViewModel.ts index bb684181f73..75c0b0084fa 100644 --- a/src/mail-app/mail/view/MailViewModel.ts +++ b/src/mail-app/mail/view/MailViewModel.ts @@ -781,4 +781,8 @@ export class MailViewModel { async createLabel(mailbox: MailBox, labelData: { name: string; color: string }) { await this.mailModel.createLabel(assertNotNull(mailbox._ownerGroup), labelData) } + + async editLabel(label: MailFolder, newData: { name: string; color: string }) { + await this.mailModel.updateLabel(label, newData) + } } diff --git a/src/mail-app/translations/de.ts b/src/mail-app/translations/de.ts index 1ae234921e2..c91945e71ac 100644 --- a/src/mail-app/translations/de.ts +++ b/src/mail-app/translations/de.ts @@ -1813,6 +1813,7 @@ export default { "yourCalendars_label": "Deine Kalender", "yourFolders_action": "DEINE ORDNER", "yourMessage_label": "Deine Nachricht", - "you_label": "Du" + "you_label": "Du", + "editLabel_action": "Label bearbeiten" } } diff --git a/src/mail-app/translations/de_sie.ts b/src/mail-app/translations/de_sie.ts index 18e566af5b6..912bd593b79 100644 --- a/src/mail-app/translations/de_sie.ts +++ b/src/mail-app/translations/de_sie.ts @@ -1813,6 +1813,7 @@ export default { "yourCalendars_label": "Deine Kalender", "yourFolders_action": "Ihre ORDNER", "yourMessage_label": "Ihre Nachricht", - "you_label": "Sie" + "you_label": "Sie", + "editLabel_action": "Label bearbeiten" } } diff --git a/src/mail-app/translations/en.ts b/src/mail-app/translations/en.ts index 8b3514d816b..19931629f21 100644 --- a/src/mail-app/translations/en.ts +++ b/src/mail-app/translations/en.ts @@ -1809,6 +1809,7 @@ export default { "yourCalendars_label": "Your calendars", "yourFolders_action": "YOUR FOLDERS", "yourMessage_label": "Your message", - "you_label": "You" + "you_label": "You", + "editLabel_action": "Edit label" } }