-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSettings.js
More file actions
187 lines (166 loc) · 4.38 KB
/
Copy pathSettings.js
File metadata and controls
187 lines (166 loc) · 4.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/**
* External dependencies
*/
import styled from '@emotion/styled';
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import {
__experimentalToolsPanel as ToolsPanel,
Spinner,
PanelBody,
__experimentalToolsPanelItem as ToolsPanelItem,
Button,
Flex,
__experimentalSpacer as Spacer,
Notice,
} from '@wordpress/components';
/**
* Internal dependencies
*/
import EnableCheckbox from './EnableCheckbox';
import { useSettings, useData } from '../hooks';
import Property from './Property';
import ButtonSettings from './ButtonSettings';
import ClearCacheButton from './ClearCacheButton';
const PanelDescription = styled.div`
grid-column: span 2;
`;
const Settings = ( props ) => {
const { attributes, setAttributes, name } = props;
const { freemius_enabled, freemius, freemius_modifications } = attributes;
const { structure, isLoading } = useSettings( 'freemius_defaults' );
const { data, DataView, errorMessage, defaults } = useData();
if ( isLoading || ! structure )
return (
<PanelBody title={ __( 'Freemius', 'freemius' ) }>
<Spinner />
</PanelBody>
);
const resetAll = () => {
setAttributes( { freemius: undefined } );
};
const onChangeHandler = ( key, val, defaultValue ) => {
let newValue = { ...freemius };
// if the value is the same as the default value, delete the key
if (
( defaultValue === val || val === undefined ) &&
val !== defaults[ key ]
)
delete newValue[ key ];
else newValue[ key ] = val;
//if empty, set to undefined
if ( Object.keys( newValue ).length === 0 ) newValue = undefined;
setAttributes( { freemius: newValue } );
};
const getValueFor = ( key ) => {
return freemius?.[ key ];
};
const getPlaceholderFor = ( key ) => {
return data?.[ key ] || getValueFor( key );
};
return (
<ToolsPanel
className={ 'freemius-button-scope-settings' }
resetAll={ () => resetAll() }
label={ __( 'Freemius', 'freemius' ) }
shouldRenderPlaceholderItems={ true }
dropdownMenuProps={ {
popoverProps: {
placement: 'left-start',
offset: 259, // the width of the panel with paddings and border
},
} }
>
<PanelDescription>
<DataView />
<EnableCheckbox
label={
props.name == 'core/button'
? __( 'Enable Freemius Checkout', 'freemius' )
: __( 'Enable Freemius', 'freemius' )
}
help={
props.name == 'core/button'
? __(
'Open a Freemius Checkout when the button is clicked.',
'freemius'
)
: __( 'Enable Freemius for this area.', 'freemius' )
}
{ ...props }
/>
{ freemius_enabled && (
<Flex gap={ 2 }>
{ freemius_modifications && (
<Button
onClick={ () =>
setAttributes( {
freemius_modifications: undefined,
} )
}
variant="secondary"
>
{ __( 'Reset Modifications', 'freemius' ) }
</Button>
) }
<ClearCacheButton />
</Flex>
) }
<Spacer />
{ freemius_enabled && errorMessage && (
<Notice status="error" isDismissible={ false }>
{ errorMessage }
</Notice>
) }
</PanelDescription>
{ freemius_enabled && (
<>
{ name == 'core/button' && <ButtonSettings { ...props } /> }
{ Object.entries( structure.properties ).map(
( [ key, item ] ) => {
const value = getValueFor( key );
const placeholder = getPlaceholderFor( key );
// do not show deprecated fields if they are not set
if ( item.isDeprecated && ! value ) return null;
return (
<Property
key={ key }
label={ item.label || key }
options={ item.options }
id={ key }
help={ item.description }
code={ item?.code }
defaultValue={ item.default }
isDeprecated={ item.isDeprecated }
isRequired={ item.isRequired }
value={ value }
placeholder={ placeholder }
type={ item.type || 'string' }
onChange={ ( val ) =>
onChangeHandler(
key,
val,
item.default
)
}
/>
);
}
) }
<ToolsPanelItem
className="freemius-button-scope"
hasValue={ () => {
return true;
} }
label={ '' }
//onDeselect={() => onChangeHandler(undefined)}
ShownByDefault={ true }
></ToolsPanelItem>
</>
) }
</ToolsPanel>
);
};
export default Settings;