-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathWhiteLabelTabContent.js
313 lines (291 loc) · 7.05 KB
/
WhiteLabelTabContent.js
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/* global neveDash */
import { __ } from '@wordpress/i18n';
import useLicenseData from '../../../Hooks/useLicenseData';
import {
CircleFadingArrowUp,
LucideBuilding,
LucideMessageCircleQuestion,
LucidePlug,
LucideSettings,
LucideToggleRight,
LucideWallpaper,
} from 'lucide-react';
import TextInput from '../../Common/TextInput';
import { useReducer, useState } from '@wordpress/element';
import Button from '../../Common/Button';
import ControlWrap from '../../Controls/ControlWrap';
import Toggle from '../../Common/Toggle';
import Notice from '../../Common/Notice';
import { changeOption } from '../../../utils/rest';
import { useDispatch } from '@wordpress/data';
import { NEVE_STORE } from '../../../utils/constants';
const DUMMY_SECTIONS = {
agency: {
title: __('Agency Branding', 'neve'),
icon: LucideBuilding,
fields: {
author_name: {
type: 'text',
label: __('Agency Author', 'neve'),
},
author_url: {
type: 'text',
label: __('Agency Author URL', 'neve'),
},
starter_sites: {
type: 'toggle',
label: __('Hide Sites Library', 'neve'),
},
my_library: {
type: 'toggle',
label: __('Hide My Library', 'neve'),
},
},
},
theme: {
title: __('Theme Branding', 'neve'),
icon: LucideWallpaper,
fields: {
theme_name: {
type: 'text',
label: __('Theme Name', 'neve'),
},
theme_description: {
type: 'textarea',
label: __('Theme Description', 'neve'),
},
screenshot_url: {
type: 'text',
label: __('Screenshot URL', 'neve'),
},
},
},
plugin: {
title: __('Plugin Branding', 'neve'),
icon: LucidePlug,
fields: {
plugin_name: {
type: 'text',
label: __('Plugin Name', 'neve'),
},
plugin_description: {
type: 'textarea',
label: __('Plugin Description', 'neve'),
},
},
},
sidebar: {
title: __('Enable White Label', 'neve'),
icon: LucideToggleRight,
fields: {
white_label: {
type: 'toggle',
label: __('Hide Options from Dashboard', 'neve'),
},
license: {
type: 'toggle',
label: __('Enable License Hiding', 'neve'),
},
},
},
};
const PlaceholderComponent = () => {
return (
<div>
{Object.entries(DUMMY_SECTIONS).map(([id, section]) => {
const ICON = section.icon;
return (
<ControlWrap
key={id}
label={section.title}
icon={ICON}
locked={true}
>
<div className="grid gap-4">
{Object.entries(section.fields).map(
([sid, setting]) => {
return (
<div key={sid} className="opacity-75">
{setting.type === 'toggle' ? (
<Toggle
disabled
locked
checked={false}
label={setting.label}
/>
) : (
<TextInput
label={setting.label}
disabled={true}
name={sid}
/>
)}
</div>
);
}
)}
</div>
</ControlWrap>
);
})}
<div className="flex justify-end mt-6">
<Button isPrimary disabled>
{__('Save', 'neve')}
</Button>
</div>
</div>
);
};
const WhiteLabelSettings = () => {
const { strings } = neveDash;
const { fields, optionKey, options } = neveDash.whiteLabelData;
const allFields = Object.values(fields)
.map((all) => all.fields)
.reduce((acc, val) => ({ ...acc, ...val }), {});
// remap values from options if the field type is toggle parse for '0' and '1'.
const formDefaults = Object.keys(allFields).reduce((acc, key) => {
if (allFields[key].type === 'toggle') {
acc[key] = options[key] === '1';
} else {
acc[key] = options[key];
}
return acc;
}, {});
const [loading, setLoading] = useState(false);
const [formData, setFormData] = useReducer((state, action) => {
return {
...state,
[action.name]: action.value,
};
}, formDefaults);
const { setToast } = useDispatch(NEVE_STORE);
if (!neveDash.whiteLabelData) {
return (
<Notice icon={CircleFadingArrowUp}>{strings.updateOldPro}</Notice>
);
}
const handleSubmit = (e) => {
e.preventDefault();
setLoading(true);
changeOption(optionKey, JSON.stringify(formData), false, false)
.then((r) => {
if (r.success) {
setToast(__('White Label settings saved.', 'neve'));
neveDash.whiteLabelData.options = formData;
return;
}
setToast(r.message ? r.message : false);
})
.catch((err) => {
setToast(err.message ? err.message : false);
})
.finally(() => {
setLoading(false);
});
};
return (
<form onSubmit={handleSubmit}>
{Object.entries(fields).map(([id, section]) => {
// We replace the icon as there's no way to retro-fit it from PRO.
section.icon = DUMMY_SECTIONS[id]?.icon || LucideSettings;
return (
<ControlWrap
key={id}
label={section.title}
icon={section.icon}
>
<div className="grid gap-4">
{Object.entries(section.fields).map(
([sid, setting]) => {
return (
<div key={sid}>
{[
'text',
'url',
'textarea',
].includes(setting.type) && (
<TextInput
label={setting.label}
type={setting.type}
onChange={(e) =>
setFormData({
name: sid,
value: e.target
.value,
})
}
value={formData[sid]}
/>
)}
{setting.type === 'toggle' && (
<Toggle
checked={
formData[sid] === '1' ||
formData[sid] === true
}
description={
setting.description
}
label={setting.label}
onToggle={(value) =>
setFormData({
name: sid,
value,
})
}
/>
)}
</div>
);
}
)}
</div>
</ControlWrap>
);
})}
<div className="flex justify-end mt-6">
<Button isSubmit isPrimary loading={loading} disabled={loading}>
{loading
? __('Saving', 'neve') + '...'
: __('Save', 'neve')}
</Button>
</div>
</form>
);
};
export default () => {
const { isLicenseValid, licenseTier } = useLicenseData();
const showPlaceholder = !isLicenseValid || licenseTier < 3;
return (
<>
<div className="flex items-center justify-between mb-6">
<h1 className="text-lg font-semibold">
{__('White Label Settings', 'neve')}
</h1>
{showPlaceholder && (
<Button
target="_blank"
className="!p-0"
isLink
href={neveDash.upgradeWhiteLabelURL}
>
<LucideMessageCircleQuestion size={18} />
{__('Available in Agency Plan', 'neve')}
</Button>
)}
</div>
{showPlaceholder && <PlaceholderComponent />}
{!showPlaceholder && !neveDash.whiteLabelData && (
<Notice icon={CircleFadingArrowUp}>
{__(
'Please reload this page in order to view the White Label Settings',
'neve'
)}
</Notice>
)}
{!showPlaceholder && !!neveDash.whiteLabelData && (
<WhiteLabelSettings />
)}
</>
);
};