admin
9 months ago
editor
1 year ago
hooks
1 year ago
store
1 year ago
views
1 year ago
entity.ts
1 year ago
types.ts
1 year ago
utils.ts
9 months ago
utils.ts
95 lines
| 1 | import {useEntityRecord} from '@wordpress/core-data'; |
| 2 | import {Campaign} from '@givewp/campaigns/admin/components/types'; |
| 3 | import type {GiveCampaignOptions} from '@givewp/campaigns/types'; |
| 4 | import apiFetch from '@wordpress/api-fetch'; |
| 5 | |
| 6 | declare const window: { |
| 7 | GiveCampaignOptions: GiveCampaignOptions; |
| 8 | } & Window; |
| 9 | |
| 10 | /** |
| 11 | * @since 4.0.0 |
| 12 | */ |
| 13 | export function useCampaignEntityRecord(campaignId?: number) { |
| 14 | const urlParams = new URLSearchParams(window.location.search); |
| 15 | |
| 16 | const { |
| 17 | record: campaign, |
| 18 | hasResolved, |
| 19 | isResolving, |
| 20 | save, |
| 21 | edit, |
| 22 | }: { |
| 23 | record: Campaign; |
| 24 | hasResolved: boolean; |
| 25 | isResolving: boolean; |
| 26 | save: () => any; |
| 27 | edit: (data: Campaign | Partial<Campaign>) => void; |
| 28 | } = useEntityRecord('givewp', 'campaign', campaignId ?? urlParams.get('id')); |
| 29 | |
| 30 | return {campaign, hasResolved, isResolving, save, edit}; |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * @since 4.0.0 |
| 35 | */ |
| 36 | export function getCampaignOptionsWindowData(): GiveCampaignOptions { |
| 37 | return window.GiveCampaignOptions; |
| 38 | } |
| 39 | |
| 40 | export function handleTooltipDismiss(id: string) { |
| 41 | return apiFetch({ |
| 42 | url: window.GiveCampaignOptions.adminUrl + '/admin-ajax.php?action=' + id, |
| 43 | method: 'POST', |
| 44 | }) |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * @since 4.0.0 |
| 49 | */ |
| 50 | export function amountFormatter(currency: Intl.NumberFormatOptions['currency'], options?: Intl.NumberFormatOptions): Intl.NumberFormat { |
| 51 | return new Intl.NumberFormat(navigator.language, { |
| 52 | style: 'currency', |
| 53 | currency: currency, |
| 54 | ...options |
| 55 | }); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * @since 4.0.0 |
| 60 | */ |
| 61 | export async function updateUserNoticeOptions(metaKey: string){ |
| 62 | try { |
| 63 | const currentUser = await apiFetch( { path: '/wp/v2/users/me' } ); |
| 64 | // @ts-ignore |
| 65 | const currentUserId = currentUser?.id; |
| 66 | |
| 67 | return await wp.data.dispatch('core').saveEntityRecord('root', 'user', { |
| 68 | id: currentUserId, |
| 69 | meta: { |
| 70 | [metaKey]: true |
| 71 | } |
| 72 | }); |
| 73 | } catch (error) { |
| 74 | console.error('Error updating user meta:', error); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * @since 4.0.0 |
| 80 | */ |
| 81 | export async function createCampaignPage(campaignId: number) { |
| 82 | try { |
| 83 | const response = await apiFetch({ |
| 84 | path: `/givewp/v3/campaigns/${campaignId}/page`, |
| 85 | method: 'POST' |
| 86 | }); |
| 87 | |
| 88 | return response as { |
| 89 | id: number, |
| 90 | }; |
| 91 | } catch (error) { |
| 92 | console.error('Error creating Campaign page:', error); |
| 93 | } |
| 94 | } |
| 95 |