Skip to content

Commit

Permalink
Allow for editing of labels
Browse files Browse the repository at this point in the history
Close #7755
  • Loading branch information
wrdhub committed Oct 29, 2024
1 parent 61261aa commit 132de38
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 19 deletions.
14 changes: 14 additions & 0 deletions src/common/api/worker/facades/lazy/MailFacade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions src/common/misc/TranslationKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1792,3 +1792,4 @@ export type TranslationKeyType =
| "callNumber_label"
| "addLabel_action"
| "labels_label"
| "editLabel_action"
4 changes: 4 additions & 0 deletions src/mail-app/mail/model/MailModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<MailFolder | null> {
const folderStructures = await this.loadMailSets()
for (const folders of folderStructures.values()) {
Expand Down
31 changes: 23 additions & 8 deletions src/mail-app/mail/view/EditLabelDialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
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, {
Expand Down
18 changes: 11 additions & 7 deletions src/mail-app/mail/view/MailView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -787,11 +787,12 @@ export class MailView extends BaseTopLevelView implements TopLevelView<MailViewA
await showEditFolderDialog(mailboxDetail, folder, parentFolder)
}

private async onAddLabelClicked(mailbox: MailBox) {
const labelData = await showEditLabelDialog()
if (labelData != null) {
await this.mailViewModel.createLabel(mailbox, labelData)
}
private async showLabelAddDialog(mailbox: MailBox) {
await showEditLabelDialog(mailbox, this.mailViewModel, null)
}

private async showLabelEditDialog(label: MailFolder) {
await showEditLabelDialog(null, this.mailViewModel, label)
}

private renderMailboxLabelItems(mailboxDetail: MailboxDetail, inEditMode: boolean, onEditMailbox: () => void): Children {
Expand Down Expand Up @@ -823,6 +824,9 @@ export class MailView extends BaseTopLevelView implements TopLevelView<MailViewA
{
label: "edit_action",
icon: Icons.Edit,
click: () => {
this.showLabelEditDialog(label)
},
},
{ label: "delete_action", icon: Icons.Trash },
],
Expand All @@ -840,7 +844,7 @@ export class MailView extends BaseTopLevelView implements TopLevelView<MailViewA
width: `calc(100% - ${px(size.hpad_button * 2)})`,
},
onclick: () => {
this.onAddLabelClicked(mailboxDetail.mailbox)
this.showLabelAddDialog(mailboxDetail.mailbox)
},
}),
]
Expand All @@ -860,7 +864,7 @@ export class MailView extends BaseTopLevelView implements TopLevelView<MailViewA
title: "addLabel_action",
icon: Icons.Add,
click: () => {
this.onAddLabelClicked(mailboxDetail.mailbox)
this.showLabelAddDialog(mailboxDetail.mailbox)
},
})
}
Expand Down
13 changes: 12 additions & 1 deletion src/mail-app/mail/view/MailViewModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,14 @@ export class MailViewModel {
const mailId = this.loadingTargetId ?? (folderId ? this.getMailFolderToSelectedMail().get(folderId) : null)
const stickyMail = this.stickyMailId
if (mailId != null) {
this.router.routeTo("/mail/:folderId/:mailId", this.addStickyMailParam({ folderId, mailId, mail: stickyMail }))
this.router.routeTo(
"/mail/:folderId/:mailId",
this.addStickyMailParam({
folderId,
mailId,
mail: stickyMail,
}),
)
} else {
this.router.routeTo("/mail/:folderId", this.addStickyMailParam({ folderId: folderId ?? "" }))
}
Expand Down Expand Up @@ -774,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)
}
}
3 changes: 2 additions & 1 deletion src/mail-app/translations/de.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1811,6 +1811,7 @@ export default {
"sendMail_label": "Eine E-Mail senden",
"callNumber_label": "Rufnummer",
"addLabel_action": "Add label",
"labels_label": "Labels"
"labels_label": "Labels",
"editLabel_action": "Label bearbeiten"
}
}
3 changes: 2 additions & 1 deletion src/mail-app/translations/de_sie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1811,6 +1811,7 @@ export default {
"sendMail_label": "Eine E-Mail senden",
"callNumber_label": "Rufnummer",
"addLabel_action": "Add label",
"labels_label": "Labels"
"labels_label": "Labels",
"editLabel_action": "Label bearbeiten"
}
}
3 changes: 2 additions & 1 deletion src/mail-app/translations/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1807,6 +1807,7 @@ export default {
"sendMail_label": "Send an email",
"callNumber_label": "Call number",
"addLabel_action": "Add label",
"labels_label": "Labels"
"labels_label": "Labels",
"editLabel_action": "Edit label"
}
}

0 comments on commit 132de38

Please sign in to comment.