From 2b72c9abe5e467069267127d5814429bf08df05e Mon Sep 17 00:00:00 2001 From: bir Date: Fri, 15 Nov 2024 17:47:35 +0100 Subject: [PATCH] Check label limits, handle new unsubscribe reason close: #7943 Co-authored-by: ivk --- src/common/api/common/TutanotaConstants.ts | 1 + src/common/misc/TranslationKey.ts | 1 + .../subscription/SwitchSubscriptionDialog.ts | 3 + src/mail-app/mail/view/EditLabelDialog.ts | 92 +++++++++++-------- 4 files changed, 57 insertions(+), 40 deletions(-) diff --git a/src/common/api/common/TutanotaConstants.ts b/src/common/api/common/TutanotaConstants.ts index 59dc262a3f98..73334db6f5a6 100644 --- a/src/common/api/common/TutanotaConstants.ts +++ b/src/common/api/common/TutanotaConstants.ts @@ -685,6 +685,7 @@ export const enum UnsubscribeFailureReason { INVOICE_NOT_PAID = "unsubscribe.invoice_not_paid", HAS_CONTACT_LIST_GROUP = "unsubscribe.has_contact_list_group", ACTIVE_APPSTORE_SUBSCRIPTION = "unsubscribe.active_appstore_subscription", + LABEL_LIMIT_EXCEEDED = "unsubscribe.label_limit_exceeded", } // legacy, should be deleted after clients older than 3.114 have been disabled. diff --git a/src/common/misc/TranslationKey.ts b/src/common/misc/TranslationKey.ts index 8230f8537837..6e288c8bba02 100644 --- a/src/common/misc/TranslationKey.ts +++ b/src/common/misc/TranslationKey.ts @@ -1802,3 +1802,4 @@ export type TranslationKeyType = | "emptyString_msg" | "editLabel_action" | "importantLabel_label" + | "labelLimitExceeded_msg" diff --git a/src/common/subscription/SwitchSubscriptionDialog.ts b/src/common/subscription/SwitchSubscriptionDialog.ts index e46600b64ade..59373aac6d08 100644 --- a/src/common/subscription/SwitchSubscriptionDialog.ts +++ b/src/common/subscription/SwitchSubscriptionDialog.ts @@ -288,6 +288,8 @@ function handleSwitchAccountPreconditionFailed(e: PreconditionFailedError): Prom return showManageThroughAppStoreDialog() } + case UnsubscribeFailureReason.LABEL_LIMIT_EXCEEDED: + return Dialog.message("labelLimitExceeded_msg") default: throw e } @@ -377,6 +379,7 @@ async function switchSubscription(targetSubscription: PlanType, dialog: Dialog, } catch (e) { if (e instanceof PreconditionFailedError) { await handleSwitchAccountPreconditionFailed(e) + return currentPlanInfo.planType } throw e diff --git a/src/mail-app/mail/view/EditLabelDialog.ts b/src/mail-app/mail/view/EditLabelDialog.ts index 939dcb7d8150..f27222dfeaf0 100644 --- a/src/mail-app/mail/view/EditLabelDialog.ts +++ b/src/mail-app/mail/view/EditLabelDialog.ts @@ -3,50 +3,62 @@ import { TextField, TextFieldAttrs } from "../../../common/gui/base/TextField" import m from "mithril" 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 { LockedError, PreconditionFailedError } from "../../../common/api/common/error/RestError" import { MailViewModel } from "./MailViewModel" import { lang } from "../../../common/misc/LanguageViewModel" import { ColorPickerView } from "../../../common/gui/base/colorPicker/ColorPickerView" +import { showNotAvailableForFreeDialog } from "../../../common/misc/SubscriptionDialogs" -export async function showEditLabelDialog(mailbox: MailBox | null, mailViewModel: MailViewModel, label: MailFolder | null): Promise { - return new Promise((resolve) => { - let name = label ? label.name : "" - let color = label && label.color ? label.color : "" - Dialog.showActionDialog({ - title: lang.get(label ? "editLabel_action" : "addLabel_action"), - allowCancel: true, - 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 - } +const LIMIT_EXCEEDED_ERROR = "limitReached" + +export async function showEditLabelDialog(mailbox: MailBox | null, mailViewModel: MailViewModel, label: MailFolder | null) { + let name = label ? label.name : "" + let color = label && label.color ? label.color : "" + + async function onOkClicked(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 (error instanceof PreconditionFailedError) { + if (error.data === LIMIT_EXCEEDED_ERROR) { + showNotAvailableForFreeDialog() + } else { + Dialog.message("unknownError_msg") } - }, - child: () => - m(".flex.col.gap-vpad", [ - m(TextField, { - label: "name_label", - value: name, - oninput: (newName) => { - name = newName - }, - } satisfies TextFieldAttrs), - m(ColorPickerView, { - value: color, - onselect: (newColor: string) => { - color = newColor - }, - }), - ]), - }) + } else if (isOfflineError(error) || !(error instanceof LockedError)) { + throw error + } + } + } + + Dialog.showActionDialog({ + title: lang.get(label ? "editLabel_action" : "addLabel_action"), + allowCancel: true, + okAction: (dialog: Dialog) => { + onOkClicked(dialog) + }, + child: () => + m(".flex.col.gap-vpad", [ + m(TextField, { + label: "name_label", + value: name, + oninput: (newName) => { + name = newName + }, + } satisfies TextFieldAttrs), + m(ColorPickerView, { + value: color, + onselect: (newColor: string) => { + color = newColor + }, + }), + ]), }) }