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
setDesignSettings.ts
2 years ago
useDonationFormPubSub.ts
2 years ago
useDonationFormPubSub.ts
166 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 | DESIGN_SETTINGS: 'preview:design-settings', |
| 15 | } |
| 16 | |
| 17 | /** |
| 18 | * Simple Publish/Subscribe system used for handling form state in preview mode |
| 19 | * |
| 20 | * @since 3.1.0 |
| 21 | */ |
| 22 | export default function useDonationFormPubSub() { |
| 23 | |
| 24 | const events = {}; |
| 25 | |
| 26 | useEffect(() => { |
| 27 | addEventListener('message', eventListener, false); |
| 28 | return () => removeEventListener('message', eventListener); |
| 29 | }, []); |
| 30 | |
| 31 | const getMessage = (message: MessageEvent) => { |
| 32 | // handle IframeResizer messages |
| 33 | if (typeof message.data === 'string') { |
| 34 | if (message.data.includes('[iFrameSizer]message:')) { |
| 35 | return JSON.parse(message.data.replace('[iFrameSizer]message:', '')); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | return message.data |
| 40 | } |
| 41 | |
| 42 | const eventListener = (message: MessageEvent) => { |
| 43 | const {event, data} = getMessage(message); |
| 44 | |
| 45 | if (events[event]) { |
| 46 | |
| 47 | const filtered = {}; |
| 48 | // Allow only primitive values |
| 49 | Object.entries(data).forEach(([key, value]) => { |
| 50 | if (typeof value !== 'function' && typeof value !== 'object') { |
| 51 | filtered[key] = value; |
| 52 | } |
| 53 | }); |
| 54 | |
| 55 | events[event].forEach((callback: (data: any) => void) => { |
| 56 | callback(filtered); |
| 57 | }); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | const subscribe = (event: string, callback: Function) => { |
| 62 | if (!events[event]) { |
| 63 | events[event] = []; |
| 64 | } |
| 65 | |
| 66 | events[event].push(callback); |
| 67 | } |
| 68 | |
| 69 | const publish = (event: string, data: any, iframeRef: RefObject<any>) => { |
| 70 | if (iframeRef.current?.sendMessage) { |
| 71 | iframeRef.current.sendMessage({ |
| 72 | event, |
| 73 | data |
| 74 | }); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | const publishSettings = (data: RequireAtLeastOne<FormSettings>) => { |
| 79 | publish(PREVIEW_EVENTS.SETTINGS, data, iframeRef) |
| 80 | } |
| 81 | |
| 82 | |
| 83 | const publishDesignSettings = (data: RequireAtLeastOne<FormSettings>) => { |
| 84 | publish(PREVIEW_EVENTS.DESIGN_SETTINGS, data, iframeRef) |
| 85 | } |
| 86 | |
| 87 | const publishGoal = (data: RequireAtLeastOne<FormGoal>) => { |
| 88 | publish(PREVIEW_EVENTS.GOAL, data, iframeRef) |
| 89 | } |
| 90 | |
| 91 | const publishGoalType = (type: string) => { |
| 92 | const isMoney = ['amount', 'amountFromSubscriptions'].includes(type); |
| 93 | |
| 94 | publish(PREVIEW_EVENTS.GOAL, { |
| 95 | type, |
| 96 | label: type, |
| 97 | typeIsCount: !isMoney, |
| 98 | typeIsMoney: isMoney, |
| 99 | }, iframeRef) |
| 100 | } |
| 101 | |
| 102 | const publishColors = (data: RequireAtLeastOne<FormColors>) => { |
| 103 | publish(PREVIEW_EVENTS.COLORS, data, iframeRef) |
| 104 | } |
| 105 | |
| 106 | const publishCss = (data: {customCss: string} ) => { |
| 107 | publish(PREVIEW_EVENTS.CSS, data, iframeRef) |
| 108 | } |
| 109 | |
| 110 | const subscribeToSettings = (callback: (data: FormSettings) => void) => { |
| 111 | subscribe(PREVIEW_EVENTS.SETTINGS, callback) |
| 112 | } |
| 113 | |
| 114 | const subscribeToDesignSettings = (callback: (data) => void) => { |
| 115 | subscribe(PREVIEW_EVENTS.DESIGN_SETTINGS, callback) |
| 116 | } |
| 117 | |
| 118 | const subscribeToGoal = (callback: (data: FormGoal) => void) => { |
| 119 | subscribe(PREVIEW_EVENTS.GOAL, callback) |
| 120 | } |
| 121 | |
| 122 | const subscribeToColors = (callback: (data: FormColors) => void) => { |
| 123 | subscribe(PREVIEW_EVENTS.COLORS, callback) |
| 124 | } |
| 125 | |
| 126 | const subscribeToCss = (callback: (data: {customCss: string}) => void) => { |
| 127 | subscribe(PREVIEW_EVENTS.CSS, callback) |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Unsubscribe from event |
| 132 | * |
| 133 | * @param event |
| 134 | */ |
| 135 | const unsubscribe = (event: string) => { |
| 136 | if (events[event]) { |
| 137 | delete events[event]; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Unsubscribe from all event |
| 143 | */ |
| 144 | const unsubscribeAll = () => { |
| 145 | for (const key in PREVIEW_EVENTS) { |
| 146 | delete events[key]; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | return { |
| 151 | unsubscribe, |
| 152 | unsubscribeAll, |
| 153 | publishGoal, |
| 154 | publishGoalType, |
| 155 | publishColors, |
| 156 | publishCss, |
| 157 | publishSettings, |
| 158 | publishDesignSettings, |
| 159 | subscribeToGoal, |
| 160 | subscribeToColors, |
| 161 | subscribeToSettings, |
| 162 | subscribeToCss, |
| 163 | subscribeToDesignSettings |
| 164 | } |
| 165 | } |
| 166 |