0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2024-12-16 20:26:19 -05:00

feat(console): add loading state for stripe actions (#4226)

This commit is contained in:
Xiao Yijun 2023-07-25 16:21:42 +08:00 committed by GitHub
parent 9eca310f6c
commit 56ca8319d8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 45 additions and 13 deletions

View file

@ -1,4 +1,4 @@
import { useContext } from 'react'; import { useContext, useState } from 'react';
import { Trans, useTranslation } from 'react-i18next'; import { Trans, useTranslation } from 'react-i18next';
import Tip from '@/assets/icons/tip.svg'; import Tip from '@/assets/icons/tip.svg';
@ -20,6 +20,7 @@ type Props = {
function BillInfo({ cost, isManagePaymentVisible }: Props) { function BillInfo({ cost, isManagePaymentVisible }: Props) {
const { currentTenantId } = useContext(TenantsContext); const { currentTenantId } = useContext(TenantsContext);
const { visitManagePaymentPage } = useSubscribe(); const { visitManagePaymentPage } = useSubscribe();
const [isLoading, setIsLoading] = useState(false);
const { t } = useTranslation(undefined, { keyPrefix: 'admin_console' }); const { t } = useTranslation(undefined, { keyPrefix: 'admin_console' });
@ -55,8 +56,11 @@ function BillInfo({ cost, isManagePaymentVisible }: Props) {
{isManagePaymentVisible && ( {isManagePaymentVisible && (
<Button <Button
title="subscription.manage_payment" title="subscription.manage_payment"
onClick={() => { isLoading={isLoading}
void visitManagePaymentPage(currentTenantId); onClick={async () => {
setIsLoading(true);
await visitManagePaymentPage(currentTenantId);
setIsLoading(false);
}} }}
/> />
)} )}

View file

@ -23,6 +23,7 @@ function PaymentOverdueModal() {
const { currentTenant, currentTenantId } = useContext(TenantsContext); const { currentTenant, currentTenantId } = useContext(TenantsContext);
const { data: invoices, error } = useInvoices(currentTenantId); const { data: invoices, error } = useInvoices(currentTenantId);
const { visitManagePaymentPage } = useSubscribe(); const { visitManagePaymentPage } = useSubscribe();
const [isActionLoading, setIsActionLoading] = useState(false);
const isLoadingInvoices = !invoices && !error; const isLoadingInvoices = !invoices && !error;
const latestUnpaidInvoice = useMemo(() => { const latestUnpaidInvoice = useMemo(() => {
@ -69,8 +70,11 @@ function PaymentOverdueModal() {
<Button <Button
type="primary" type="primary"
title="upsell.payment_overdue_modal.update_payment" title="upsell.payment_overdue_modal.update_payment"
onClick={() => { isLoading={isActionLoading}
void visitManagePaymentPage(currentTenantId); onClick={async () => {
setIsActionLoading(true);
await visitManagePaymentPage(currentTenantId);
setIsActionLoading(false);
}} }}
/> />
</> </>

View file

@ -2,7 +2,6 @@ import type { AdminConsoleKey } from '@logto/phrases';
import classNames from 'classnames'; import classNames from 'classnames';
import type { HTMLProps, ReactElement, ReactNode } from 'react'; import type { HTMLProps, ReactElement, ReactNode } from 'react';
import { useEffect, useRef, useState } from 'react'; import { useEffect, useRef, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { Ring as Spinner } from '@/ds-components/Spinner'; import { Ring as Spinner } from '@/ds-components/Spinner';
@ -54,9 +53,9 @@ function Button({
onClick, onClick,
trailingIcon, trailingIcon,
to, to,
disabled,
...rest ...rest
}: Props) { }: Props) {
const { t } = useTranslation(undefined, { keyPrefix: 'admin_console' });
const [showSpinner, setShowSpinner] = useState(false); const [showSpinner, setShowSpinner] = useState(false);
const timerRef = useRef<number>(); const timerRef = useRef<number>();
@ -87,6 +86,7 @@ function Button({
)} )}
type={htmlType} type={htmlType}
title={to} title={to}
disabled={isLoading || disabled}
onClick={(event) => { onClick={(event) => {
if (isLoading) { if (isLoading) {
return false; return false;

View file

@ -20,6 +20,7 @@ type Props = {
onClick?: () => void; onClick?: () => void;
variant?: 'plain' | 'shadow'; variant?: 'plain' | 'shadow';
hasIcon?: boolean; hasIcon?: boolean;
isActionLoading?: boolean;
className?: string; className?: string;
}; };
@ -31,6 +32,7 @@ function InlineNotification({
severity = 'info', severity = 'info',
variant = 'plain', variant = 'plain',
hasIcon = true, hasIcon = true,
isActionLoading = false,
className, className,
}: Props) { }: Props) {
return ( return (
@ -57,7 +59,13 @@ function InlineNotification({
)} )}
{action && onClick && ( {action && onClick && (
<div className={styles.action}> <div className={styles.action}>
<Button title={action} type="text" size="small" onClick={onClick} /> <Button
title={action}
type="text"
size="small"
isLoading={isActionLoading}
onClick={onClick}
/>
</div> </div>
)} )}
</div> </div>

View file

@ -1,4 +1,4 @@
import { useContext, useMemo } from 'react'; import { useContext, useMemo, useState } from 'react';
import { toastResponseError } from '@/cloud/hooks/use-cloud-api'; import { toastResponseError } from '@/cloud/hooks/use-cloud-api';
import { subscriptionPage } from '@/consts/pages'; import { subscriptionPage } from '@/consts/pages';
@ -24,6 +24,7 @@ function MauLimitExceededNotification({ activeUsers, currentPlan, className }: P
const { subscribe } = useSubscribe(); const { subscribe } = useSubscribe();
const { show } = useConfirmModal(); const { show } = useConfirmModal();
const { data: subscriptionPlans } = useSubscriptionPlans(); const { data: subscriptionPlans } = useSubscriptionPlans();
const [isLoading, setIsLoading] = useState(false);
const proPlan = useMemo( const proPlan = useMemo(
() => subscriptionPlans?.find(({ id }) => id === ReservedPlanId.pro), () => subscriptionPlans?.find(({ id }) => id === ReservedPlanId.pro),
[subscriptionPlans] [subscriptionPlans]
@ -46,14 +47,19 @@ function MauLimitExceededNotification({ activeUsers, currentPlan, className }: P
severity="error" severity="error"
action="subscription.upgrade_pro" action="subscription.upgrade_pro"
className={className} className={className}
isActionLoading={isLoading}
onClick={async () => { onClick={async () => {
try { try {
setIsLoading(true);
await subscribe({ await subscribe({
planId: proPlan.id, planId: proPlan.id,
tenantId: currentTenantId, tenantId: currentTenantId,
callbackPage: subscriptionPage, callbackPage: subscriptionPage,
}); });
setIsLoading(false);
} catch (error: unknown) { } catch (error: unknown) {
setIsLoading(false);
if (await isExceededQuotaLimitError(error)) { if (await isExceededQuotaLimitError(error)) {
await show({ await show({
ModalContent: () => <NotEligibleSwitchPlanModalContent targetPlan={proPlan} />, ModalContent: () => <NotEligibleSwitchPlanModalContent targetPlan={proPlan} />,

View file

@ -1,5 +1,5 @@
import { conditional } from '@silverhand/essentials'; import { conditional } from '@silverhand/essentials';
import { useContext, useMemo } from 'react'; import { useContext, useMemo, useState } from 'react';
import { TenantsContext } from '@/contexts/TenantsProvider'; import { TenantsContext } from '@/contexts/TenantsProvider';
import DynamicT from '@/ds-components/DynamicT'; import DynamicT from '@/ds-components/DynamicT';
@ -15,6 +15,7 @@ type Props = {
function PaymentOverdueNotification({ className }: Props) { function PaymentOverdueNotification({ className }: Props) {
const { currentTenantId } = useContext(TenantsContext); const { currentTenantId } = useContext(TenantsContext);
const { visitManagePaymentPage } = useSubscribe(); const { visitManagePaymentPage } = useSubscribe();
const [isActionLoading, setIsActionLoading] = useState(false);
const { data: invoices, error } = useInvoices(currentTenantId); const { data: invoices, error } = useInvoices(currentTenantId);
const isLoadingInvoices = !invoices && !error; const isLoadingInvoices = !invoices && !error;
const latestUnpaidInvoice = useMemo( const latestUnpaidInvoice = useMemo(
@ -31,8 +32,11 @@ function PaymentOverdueNotification({ className }: Props) {
severity="error" severity="error"
action="subscription.update_payment" action="subscription.update_payment"
className={className} className={className}
onClick={() => { isActionLoading={isActionLoading}
void visitManagePaymentPage(currentTenantId); onClick={async () => {
setIsActionLoading(true);
await visitManagePaymentPage(currentTenantId);
setIsActionLoading(false);
}} }}
> >
<DynamicT <DynamicT

View file

@ -1,4 +1,4 @@
import { useContext } from 'react'; import { useContext, useState } from 'react';
import { toast } from 'react-hot-toast'; import { toast } from 'react-hot-toast';
import { Trans, useTranslation } from 'react-i18next'; import { Trans, useTranslation } from 'react-i18next';
@ -35,6 +35,7 @@ function SwitchPlanActionBar({
const { currentTenantId } = useContext(TenantsContext); const { currentTenantId } = useContext(TenantsContext);
const { subscribe, cancelSubscription } = useSubscribe(); const { subscribe, cancelSubscription } = useSubscribe();
const { show } = useConfirmModal(); const { show } = useConfirmModal();
const [isLoading, setIsLoading] = useState(false);
const handleSubscribe = async (targetPlanId: string, isDowngrade: boolean) => { const handleSubscribe = async (targetPlanId: string, isDowngrade: boolean) => {
const currentPlan = subscriptionPlans.find(({ id }) => id === currentSubscriptionPlanId); const currentPlan = subscriptionPlans.find(({ id }) => id === currentSubscriptionPlanId);
@ -59,6 +60,7 @@ function SwitchPlanActionBar({
} }
try { try {
setIsLoading(true);
if (targetPlanId === ReservedPlanId.free) { if (targetPlanId === ReservedPlanId.free) {
await cancelSubscription(currentTenantId); await cancelSubscription(currentTenantId);
onSubscriptionUpdated(); onSubscriptionUpdated();
@ -76,7 +78,10 @@ function SwitchPlanActionBar({
isDowngrade, isDowngrade,
callbackPage: subscriptionPage, callbackPage: subscriptionPage,
}); });
setIsLoading(false);
} catch (error: unknown) { } catch (error: unknown) {
setIsLoading(false);
if (await isExceededQuotaLimitError(error)) { if (await isExceededQuotaLimitError(error)) {
await show({ await show({
ModalContent: () => ( ModalContent: () => (
@ -115,6 +120,7 @@ function SwitchPlanActionBar({
} }
type={isDowngrade ? 'default' : 'primary'} type={isDowngrade ? 'default' : 'primary'}
disabled={isCurrentPlan} disabled={isCurrentPlan}
isLoading={!isCurrentPlan && isLoading}
onClick={() => { onClick={() => {
void handleSubscribe(planId, isDowngrade); void handleSubscribe(planId, isDowngrade);
}} }}