actions.js
2 years ago
constants.js
2 years ago
dispatchers.js
2 years ago
index.js
2 years ago
reducer.js
2 years ago
selectors.js
2 years ago
reducer.js
81 lines
| 1 | import dispatchers from './dispatchers'; |
| 2 | import humanReadablePreset from '../../preset/helpers/humanReadablePreset'; |
| 3 | |
| 4 | const { |
| 5 | select, |
| 6 | } = wp.data; |
| 7 | const { |
| 8 | __, |
| 9 | } = wp.i18n; |
| 10 | |
| 11 | const emptyCondition = function ( condition ) { |
| 12 | const operatorOptions = select( 'jet-forms/block-conditions' ). |
| 13 | getOperator( condition?.operator ); |
| 14 | |
| 15 | if ( !operatorOptions ) { |
| 16 | return ''; |
| 17 | } |
| 18 | const field = condition?.field || '(no field)'; |
| 19 | |
| 20 | return [ |
| 21 | `<code>${ field }</code>`, |
| 22 | operatorOptions.label, |
| 23 | ].join( ' ' ); |
| 24 | }; |
| 25 | |
| 26 | const DEFAULT_STATE = { |
| 27 | functions: [], |
| 28 | operators: [], |
| 29 | conditionReaders: { |
| 30 | default: function ( condition ) { |
| 31 | const operatorOptions = select( 'jet-forms/block-conditions' ). |
| 32 | getOperator( condition?.operator ); |
| 33 | |
| 34 | if ( !operatorOptions ) { |
| 35 | return ''; |
| 36 | } |
| 37 | const field = condition?.field || '(no field)'; |
| 38 | const value = ( |
| 39 | humanReadablePreset( condition.value, 'b' ) || '(no value)' |
| 40 | ); |
| 41 | |
| 42 | return [ |
| 43 | `<code>${ field }</code>`, |
| 44 | operatorOptions.label, |
| 45 | `<code>${ value }</code>`, |
| 46 | ].join( ' ' ); |
| 47 | }, |
| 48 | empty: emptyCondition, |
| 49 | not_empty: emptyCondition, |
| 50 | render_state: function ( condition ) { |
| 51 | const states = ( |
| 52 | condition?.render_state ?? [] |
| 53 | ).map( |
| 54 | current => `<code>${ current }</code>`, |
| 55 | ); |
| 56 | |
| 57 | const label = 1 === states.length |
| 58 | ? __( 'Is render state', 'jet-form-builder' ) |
| 59 | : __( |
| 60 | 'One of the render states', |
| 61 | 'jet-form-builder', |
| 62 | ); |
| 63 | |
| 64 | return [ |
| 65 | label, |
| 66 | states.join( ', ' ), |
| 67 | ].join( ': ' ); |
| 68 | }, |
| 69 | }, |
| 70 | renderStates: [], |
| 71 | }; |
| 72 | |
| 73 | export default function ( state = DEFAULT_STATE, action ) { |
| 74 | const callback = dispatchers[ action?.type ]; |
| 75 | |
| 76 | if ( callback ) { |
| 77 | return callback( state, action ); |
| 78 | } |
| 79 | |
| 80 | return state; |
| 81 | } |