PluginProbe
CartFlows – Funnel Builder & Checkout Plugin for WooCommerce / 3.1.0
CartFlows – Funnel Builder & Checkout Plugin for WooCommerce v3.1.0
3.3.0 3.2.1 3.2.0 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 3.0.1 trunk 1.0.4 1.1.0 1.1.0.1 1.1.1 1.1.10 1.1.11 1.1.12 1.1.13 1.1.14 1.1.15 1.1.16 1.1.17 1.1.18 1.1.19 1.1.2 All 162 releases
cartflows / modules / gutenberg / src / components / reset / index.js

index.js in CartFlows – Funnel Builder & Checkout Plugin for WooCommerce 3.1.0, at modules/gutenberg/src/components/reset/index.js

111 lines 2.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { blocksAttributes } from '@Attributes/getBlocksDefaultAttributes';
2 import { select } from '@wordpress/data';
3 import { Button, Tooltip, Dashicon } from '@wordpress/components';
4 import { __ } from '@wordpress/i18n';
5 import { useState } from '@wordpress/element';
6
7 const UAGReset = ( props ) => {
8 const { onReset, attributeNames, setAttributes } = props;
9
10 const [ refreshPresets, toggleRefreshPresets ] = useState( false );
11
12 const { getSelectedBlock } = select( 'core/block-editor' );
13
14 const getBlockResetValue = () => {
15 const selectedBlockName = getSelectedBlock()?.name.split( '/' ).pop();
16 let defaultValues = false;
17
18 if (
19 attributeNames &&
20 'undefined' !== typeof blocksAttributes[ selectedBlockName ]
21 ) {
22 attributeNames.map( ( attributeName ) => {
23 if ( attributeName ) {
24 const blockDefaultAttributeValue =
25 'undefined' !==
26 typeof blocksAttributes[ selectedBlockName ][
27 attributeName
28 ]?.default
29 ? blocksAttributes[ selectedBlockName ][
30 attributeName
31 ]?.default
32 : '';
33 defaultValues = {
34 ...defaultValues,
35 [ attributeName ]: blockDefaultAttributeValue,
36 };
37 }
38
39 return attributeName;
40 } );
41 }
42
43 return defaultValues;
44 };
45
46 const getResetState = () => {
47 const defaultValues = getBlockResetValue();
48 const selectedBlockAttributes = getSelectedBlock()?.attributes;
49 let resetDisableState = true;
50
51 attributeNames.map( ( attributeName ) => {
52 if (
53 selectedBlockAttributes?.[ attributeName ] &&
54 selectedBlockAttributes?.[ attributeName ] !==
55 defaultValues?.[ attributeName ]
56 ) {
57 resetDisableState = false;
58 }
59 return attributeName;
60 } );
61
62 return resetDisableState;
63 };
64
65 const resetDisableState = getResetState();
66
67 const resetHandler = () => {
68 const defaultValues = getBlockResetValue();
69
70 if ( attributeNames ) {
71 attributeNames.map( ( attributeName ) => {
72 if ( attributeName ) {
73 if ( setAttributes ) {
74 setAttributes( {
75 [ attributeName ]: defaultValues?.[ attributeName ],
76 } );
77 }
78 }
79 toggleRefreshPresets( ! refreshPresets );
80 return attributeName;
81 } );
82 }
83
84 if ( onReset ) {
85 onReset( defaultValues );
86 }
87 };
88
89 return (
90 <Tooltip
91 text={ __( 'Reset', 'ultimate-addons-for-gutenberg' ) }
92 key={ 'reset' }
93 >
94 <Button
95 className="uagb-reset"
96 isSecondary
97 isSmall
98 onClick={ ( e ) => {
99 e.preventDefault();
100 resetHandler();
101 } }
102 disabled={ resetDisableState }
103 >
104 <Dashicon icon="image-rotate" />
105 </Button>
106 </Tooltip>
107 );
108 };
109
110 export default UAGReset;
111