useDonorDonations.ts
60 lines
| 1 | /** |
| 2 | * @since 4.4.0 |
| 3 | */ |
| 4 | import {useEntityRecords} from '@wordpress/core-data'; |
| 5 | |
| 6 | /** |
| 7 | * @since 4.4.0 |
| 8 | */ |
| 9 | export interface DonationResponse { |
| 10 | id: number; |
| 11 | formTitle: string; |
| 12 | createdAt: { |
| 13 | date: string; |
| 14 | timezone_type: number; |
| 15 | timezone: string; |
| 16 | }; |
| 17 | status: string; |
| 18 | amount: { |
| 19 | value: string; |
| 20 | valueInMinorUnits: string; |
| 21 | currency: string; |
| 22 | }; |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * @since 4.4.0 |
| 27 | */ |
| 28 | interface DonationsQueryParams { |
| 29 | donorId: number; |
| 30 | page?: number; |
| 31 | perPage?: number; |
| 32 | mode?: 'test' | 'live'; |
| 33 | status?: 'any' | 'publish' | 'give_subscription' | 'pending' | 'processing' | 'refunded' | 'revoked' | 'failed' | 'cancelled' | 'abandoned' | 'preapproval'; |
| 34 | sort?: 'id' | 'createdAt' | 'updatedAt' | 'status' | 'amount' | 'feeAmountRecovered' | 'donorId' | 'firstName' | 'lastName'; |
| 35 | direction?: 'ASC' | 'DESC'; |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * @since 4.4.0 |
| 40 | */ |
| 41 | export function useDonorDonations({donorId, page = 1, perPage = 5, mode = 'live', status = 'any', sort = 'createdAt', direction = 'DESC'}: DonationsQueryParams) { |
| 42 | const query = { |
| 43 | page, |
| 44 | per_page: perPage, |
| 45 | mode, |
| 46 | donorId: donorId, |
| 47 | status, |
| 48 | sort, |
| 49 | direction |
| 50 | }; |
| 51 | |
| 52 | const {records, hasResolved, isResolving} = useEntityRecords<DonationResponse>('givewp', 'donation', query); |
| 53 | |
| 54 | return { |
| 55 | donations: records, |
| 56 | hasResolved, |
| 57 | isResolving |
| 58 | }; |
| 59 | } |
| 60 |