File size: 3,816 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import { Gridicon } from '@automattic/components';
import { getPaymentMethodImageURL, isCreditCard } from '@automattic/wpcom-checkout';
import { Button } from '@wordpress/components';
import { useTranslate, TranslateResult } from 'i18n-calypso';
import { ConfirmDialog, DialogContent, DialogFooter } from 'calypso/components/confirm-dialog';
import { useLocalizedMoment } from 'calypso/components/localized-moment';
import type { StoredPaymentMethod } from '@automattic/wpcom-checkout';
import type { Purchase } from 'calypso/lib/purchases/types';
import 'calypso/me/purchases/payment-methods/style.scss';

interface Props {
	card: StoredPaymentMethod;
	paymentMethodSummary: TranslateResult;
	purchases: Purchase[];
	isVisible: boolean;
	onClose: () => void;
	onConfirm: () => void;
}

const PaymentMethodDeleteDialog = ( {
	card,
	paymentMethodSummary,
	purchases,
	onClose,
	isVisible,
	onConfirm,
}: Props ) => {
	const moment = useLocalizedMoment();
	const translate = useTranslate();
	const associatedSubscriptions = purchases?.filter(
		( purchase: Purchase ) =>
			purchase.payment?.storedDetailsId === card.stored_details_id && purchase.isAutoRenewEnabled
	);

	const handleClose = () => {
		onClose();
	};

	if ( ! isVisible ) {
		return null;
	}

	return (
		<ConfirmDialog
			onRequestClose={ handleClose }
			className="payment-method-delete-dialog"
			title={ translate( 'Remove payment method' ) }
		>
			<DialogContent className="payment-method-delete-dialog__content">
				<div className="payment-method-delete-dialog__explanation">
					{ isCreditCard( card ) && (
						<img src={ getPaymentMethodImageURL( card?.card_type ) } alt="" />
					) }
					<p>
						{ translate(
							'The payment method {{paymentMethodSummary/}} will be removed from your account and from all the associated subscriptions.',
							{
								components: {
									paymentMethodSummary: <strong>{ paymentMethodSummary }</strong>,
								},
							}
						) }
					</p>
				</div>

				{ associatedSubscriptions.length > 0 && (
					<div className="payment-method-delete-dialog__affected-subscriptions-wrapper">
						<table className="payment-method-delete-dialog__affected-subscriptions-table">
							<thead>
								<tr>
									<th>{ translate( 'Subscription' ) }</th>
									<th>{ translate( 'Renew date' ) }</th>
								</tr>
							</thead>
							<tbody>
								{ associatedSubscriptions.map( ( purchase: Purchase ) => (
									<tr key={ purchase.id }>
										<td className="payment-method-delete-dialog__affected-subscription-details-product">
											<strong>{ purchase.productName }</strong>
											<span className="payment-method-delete-dialog__affected-subscription-domain">
												{ purchase.meta || purchase.domain }
											</span>
										</td>
										<td className="payment-method-delete-dialog__affected-subscription-details-renew-date fixed">
											{ moment( purchase.renewDate ).format( 'll' ) }
										</td>
									</tr>
								) ) }
							</tbody>
						</table>
						<div className="payment-method-delete-dialog__warning">
							<Gridicon icon="notice-outline" size={ 24 } />
							<p>
								{ translate(
									'This subscription will no longer auto-renew until an alternative payment method is added.',
									'These subscriptions will no longer auto-renew until an alternative payment method is added.',
									{
										count: associatedSubscriptions.length,
									}
								) }
							</p>
						</div>
					</div>
				) }
			</DialogContent>
			<DialogFooter>
				<Button onClick={ onClose }>{ translate( 'Cancel' ) }</Button>
				<Button variant="primary" isDestructive onClick={ onConfirm }>
					{ translate( 'Remove' ) }
				</Button>
			</DialogFooter>
		</ConfirmDialog>
	);
};

export default PaymentMethodDeleteDialog;