withSelectGateways.js
66 lines
| 1 | import gatewayAttr from '../helpers/gatewayAttr'; |
| 2 | import gatewayLabel from '../helpers/gatewayLabel'; |
| 3 | import globalTab from '../../actions/helpers/globalTab'; |
| 4 | |
| 5 | function withSelectGateways( select ) { |
| 6 | const store = select( 'jet-forms/gateways' ); |
| 7 | |
| 8 | const gatewayRequestId = store.getCurrentRequestId(); |
| 9 | const gatewaySpecific = store.getGatewaySpecific(); |
| 10 | const scenario = store.getScenario(); |
| 11 | |
| 12 | const CURRENT_GATEWAY = store.getGatewayId(); |
| 13 | const { id: CURRENT_SCENARIO = 'PAY_NOW' } = scenario; |
| 14 | |
| 15 | const { |
| 16 | use_global = false, |
| 17 | } = gatewaySpecific; |
| 18 | |
| 19 | const currentTab = globalTab( { slug: CURRENT_GATEWAY } ); |
| 20 | |
| 21 | const getSpecificOrGlobal = ( key, ifEmpty = '' ) => { |
| 22 | return ( |
| 23 | use_global ? ( |
| 24 | currentTab[ key ] || ifEmpty |
| 25 | ) : ( |
| 26 | gatewaySpecific[ key ] || ifEmpty |
| 27 | ) |
| 28 | ); |
| 29 | }; |
| 30 | |
| 31 | const callableGateway = gatewayAttr( 'additional' ); |
| 32 | const additionalSourceGateway = callableGateway( CURRENT_GATEWAY ); |
| 33 | |
| 34 | const loadingGateway = select( 'jet-forms/actions' ). |
| 35 | getLoading( gatewayRequestId ); |
| 36 | |
| 37 | const globalGatewayLabel = gatewayAttr( 'labels' ); |
| 38 | const specificGatewayLabel = gatewayLabel( CURRENT_GATEWAY ); |
| 39 | |
| 40 | const customGatewayLabel = function ( key ) { |
| 41 | return globalGatewayLabel( `${ CURRENT_GATEWAY }.${ key }` ); |
| 42 | }; |
| 43 | const scenarioLabel = function ( key ) { |
| 44 | return customGatewayLabel( `scenario.${ CURRENT_SCENARIO }.${ key }` ); |
| 45 | }; |
| 46 | |
| 47 | return { |
| 48 | gatewayGeneral: store.getGateway(), |
| 49 | gatewayRequest: store.getCurrentRequest(), |
| 50 | scenarioSource: additionalSourceGateway[ CURRENT_SCENARIO ] || {}, |
| 51 | currentScenario: scenario[ CURRENT_SCENARIO ] || {}, |
| 52 | CURRENT_SCENARIO, |
| 53 | gatewayScenario: scenario, |
| 54 | additionalSourceGateway, |
| 55 | gatewaySpecific, |
| 56 | gatewayRequestId, |
| 57 | loadingGateway, |
| 58 | getSpecificOrGlobal, |
| 59 | globalGatewayLabel, |
| 60 | specificGatewayLabel, |
| 61 | customGatewayLabel, |
| 62 | scenarioLabel, |
| 63 | }; |
| 64 | } |
| 65 | |
| 66 | export default withSelectGateways; |