useActionCallback.js
2 years ago
useActionDetail.js
2 years ago
useActions.js
2 years ago
useActionsEdit.js
2 years ago
useCurrentAction.js
2 years ago
useRequestFields.js
2 years ago
useSanitizeFieldsMap.js
2 years ago
useUpdateCurrentAction.js
2 years ago
useUpdateCurrentActionMeta.js
2 years ago
withCurrentAction.js
2 years ago
withDispatchActionLoading.js
2 years ago
withRequestFields.js
2 years ago
withSelectActionLoading.js
2 years ago
useRequestFields.js
147 lines
| 1 | import BaseAction from '../abstract/BaseAction'; |
| 2 | import CurrentActionEditContext from '../context/CurrentActionEditContext'; |
| 3 | |
| 4 | const { |
| 5 | useSelect, |
| 6 | } = wp.data; |
| 7 | |
| 8 | const { |
| 9 | useContext, |
| 10 | } = wp.element; |
| 11 | |
| 12 | const getRequestFields = actions => { |
| 13 | const requestFields = []; |
| 14 | |
| 15 | for ( const action of actions ) { |
| 16 | const { |
| 17 | [ action.type ]: current = {}, |
| 18 | } = action.settings; |
| 19 | |
| 20 | if ( !current.requestFields ) { |
| 21 | continue; |
| 22 | } |
| 23 | |
| 24 | for ( const requestField of current.requestFields ) { |
| 25 | const index = requestFields.findIndex( |
| 26 | field => field.value === requestField.name ); |
| 27 | |
| 28 | if ( -1 !== index ) { |
| 29 | continue; |
| 30 | } |
| 31 | |
| 32 | requestFields.push( { |
| 33 | from: action.type, |
| 34 | id: action.id, |
| 35 | label: requestField.name, |
| 36 | value: requestField.name, |
| 37 | name: requestField.name, |
| 38 | help: requestField.help, |
| 39 | } ); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | return requestFields; |
| 44 | }; |
| 45 | |
| 46 | function getComputedFields( fields, actions, computed ) { |
| 47 | /** |
| 48 | * @type {BaseAction[]} |
| 49 | */ |
| 50 | actions = actions.map( item => new BaseAction( item ) ); |
| 51 | const nameSet = new Set(); |
| 52 | |
| 53 | for ( const baseComputedField of computed ) { |
| 54 | /** |
| 55 | * @type {BaseComputedField} |
| 56 | */ |
| 57 | const current = new baseComputedField(); |
| 58 | |
| 59 | for ( let action of actions ) { |
| 60 | if ( !current.isSupported( action ) ) { |
| 61 | continue; |
| 62 | } |
| 63 | current.setAction( action ); |
| 64 | current.hasInList = false; |
| 65 | |
| 66 | const label = current.getLabel(); |
| 67 | let name = current.getName(); |
| 68 | |
| 69 | if ( nameSet.has( name ) ) { |
| 70 | current.hasInList = true; |
| 71 | |
| 72 | name = current.getName(); |
| 73 | } |
| 74 | |
| 75 | if ( fields.some( ( { value } ) => value === name ) ) { |
| 76 | continue; |
| 77 | } |
| 78 | |
| 79 | nameSet.add( name ); |
| 80 | |
| 81 | fields.push( { |
| 82 | from: action.type, |
| 83 | id: action.id, |
| 84 | label: label || name, |
| 85 | value: name, |
| 86 | name: name, |
| 87 | help: current.getHelp(), |
| 88 | } ); |
| 89 | } |
| 90 | |
| 91 | if ( current.action || !current.isSupportedGlobal() ) { |
| 92 | continue; |
| 93 | } |
| 94 | const label = current.getLabel(); |
| 95 | const name = current.getName(); |
| 96 | |
| 97 | fields.push( { |
| 98 | label: label || name, |
| 99 | value: name, |
| 100 | name: name, |
| 101 | help: current.getHelp(), |
| 102 | } ); |
| 103 | } |
| 104 | |
| 105 | return fields; |
| 106 | } |
| 107 | |
| 108 | function useRequestFields( { returnOnEmptyCurrentAction = true } = {} ) { |
| 109 | const meta = useSelect( ( select ) => { |
| 110 | return select( 'core/editor' ).getEditedPostAttribute( 'meta' ) || {}; |
| 111 | }, [] ); |
| 112 | |
| 113 | const actionProps = useContext( CurrentActionEditContext ); |
| 114 | |
| 115 | const { currentAction, computed } = useSelect( |
| 116 | select => ( |
| 117 | { |
| 118 | currentAction: select( 'jet-forms/actions' ).getCurrentAction(), |
| 119 | computed: select( 'jet-forms/actions' ).getComputedFields(), |
| 120 | } |
| 121 | ), |
| 122 | [], |
| 123 | ); |
| 124 | |
| 125 | if ( !actionProps?.actionId && returnOnEmptyCurrentAction ) { |
| 126 | return []; |
| 127 | } |
| 128 | |
| 129 | const actions = JSON.parse( meta._jf_actions || '[]' ); |
| 130 | |
| 131 | // current action could be empty object |
| 132 | if ( !Number.isNaN( Number( currentAction?.index ) ) ) { |
| 133 | actions.splice( currentAction.index ); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Should be deprecated |
| 138 | * |
| 139 | * @type {*[]} |
| 140 | */ |
| 141 | const fields = getRequestFields( actions ); |
| 142 | |
| 143 | return getComputedFields( fields, actions, computed ); |
| 144 | } |
| 145 | |
| 146 | export { getRequestFields, getComputedFields }; |
| 147 | export default useRequestFields; |