PluginProbe ʕ •ᴥ•ʔ
LatePoint – Calendar Booking Plugin for Appointments and Events / 5.6.6
LatePoint – Calendar Booking Plugin for Appointments and Events v5.6.6
5.6.7 5.6.6 5.6.5 5.6.4 5.6.3 5.6.2 5.6.1 5.6.0 5.5.2 5.5.1 5.5.0 5.4.2 trunk 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.1.8 5.1.9 5.1.91 5.1.92 5.1.93 5.1.94 5.2.0 5.2.1 5.2.10 5.2.11 5.2.2 5.2.3 5.2.4 5.2.5 5.2.6 5.2.7 5.2.8 5.2.9 5.3.0 5.3.1 5.3.2 5.4.0 5.4.1
latepoint / blocks / src / book-button / edit.js
latepoint / blocks / src / book-button Last commit date
block.json 3 weeks ago edit.js 1 week ago index-min.js 1 year ago index.js 1 year ago save.js 1 year ago
edit.js
381 lines
1 /**
2 * External dependencies
3 */
4 import styled from '@emotion/styled';
5
6 /**
7 * WordPress components that create the necessary UI elements for the block
8 *
9 * @see https://developer.wordpress.org/block-editor/packages/packages-components/
10 */
11 import {registerBlockType} from '@wordpress/blocks';
12 import {__} from '@wordpress/i18n';
13 import {
14 Panel,
15 PanelBody,
16 PanelRow,
17 TextControl,
18 SelectControl,
19 ToggleControl,
20 TabPanel,
21 __experimentalToggleGroupControl as ToggleGroupControl,
22 __experimentalToggleGroupControlOption as ToggleGroupControlOption
23 } from '@wordpress/components';
24
25 import {useEffect, useState} from '@wordpress/element';
26
27
28 /**
29 * React hook that is used to mark the block wrapper element.
30 * It provides all the necessary props like the class name.
31 *
32 * @see https://developer.wordpress.org/block-editor/reference-guides/packages/packages-block-editor/#useblockprops
33 */
34 import {
35 useBlockProps,
36 InspectorControls,
37 } from '@wordpress/block-editor';
38
39 import React from "react";
40 import IframeEmotionCacheProvider from '../utils/IframeEmotionCacheProvider';
41 import TypographyControl from "../../controls/TypographyControl";
42 import ColorSelectorControl from "../../controls/ColorSelectorControl";
43 import BorderControl from "../../controls/BorderControl";
44 import PaddingBoxControl from "../../controls/PaddingBoxControl";
45 import {SettingsIcon, StylesIcon} from "../../controls/LatepointIcons";
46
47 const LatepointBookButtonWrapper = styled.div`
48 `;
49
50 const LatepointBookButton = styled.div`
51 `;
52
53 const PanelRowBlock = styled(PanelRow)`
54 display: block;
55 margin-bottom: 20px;
56 `;
57
58 /**
59 * The edit function describes the structure of your block in the context of the
60 * editor. This represents what the editor will render when the block is used.
61 *
62 * @see https://developer.wordpress.org/block-editor/reference-guides/block-api/block-edit-save/#edit
63 *
64 * @param {Object} props Properties passed to the function.
65 * @param {Object} props.attributes Available block attributes.
66 * @param {Function} props.setAttributes Function that updates individual attributes.
67 *
68 * @return {WPElement} Element to render.
69 */
70 export default function Edit({attributes, setAttributes}) {
71 const blockProps = useBlockProps();
72
73 useEffect(() => {
74 if (!attributes.id) {
75 setAttributes({ id: Math.random().toString(36).substr(2, 9) + '-' + Date.now() });
76 }
77 }, []);
78
79 const [isHovered, setIsHovered] = useState(false);
80 const [activeTab, setActiveTab] = useState('settings');
81
82 const Separator = styled.div`
83 height: 1px;
84 background-color: #e0e0e0;
85 `;
86
87 const generateStyles = () => {
88 let styles = {}
89 if (attributes.is_inherit) return styles;
90
91 if (attributes.font_weight) styles.fontWeight = attributes.font_weight
92 if (attributes.font_size) styles.fontSize = attributes.font_size
93 if (attributes.text_transform) styles.textTransform = attributes.text_transform
94 if (attributes.font_family) styles.fontFamily = attributes.font_family
95 if (attributes.line_height) styles.lineHeight = attributes.line_height
96 if (attributes.letter_spacing) styles.letterSpacing = attributes.letter_spacing
97
98 if (attributes.border_radius) styles.borderRadius = attributes.border_radius
99 if (attributes.bg_color) styles.backgroundColor = isHovered && attributes.bg_color_hover ? attributes.bg_color_hover : attributes.bg_color;
100 if (attributes.text_color) styles.color = isHovered && attributes.text_color_hover ? attributes.text_color_hover : attributes.text_color;
101 if (attributes.border_color) styles.borderColor = isHovered && attributes.border_color_hover ? attributes.border_color_hover : attributes.border_color;
102 if (attributes.border_style && attributes.border_style !== 'default') styles.borderStyle = attributes.border_style;
103 if (attributes.border_width && attributes.border_style !== 'default') styles.borderWidth = attributes.border_width;
104 if (attributes.padding) styles.padding = attributes.padding;
105 return styles
106 }
107
108 const getGeneralTabs = () => {
109 let tabs = [
110 {name: 'settings', title: <div className="lb-tab-head"><SettingsIcon/>Settings</div>}
111 ];
112 if(!attributes.is_inherit){
113 tabs.push({name: 'styles', title: <div className="lb-tab-head"><StylesIcon/>Style</div>});
114 }
115 return tabs;
116 }
117
118 return (
119 <div {...blockProps}>
120
121 <InspectorControls>
122 <TabPanel
123 className="lb-general-tabs"
124 activeClass="active-tab"
125 tabs={ getGeneralTabs() }
126 onSelect={(tabName) => setActiveTab(tabName)}
127 >
128 {(tab) => {
129 if (tab.name === 'settings') {
130 return (
131 <>
132 <PanelBody title="Button Settings" initialOpen={true}>
133 <TextControl __nextHasNoMarginBottom __next40pxDefaultSize
134 label="Caption"
135 value={attributes.caption || ''}
136 onChange={(value) => setAttributes({caption: value})}
137 />
138 <ToggleControl __nextHasNoMarginBottom
139 label="Inherit From Theme"
140 checked={attributes.is_inherit}
141 onChange={(value) => setAttributes({is_inherit: value})}
142 />
143 </PanelBody>
144 <PanelBody title="Booking Form Settings" initialOpen={false}>
145 <ToggleControl __nextHasNoMarginBottom
146 label="Hide Summary Panel"
147 checked={attributes.hide_summary}
148 onChange={(value) => setAttributes({hide_summary: value})}
149 />
150 <ToggleControl __nextHasNoMarginBottom
151 label="Hide Side Panel"
152 checked={attributes.hide_side_panel}
153 onChange={(value) => setAttributes({hide_side_panel: value})}
154 />
155 </PanelBody>
156
157 <PanelBody title="Step Settings" initialOpen={false}>
158 <SelectControl __nextHasNoMarginBottom __next40pxDefaultSize
159 value={attributes.selected_agent}
160 label={__('Preselected Agent', 'latepoint')}
161 onChange={(value) => setAttributes({selected_agent: value})}
162 options={latepoint_helper.selected_agents_options}
163 />
164 <SelectControl __nextHasNoMarginBottom __next40pxDefaultSize
165 value={attributes.selected_service}
166 label={__('Preselected Service', 'latepoint')}
167 onChange={(value) => setAttributes({selected_service: value})}
168 options={latepoint_helper.selected_services_options}
169 />
170 <SelectControl __nextHasNoMarginBottom __next40pxDefaultSize
171 value={attributes.selected_service_category}
172 label={__('Preselected Service Category', 'latepoint')}
173 onChange={(value) => setAttributes({selected_service_category: value})}
174 options={latepoint_helper.selected_service_categories_options}
175 />
176 <SelectControl __nextHasNoMarginBottom __next40pxDefaultSize
177 value={attributes.selected_bundle}
178 label={__('Preselected Bundle', 'latepoint')}
179 onChange={(value) => setAttributes({selected_bundle: value})}
180 options={latepoint_helper.selected_bundles_options}
181 />
182 <SelectControl __nextHasNoMarginBottom __next40pxDefaultSize
183 value={attributes.selected_location}
184 label={__('Preselected Location', 'latepoint')}
185 onChange={(value) => setAttributes({selected_location: value})}
186 options={latepoint_helper.selected_locations_options}
187 />
188 <TextControl __nextHasNoMarginBottom __next40pxDefaultSize
189 label={__('Preselected Booking Start Date', 'latepoint')}
190 value={attributes.selected_start_date || ''}
191 placeholder="YYYY-MM-DD"
192 onChange={(value) => setAttributes({selected_start_date: value})}
193 />
194 <TextControl __nextHasNoMarginBottom __next40pxDefaultSize
195 label={__('Preselected Booking Start Time', 'latepoint')}
196 value={attributes.selected_start_time || ''}
197 placeholder="Minutes"
198 onChange={(value) => setAttributes({selected_start_time: value})}
199 />
200 <TextControl __nextHasNoMarginBottom __next40pxDefaultSize
201 label={__('Preselected Duration', 'latepoint')}
202 value={attributes.selected_duration || ''}
203 placeholder="Minutes"
204 onChange={(value) => setAttributes({selected_duration: value})}
205 />
206 <TextControl __nextHasNoMarginBottom __next40pxDefaultSize
207 label={__('Preselected Total Attendees', 'latepoint')}
208 value={attributes.selected_total_attendees || ''}
209 placeholder="Number"
210 onChange={(value) => setAttributes({selected_total_attendees: value})}
211 />
212 </PanelBody>
213
214 <PanelBody title="Other Settings" initialOpen={false}>
215 <TextControl __nextHasNoMarginBottom __next40pxDefaultSize
216 label="Source ID"
217 value={attributes.source_id || ''}
218 onChange={(value) => setAttributes({source_id: value})}
219 />
220 <TextControl __nextHasNoMarginBottom __next40pxDefaultSize
221 label="Calendar Start Date"
222 value={attributes.calendar_start_date || ''}
223 placeholder="YYYY-MM-DD"
224 onChange={(value) => setAttributes({calendar_start_date: value})}
225 />
226 <TextControl __nextHasNoMarginBottom __next40pxDefaultSize
227 label="Show Services"
228 placeholder="Comma separated service IDs"
229 value={attributes.show_services || ''}
230 onChange={(value) => setAttributes({show_services: value})}
231 />
232 <TextControl __nextHasNoMarginBottom __next40pxDefaultSize
233 label="Show Service Categories"
234 placeholder="Comma separated category IDs"
235 value={attributes.show_service_categories || ''}
236 onChange={(value) => setAttributes({show_service_categories: value})}
237 />
238 <TextControl __nextHasNoMarginBottom __next40pxDefaultSize
239 label="Show Agents"
240 placeholder="Comma separated agent IDs"
241 value={attributes.show_agents || ''}
242 onChange={(value) => setAttributes({show_agents: value})}
243 />
244
245 <TextControl __nextHasNoMarginBottom __next40pxDefaultSize
246 label="Show Locations"
247 placeholder="Comma separated location IDs"
248 value={attributes.show_locations || ''}
249 onChange={(value) => setAttributes({show_locations: value})}
250 />
251 </PanelBody>
252 </>
253 );
254 }
255 if (tab.name === 'styles') {
256 return (
257 <>
258 <Panel>
259 <PanelBody>
260 <ToggleGroupControl
261 className="lb-toggle-group"
262 isBlock
263 isDeselectable={true}
264 value={attributes.align}
265 label={__('Alignment', 'latepoint')}
266 onChange={(value) => {
267 setAttributes({align: value})
268 }}
269 >
270 <ToggleGroupControlOption label="Left" value="left"/>
271 <ToggleGroupControlOption label="Center" value="center"/>
272 <ToggleGroupControlOption label="Right" value="right"/>
273 <ToggleGroupControlOption label="Justify" value="justify"/>
274 </ToggleGroupControl>
275 </PanelBody>
276 </Panel>
277
278 {!attributes.is_inherit && (
279 <Panel>
280 <PanelBody>
281 <PaddingBoxControl
282 label={__('Padding', 'latepoint')}
283 paddingAttribute="padding"
284 attributes={attributes}
285 setAttributes={setAttributes}
286 />
287 </PanelBody>
288 <PanelBody title="Color" initialOpen={false}>
289 <TabPanel
290 className="lb-tabs"
291 activeClass="active-tab"
292 tabs={[
293 {name: 'tab-normal', title: 'Normal',},
294 {name: 'tab-hover', title: 'Hover',},
295 ]}
296 >
297 {(tab) => {
298 if (tab.name === 'tab-normal') {
299 return (
300 <>
301 <PanelRow>
302 <ColorSelectorControl
303 attributes={attributes}
304 setAttributes={setAttributes}
305 colorAttribute="bg_color"
306 label={__('Background Color', 'latepoint')}
307 ></ColorSelectorControl>
308 </PanelRow>
309 <PanelRow>
310 <ColorSelectorControl
311 attributes={attributes}
312 setAttributes={setAttributes}
313 colorAttribute="text_color"
314 label={__('Text Color', 'latepoint')}
315 ></ColorSelectorControl>
316 </PanelRow>
317 </>
318 );
319 }
320 if (tab.name === 'tab-hover') {
321 return (
322 <>
323 <PanelRow>
324 <ColorSelectorControl
325 attributes={attributes}
326 setAttributes={setAttributes}
327 colorAttribute="bg_color_hover"
328 label={__('Background Color', 'latepoint')}
329 ></ColorSelectorControl>
330 </PanelRow>
331 <PanelRow>
332 <ColorSelectorControl
333 attributes={attributes}
334 setAttributes={setAttributes}
335 colorAttribute="text_color_hover"
336 label={__('Text Color', 'latepoint')}
337 ></ColorSelectorControl>
338 </PanelRow>
339 </>
340 );
341 }
342 }}
343 </TabPanel>
344 </PanelBody>
345
346 <PanelBody title="Text" initialOpen={false}>
347 <TypographyControl attributes={attributes} setAttributes={setAttributes} fontSizeAttr="font_size"></TypographyControl>
348 </PanelBody>
349 <PanelBody title="Border" initialOpen={false}>
350 <BorderControl attributes={attributes} setAttributes={setAttributes} borderRadiusAttr="border_radius"></BorderControl>
351 </PanelBody>
352 </Panel>
353 )}
354 </>
355 );
356 }
357 }}
358 </TabPanel>
359
360 <Panel>
361 <Separator></Separator>
362 </Panel>
363
364 </InspectorControls>
365
366 <IframeEmotionCacheProvider>
367 <LatepointBookButtonWrapper
368 className={'latepoint-book-button-wrapper ' + 'wp-block-button ' + (attributes.align ? `latepoint-book-button-align-${attributes.align}` : '')}>
369 <LatepointBookButton
370 onMouseEnter={() => setIsHovered(true)}
371 onMouseLeave={() => setIsHovered(false)}
372 style={generateStyles()}
373 className="wp-block-button__link latepoint-book-button">
374 {attributes.caption}
375 </LatepointBookButton>
376 </LatepointBookButtonWrapper>
377 </IframeEmotionCacheProvider>
378 </div>
379 );
380 }
381