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