give
/
src
/
Subscriptions
/
resources
/
admin
/
components
/
SubscriptionDetailsPage
/
Tabs
/
Overview
/
SubscriptionAnnualProjection
/
utils.ts
give
/
src
/
Subscriptions
/
resources
/
admin
/
components
/
SubscriptionDetailsPage
/
Tabs
/
Overview
/
SubscriptionAnnualProjection
Last commit date
index.tsx
10 months ago
styles.module.scss
10 months ago
utils.ts
10 months ago
utils.ts
83 lines
| 1 | import { Donation } from '@givewp/donations/admin/components/types'; |
| 2 | import { ApexOptions } from 'apexcharts'; |
| 3 | import { getCompletedDonations } from '../SubscriptionStats'; |
| 4 | |
| 5 | /** |
| 6 | * Get completed donations for the current year |
| 7 | * |
| 8 | * @since 4.8.0 |
| 9 | */ |
| 10 | export const getCurrentYearCompletedDonations = (donations: Donation[]): Donation[] => { |
| 11 | const completedDonations = getCompletedDonations(donations); |
| 12 | const currentYear = new Date().getFullYear(); |
| 13 | |
| 14 | return completedDonations?.filter(donation => { |
| 15 | const donationDate = new Date(donation.createdAt.date); |
| 16 | return donationDate.getFullYear() === currentYear; |
| 17 | }); |
| 18 | }; |
| 19 | |
| 20 | /** |
| 21 | * Calculate total contributions from completed donations |
| 22 | * |
| 23 | * @since 4.8.0 |
| 24 | */ |
| 25 | export const calculateTotalContributions = (completedDonations: Donation[]): number => { |
| 26 | if (!completedDonations) { |
| 27 | return 0; |
| 28 | } |
| 29 | |
| 30 | return completedDonations?.reduce((acc, donation) => acc + Number(donation.amount.value), 0); |
| 31 | }; |
| 32 | |
| 33 | /** |
| 34 | * Calculate progress percentage based on completed vs projected amounts |
| 35 | * |
| 36 | * @since 4.8.0 |
| 37 | */ |
| 38 | export const calculateProgressPercentage = (completedAmount: number, projectedAmount: number): number => { |
| 39 | const progress = Math.ceil((completedAmount / projectedAmount) * 100); |
| 40 | |
| 41 | return Math.min(progress, 100); |
| 42 | }; |
| 43 | |
| 44 | /** |
| 45 | * @since 4.8.0 |
| 46 | */ |
| 47 | export const chartOptions = (label: string): ApexOptions => { |
| 48 | return { |
| 49 | chart: { |
| 50 | height: 1024, |
| 51 | type: 'radialBar', |
| 52 | }, |
| 53 | plotOptions: { |
| 54 | radialBar: { |
| 55 | hollow: { |
| 56 | margin: 15, |
| 57 | size: '60%', |
| 58 | }, |
| 59 | dataLabels: { |
| 60 | /** |
| 61 | * The "name" is the top label, here it is the value/amount |
| 62 | * The "value" is the percent progress |
| 63 | */ |
| 64 | name: { |
| 65 | offsetY: 20, |
| 66 | show: true, |
| 67 | color: '#4B5563', |
| 68 | fontSize: '12px', |
| 69 | }, |
| 70 | value: { |
| 71 | offsetY: -20, |
| 72 | color: '#060C1A', |
| 73 | fontSize: '24px', |
| 74 | show: true, |
| 75 | }, |
| 76 | }, |
| 77 | }, |
| 78 | }, |
| 79 | colors: ['#459948'], |
| 80 | labels: [label], |
| 81 | }; |
| 82 | }; |
| 83 |