PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 3.1.6
JetFormBuilder — Dynamic Blocks Form Builder v3.1.6
3.6.3.1 3.6.3 3.6.2.2 3.6.2.1 3.6.2 3.6.1.1 3.6.1 3.6.0.1 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.1.0 2.1.1 2.1.10 2.1.11 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 3.0.0 3.0.0.1 3.0.0.2 3.0.0.3 3.0.1 3.0.1.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.0.1 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 3.2.0 3.2.1 3.2.2 3.2.3 3.3.0 3.3.1 3.3.2 3.3.3 3.3.3.1 3.3.4 3.3.4.1 3.3.4.2 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.4.5.1 3.4.5.2 3.4.6 3.4.7 3.4.7.1 3.5.0 3.5.1 3.5.1.1 3.5.1.2 3.5.2 3.5.2.1 3.5.3 3.5.4 3.5.5 3.5.6 3.5.6.1 3.5.6.2 3.5.6.3 3.6.0
jetformbuilder / assets / src / package / actions / hooks / useRequestFields.js
jetformbuilder / assets / src / package / actions / hooks Last commit date
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;