Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Integrated handling

Manav Rathi7385d665d27f
diff --git a/web/apps/photos/src/components/pages/gallery/PlanSelector.tsx b/web/apps/photos/src/components/pages/gallery/PlanSelector.tsx
index f5ccb82d80..329499f49c 100644
--- a/web/apps/photos/src/components/pages/gallery/PlanSelector.tsx
+++ b/web/apps/photos/src/components/pages/gallery/PlanSelector.tsx
@@ -5,6 +5,7 @@ import {
import type { ModalVisibilityProps } from "@/base/components/utils/modal";
import log from "@/base/log";
import { useUserDetailsSnapshot } from "@/new/photos/components/utils/use-snapshot";
+import { useWrapAsyncOperation } from "@/new/photos/components/utils/use-wrap-async";
import type {
Bonus,
Plan,
@@ -56,7 +57,7 @@ import {
import Box from "@mui/material/Box";
import Typography from "@mui/material/Typography";
import { t } from "i18next";
-import { useCallback, useContext, useEffect, useState } from "react";
+import React, { useCallback, useContext, useEffect, useState } from "react";
import { Trans } from "react-i18next";
import { SetLoading } from "types/gallery";
@@ -709,10 +710,9 @@ function ManageSubscription({
<Stack spacing={1}>
{isSubscriptionStripe(subscription) && (
<StripeSubscriptionOptions
+ onClose={closeModal}
subscription={subscription}
hasAddOnBonus={hasAddOnBonus}
- closeModal={closeModal}
- setLoading={setLoading}
/>
)}
<ManageSubscriptionButton
@@ -725,13 +725,18 @@ function ManageSubscription({
);
}
-function StripeSubscriptionOptions({
+interface StripeSubscriptionOptionsProps {
+ onClose: () => void;
+ subscription: Subscription;
+ hasAddOnBonus: boolean;
+}
+
+const StripeSubscriptionOptions: React.FC<StripeSubscriptionOptionsProps> = ({
+ onClose,
subscription,
hasAddOnBonus,
- setLoading,
- closeModal,
-}: ManageSubscriptionProps) {
- const { showMiniDialog, onGenericError } = useAppContext();
+}) => {
+ const { showMiniDialog } = useAppContext();
const confirmReactivation = () =>
showMiniDialog({
@@ -746,23 +751,13 @@ function StripeSubscriptionOptions({
});
const reactivate = async () => {
- try {
- setLoading(true);
- await activateStripeSubscription();
- showMiniDialog({
- title: t("success"),
- message: t("SUBSCRIPTION_ACTIVATE_SUCCESS"),
- cancel: t("ok"),
- });
- } catch (e) {
- log.error("Subscription activation failed", e);
- showMiniDialog(
- errorDialogAttributes(t("SUBSCRIPTION_ACTIVATE_FAILED")),
- );
- } finally {
- closeModal();
- setLoading(false);
- }
+ await activateStripeSubscription();
+ showMiniDialog({
+ title: t("success"),
+ message: t("SUBSCRIPTION_ACTIVATE_SUCCESS"),
+ continue: { action: onClose },
+ cancel: false,
+ });
};
const confirmCancel = () =>
@@ -782,34 +777,19 @@ function StripeSubscriptionOptions({
});
const cancel = async () => {
- try {
- setLoading(true);
- await cancelStripeSubscription();
- showMiniDialog({
- title: t("success"),
- message: t("SUBSCRIPTION_CANCEL_SUCCESS"),
- cancel: t("ok"),
- });
- } catch (e) {
- log.error("Subscription cancel failed", e);
- showMiniDialog(
- errorDialogAttributes(t("SUBSCRIPTION_CANCEL_FAILED")),
- );
- } finally {
- closeModal();
- setLoading(false);
- }
+ await cancelStripeSubscription();
+ showMiniDialog({
+ title: t("success"),
+ message: t("SUBSCRIPTION_CANCEL_SUCCESS"),
+ continue: {
+ color: "secondary",
+ action: onClose,
+ },
+ cancel: false,
+ });
};
- const handleManageClick = async () => {
- try {
- setLoading(true);
- await redirectToCustomerPortal();
- } catch (e) {
- setLoading(false);
- onGenericError(e);
- }
- };
+ const handleManageClick = useWrapAsyncOperation(redirectToCustomerPortal);
return (
<>
@@ -836,7 +816,7 @@ function StripeSubscriptionOptions({
</ManageSubscriptionButton>
</>
);
-}
+};
const ManageSubscriptionButton = ({ children, ...props }: ButtonProps) => (
<Button size="large" endIcon={<ChevronRight />} {...props}>
diff --git a/web/packages/new/photos/components/utils/use-wrap-async.ts b/web/packages/new/photos/components/utils/use-wrap-async.ts
index bd77821586..a55c38280c 100644
--- a/web/packages/new/photos/components/utils/use-wrap-async.ts
+++ b/web/packages/new/photos/components/utils/use-wrap-async.ts
@@ -2,15 +2,19 @@ import { useCallback } from "react";
import { useAppContext } from "../../types/context";
/**
- * Wrap an asynchronous operation (e.g. API calls) in an global activity
- * indicator and error handler.
+ * Wrap a function returning a promise in a loading bar and error handler.
*
- * This function takes a async function, and wraps it in a function that starts
- * the global activity indicator, lets the promise resolve, and then stop the
- * activity indicator. If the promise rejects, then it shows a generic error.
+ * This function wraps asynchronous operations (e.g. API calls) in an global
+ * activity indicator and error handler. The global activity indicator and error
+ * alert triggering mechanism is obtained from the app context.
*
- * The global activity indicator and error alert triggering mechanism is
- * obtained from the app context.
+ * Specifically, this function takes an async function. It starts the global
+ * activity indicator (using {@link showLoadingBar}), performs the operation,
+ * and stops the activity indicator on completion (using
+ * {@link hideLoadingBar}).
+ *
+ * If the operation throws, it shows a generic error (using
+ * {@link onGenericError}).
*/
export const useWrapAsyncOperation = <T extends unknown[]>(
f: (...args: T) => Promise<void>,