give
/
src
/
Subscriptions
/
resources
/
admin
/
components
/
SubscriptionDetailsPage
/
Tabs
/
Overview
/
SubscriptionSummary
/
index.tsx
give
/
src
/
Subscriptions
/
resources
/
admin
/
components
/
SubscriptionDetailsPage
/
Tabs
/
Overview
/
SubscriptionSummary
Last commit date
index.tsx
8 months ago
styles.module.scss
10 months ago
index.tsx
128 lines
| 1 | import {Header, OverviewPanel, SummaryItem, SummaryTable} from '@givewp/admin/components'; |
| 2 | import {amountFormatter, formatTimestamp} from '@givewp/admin/common'; |
| 3 | import {Subscription} from '@givewp/subscriptions/admin/components/types'; |
| 4 | import {dateI18n} from '@wordpress/date'; |
| 5 | import {__} from '@wordpress/i18n'; |
| 6 | import styles from './styles.module.scss'; |
| 7 | import {getSubscriptionEmbeds} from '@givewp/subscriptions/common'; |
| 8 | |
| 9 | |
| 10 | /** |
| 11 | * Calculates the end date of a subscription based on its billing parameters. |
| 12 | * |
| 13 | * The calculation uses the formula: End Date = Start Date + (Installments - 1) × Frequency × Period |
| 14 | * |
| 15 | * For example: |
| 16 | * - A monthly subscription with 12 installments: Start Date + 11 months |
| 17 | * - A weekly subscription with 4 installments: Start Date + 3 weeks |
| 18 | * - A yearly subscription with 5 installments: Start Date + 4 years |
| 19 | * |
| 20 | * @param subscription - The subscription object containing billing parameters |
| 21 | * @returns ISO string of the calculated end date, or null if calculation is not possible |
| 22 | * |
| 23 | * @since 4.8.0 |
| 24 | */ |
| 25 | const calculateEndDate = (subscription: Subscription): string | null => { |
| 26 | if (!subscription) { |
| 27 | return null; |
| 28 | } |
| 29 | |
| 30 | // If installments is 0, the subscription is ongoing (no end date) |
| 31 | if (subscription.installments === 0) { |
| 32 | return null; |
| 33 | } |
| 34 | |
| 35 | const startDate = new Date(subscription.createdAt); |
| 36 | const period = subscription.period; // day, week, month, quarter, year |
| 37 | const frequency = subscription.frequency; // how many periods between each payment |
| 38 | const installments = subscription.installments; // total number of payments |
| 39 | |
| 40 | // Calculate total periods to add: (installments - 1) × frequency |
| 41 | // We subtract 1 from installments because the first payment is on the start date |
| 42 | const totalPeriods = (installments - 1) * frequency; |
| 43 | |
| 44 | const endDate = new Date(startDate); |
| 45 | |
| 46 | // Add the calculated periods based on the billing period type |
| 47 | switch (period) { |
| 48 | case 'day': |
| 49 | endDate.setDate(endDate.getDate() + totalPeriods); |
| 50 | break; |
| 51 | case 'week': |
| 52 | endDate.setDate(endDate.getDate() + totalPeriods * 7); |
| 53 | break; |
| 54 | case 'month': |
| 55 | endDate.setMonth(endDate.getMonth() + totalPeriods); |
| 56 | break; |
| 57 | case 'quarter': |
| 58 | endDate.setMonth(endDate.getMonth() + totalPeriods * 3); |
| 59 | break; |
| 60 | case 'year': |
| 61 | endDate.setFullYear(endDate.getFullYear() + totalPeriods); |
| 62 | break; |
| 63 | default: |
| 64 | return null; |
| 65 | } |
| 66 | |
| 67 | return endDate.toISOString(); |
| 68 | }; |
| 69 | |
| 70 | /** |
| 71 | * @since 4.8.0 |
| 72 | */ |
| 73 | interface SummaryProps { |
| 74 | subscription: Subscription; |
| 75 | intendedAmount: number; |
| 76 | adminUrl: string; |
| 77 | isLoading: boolean; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * @since 4.11.0 fix form link |
| 82 | * @since 4.10.0 removed donation from props |
| 83 | * @since 4.8.0 |
| 84 | */ |
| 85 | export default function Summary({subscription, adminUrl, intendedAmount, isLoading}: SummaryProps) { |
| 86 | const {form} = getSubscriptionEmbeds(subscription); |
| 87 | const formTitle = form?.title ?? __('Donation Form', 'give'); |
| 88 | const endDate = calculateEndDate(subscription); |
| 89 | |
| 90 | const summaryItems: SummaryItem[] = [ |
| 91 | { |
| 92 | label: __('Start date', 'give'), |
| 93 | value: formatTimestamp(subscription?.createdAt, false), |
| 94 | }, |
| 95 | { |
| 96 | label: __('End date', 'give'), |
| 97 | value: endDate ? formatTimestamp(endDate, false) : __('Ongoing', 'give'), |
| 98 | }, |
| 99 | { |
| 100 | label: __('Donation form', 'give'), |
| 101 | value: subscription?.donationFormId ? ( |
| 102 | <a |
| 103 | className={styles.link} |
| 104 | href={`${adminUrl}post.php?post=${subscription?.donationFormId}&action=edit`} |
| 105 | target="_blank" |
| 106 | rel="noopener noreferrer" |
| 107 | > |
| 108 | {formTitle} |
| 109 | </a> |
| 110 | ) : '', |
| 111 | }, |
| 112 | { |
| 113 | label: __('Renewal', 'give'), |
| 114 | value: amountFormatter(subscription?.amount?.currency).format(intendedAmount), |
| 115 | }, |
| 116 | ]; |
| 117 | |
| 118 | return ( |
| 119 | <OverviewPanel className={styles.summaryPanel}> |
| 120 | <Header |
| 121 | title={__('Summary', 'give')} |
| 122 | subtitle={__('Information about the initial recurring donation', 'give')} |
| 123 | /> |
| 124 | <SummaryTable data={summaryItems} isLoading={isLoading} /> |
| 125 | </OverviewPanel> |
| 126 | ); |
| 127 | } |
| 128 |