ConvertFieldAPIRulesToJoi.ts
2 years ago
PrepareFormData.ts
2 years ago
amountFormatter.ts
2 years ago
buildRegisterValidationOptions.ts
2 years ago
conditionOperatorFunctions.js
2 years ago
convertValuesToFormData.ts
2 years ago
createValidationObjectFromFields.js
2 years ago
generateRequestErrors.ts
2 years ago
getCurrentFormUrlData.js
2 years ago
getDefaultValuesFromSections.ts
2 years ago
getDonationFormNodeSettings.ts
2 years ago
getErrorByFieldName.ts
2 years ago
getWindowData.ts
2 years ago
groups.ts
2 years ago
handleFormRedirect.ts
2 years ago
handleFormSubmitRequest.ts
2 years ago
handleValidationRequest.ts
2 years ago
isRouteInlineRedirect.ts
2 years ago
memoNode.ts
2 years ago
mountWindowData.ts
2 years ago
postData.ts
2 years ago
postFormData.ts
2 years ago
registerFieldAndBuildProps.tsx
2 years ago
useDonationFormPubSub.ts
2 years ago
useDonationFormPubSub.ts
154 lines
| 1 | import {RefObject, useEffect} from 'react'; |
| 2 | import {iframeRef} from '@givewp/form-builder/components/canvas/DesignPreview'; |
| 3 | import {FormSettings} from '@givewp/form-builder/types'; |
| 4 | import {FormColors, FormGoal, RequireAtLeastOne} from '@givewp/forms/types'; |
| 5 | |
| 6 | /** |
| 7 | * Events used in design mode |
| 8 | */ |
| 9 | export const PREVIEW_EVENTS = { |
| 10 | SETTINGS: 'preview:settings', |
| 11 | GOAL: 'preview:goal', |
| 12 | COLORS: 'preview:colors', |
| 13 | CSS: 'preview:css', |
| 14 | } |
| 15 | |
| 16 | /** |
| 17 | * Simple Publish/Subscribe system used for handling form state in preview mode |
| 18 | * |
| 19 | * @since 3.1.0 |
| 20 | */ |
| 21 | export default function useDonationFormPubSub() { |
| 22 | |
| 23 | const events = {}; |
| 24 | |
| 25 | useEffect(() => { |
| 26 | addEventListener('message', eventListener, false); |
| 27 | return () => removeEventListener('message', eventListener); |
| 28 | }, []); |
| 29 | |
| 30 | const getMessage = (message: MessageEvent) => { |
| 31 | // handle IframeResizer messages |
| 32 | if (typeof message.data === 'string') { |
| 33 | if (message.data.includes('[iFrameSizer]message:')) { |
| 34 | return JSON.parse(message.data.replace('[iFrameSizer]message:', '')); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | return message.data |
| 39 | } |
| 40 | |
| 41 | const eventListener = (message: MessageEvent) => { |
| 42 | const {event, data} = getMessage(message); |
| 43 | |
| 44 | if (events[event]) { |
| 45 | |
| 46 | const filtered = {}; |
| 47 | // Allow only primitive values |
| 48 | Object.entries(data).forEach(([key, value]) => { |
| 49 | if (typeof value !== 'function' && typeof value !== 'object') { |
| 50 | filtered[key] = value; |
| 51 | } |
| 52 | }); |
| 53 | |
| 54 | events[event].forEach((callback: (data: any) => void) => { |
| 55 | callback(filtered); |
| 56 | }); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | const subscribe = (event: string, callback: Function) => { |
| 61 | if (!events[event]) { |
| 62 | events[event] = []; |
| 63 | } |
| 64 | |
| 65 | events[event].push(callback); |
| 66 | } |
| 67 | |
| 68 | const publish = (event: string, data: any, iframeRef: RefObject<any>) => { |
| 69 | if (iframeRef.current?.sendMessage) { |
| 70 | iframeRef.current.sendMessage({ |
| 71 | event, |
| 72 | data |
| 73 | }); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | const publishSettings = (data: RequireAtLeastOne<FormSettings>) => { |
| 78 | publish(PREVIEW_EVENTS.SETTINGS, data, iframeRef) |
| 79 | } |
| 80 | |
| 81 | const publishGoal = (data: RequireAtLeastOne<FormGoal>) => { |
| 82 | publish(PREVIEW_EVENTS.GOAL, data, iframeRef) |
| 83 | } |
| 84 | |
| 85 | const publishGoalType = (type: string) => { |
| 86 | const isMoney = ['amount', 'amountFromSubscriptions'].includes(type); |
| 87 | |
| 88 | publish(PREVIEW_EVENTS.GOAL, { |
| 89 | type, |
| 90 | label: type, |
| 91 | typeIsCount: !isMoney, |
| 92 | typeIsMoney: isMoney, |
| 93 | }, iframeRef) |
| 94 | } |
| 95 | |
| 96 | const publishColors = (data: RequireAtLeastOne<FormColors>) => { |
| 97 | publish(PREVIEW_EVENTS.COLORS, data, iframeRef) |
| 98 | } |
| 99 | |
| 100 | const publishCss = (data: {customCss: string} ) => { |
| 101 | publish(PREVIEW_EVENTS.CSS, data, iframeRef) |
| 102 | } |
| 103 | |
| 104 | const subscribeToSettings = (callback: (data: FormSettings) => void) => { |
| 105 | subscribe(PREVIEW_EVENTS.SETTINGS, callback) |
| 106 | } |
| 107 | |
| 108 | const subscribeToGoal = (callback: (data: FormGoal) => void) => { |
| 109 | subscribe(PREVIEW_EVENTS.GOAL, callback) |
| 110 | } |
| 111 | |
| 112 | const subscribeToColors = (callback: (data: FormColors) => void) => { |
| 113 | subscribe(PREVIEW_EVENTS.COLORS, callback) |
| 114 | } |
| 115 | |
| 116 | const subscribeToCss = (callback: (data: {customCss: string}) => void) => { |
| 117 | subscribe(PREVIEW_EVENTS.CSS, callback) |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Unsubscribe from event |
| 122 | * |
| 123 | * @param event |
| 124 | */ |
| 125 | const unsubscribe = (event: string) => { |
| 126 | if (events[event]) { |
| 127 | delete events[event]; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Unsubscribe from all event |
| 133 | */ |
| 134 | const unsubscribeAll = () => { |
| 135 | for (const key in PREVIEW_EVENTS) { |
| 136 | delete events[key]; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | return { |
| 141 | unsubscribe, |
| 142 | unsubscribeAll, |
| 143 | publishGoal, |
| 144 | publishGoalType, |
| 145 | publishColors, |
| 146 | publishCss, |
| 147 | publishSettings, |
| 148 | subscribeToGoal, |
| 149 | subscribeToColors, |
| 150 | subscribeToSettings, |
| 151 | subscribeToCss |
| 152 | } |
| 153 | } |
| 154 |