admin
7 months ago
editor
1 year ago
hooks
1 year ago
store
1 year ago
views
1 year ago
types.ts
1 year ago
utils.ts
7 months ago
utils.ts
113 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.13.1 Return "record" instead of "campaign" |
| 12 | * @since 4.0.0 |
| 13 | */ |
| 14 | export function useCampaignEntityRecord(campaignId?: number) { |
| 15 | const urlParams = new URLSearchParams(window.location.search); |
| 16 | |
| 17 | const { |
| 18 | record: campaign, |
| 19 | hasResolved, |
| 20 | isResolving, |
| 21 | save, |
| 22 | edit, |
| 23 | }: { |
| 24 | record: Campaign; |
| 25 | hasResolved: boolean; |
| 26 | isResolving: boolean; |
| 27 | save: () => any; |
| 28 | edit: (data: Campaign | Partial<Campaign>) => void; |
| 29 | } = useEntityRecord('givewp', 'campaign', campaignId ?? urlParams.get('id')); |
| 30 | |
| 31 | return { record: campaign, hasResolved, isResolving, save, edit }; |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * @since 4.0.0 |
| 36 | */ |
| 37 | export function getCampaignOptionsWindowData(): GiveCampaignOptions { |
| 38 | return window.GiveCampaignOptions; |
| 39 | } |
| 40 | |
| 41 | export function handleTooltipDismiss(id: string) { |
| 42 | return apiFetch({ |
| 43 | url: window.GiveCampaignOptions.adminUrl + '/admin-ajax.php?action=' + id, |
| 44 | method: 'POST', |
| 45 | }) |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * @since 4.0.0 |
| 50 | */ |
| 51 | export function amountFormatter(currency: Intl.NumberFormatOptions['currency'], options?: Intl.NumberFormatOptions): Intl.NumberFormat { |
| 52 | return new Intl.NumberFormat(navigator.language, { |
| 53 | style: 'currency', |
| 54 | currency: currency, |
| 55 | ...options |
| 56 | }); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * @since 4.0.0 |
| 61 | */ |
| 62 | export async function updateUserNoticeOptions(metaKey: string) { |
| 63 | try { |
| 64 | const currentUser = await apiFetch({ path: '/wp/v2/users/me' }); |
| 65 | // @ts-ignore |
| 66 | const currentUserId = currentUser?.id; |
| 67 | |
| 68 | return await wp.data.dispatch('core').saveEntityRecord('root', 'user', { |
| 69 | id: currentUserId, |
| 70 | meta: { |
| 71 | [metaKey]: true |
| 72 | } |
| 73 | }); |
| 74 | } catch (error) { |
| 75 | console.error('Error updating user meta:', error); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * @since 4.0.0 |
| 81 | */ |
| 82 | export async function createCampaignPage(campaignId: number) { |
| 83 | try { |
| 84 | const response = await apiFetch({ |
| 85 | path: `/givewp/v3/campaigns/${campaignId}/page`, |
| 86 | method: 'POST' |
| 87 | }); |
| 88 | |
| 89 | return response as { |
| 90 | id: number, |
| 91 | }; |
| 92 | } catch (error) { |
| 93 | console.error('Error creating Campaign page:', error); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * @since 4.13.1 |
| 99 | */ |
| 100 | export async function updateCampaignStatus(campaignId: number, status: 'archived' | 'active') { |
| 101 | try { |
| 102 | const response = await apiFetch({ |
| 103 | path: `/givewp/v3/campaigns/${campaignId}`, |
| 104 | method: 'PUT', |
| 105 | data: { status } |
| 106 | }); |
| 107 | |
| 108 | return response as Campaign; |
| 109 | } catch (error) { |
| 110 | console.error('Error updating campaign status:', error); |
| 111 | } |
| 112 | } |
| 113 |