PluginProbe
Yatra – Travel Booking & Tour Operator Software / 3.0.4
Yatra – Travel Booking & Tour Operator Software v3.0.4
3.0.16 3.0.15 3.0.14 3.0.14.1 3.0.14.2 3.0.12 3.0.13 3.0.11 3.0.10 3.0.9 3.0.8 3.0.7 3.0.6 3.0.5 3.0.5.1 3.0.4 3.0.3 3.0.2.9 3.0.2.7 3.0.2.8 3.0.2.6 trunk 1.0.0 2.0.0 2.0.1 All 84 releases
yatra / resources / js / blocks / activity / edit.tsx

edit.tsx in Yatra – Travel Booking & Tour Operator Software 3.0.4, at resources/js/blocks/activity/edit.tsx

135 lines 3.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { InspectorControls } from "@wordpress/block-editor";
2 import {
3 PanelBody,
4 TextControl,
5 RangeControl,
6 SelectControl,
7 ToggleControl,
8 } from "@wordpress/components";
9 import { __ } from "@wordpress/i18n";
10 import { useEffect, useRef } from "@wordpress/element";
11 import ServerSideRender from "@wordpress/server-side-render";
12 import { ClassificationMultiSelect } from "../components/ClassificationMultiSelect";
13 import { migrateNumericCsvPairToIds } from "../utils/migrateTaxonomyIds";
14
15 interface ActivityBlockAttributes {
16 order: "asc" | "desc";
17 columns: number;
18 per_page: number;
19 title: string;
20 show_pagination: boolean;
21 activityIds: number[];
22 activity_ids?: string;
23 activity?: string;
24 }
25
26 interface EditProps {
27 attributes: ActivityBlockAttributes;
28 setAttributes: (attrs: Partial<ActivityBlockAttributes>) => void;
29 }
30
31 function normalizeIds(arr: number[] | undefined): number[] {
32 return Array.isArray(arr)
33 ? [...new Set(arr.map((n) => parseInt(String(n), 10)).filter((n) => n > 0))]
34 : [];
35 }
36
37 export default function Edit({ attributes, setAttributes }: EditProps) {
38 const migratedOnce = useRef(false);
39
40 useEffect(() => {
41 if (migratedOnce.current) {
42 return;
43 }
44 migratedOnce.current = true;
45
46 let ids = normalizeIds(attributes.activityIds);
47 if (ids.length === 0) {
48 ids = migrateNumericCsvPairToIds(attributes.activity_ids, attributes.activity);
49 }
50 if (ids.length === 0) {
51 return;
52 }
53 setAttributes({
54 activityIds: ids,
55 activity: "",
56 activity_ids: "",
57 });
58 // eslint-disable-next-line react-hooks/exhaustive-deps
59 }, [setAttributes]);
60
61 const activityIds = normalizeIds(attributes.activityIds);
62
63 return (
64 <>
65 <InspectorControls>
66 <PanelBody title={__("Activity Settings", "yatra")} initialOpen={true}>
67 <TextControl
68 label={__("Title", "yatra")}
69 value={attributes.title}
70 onChange={(value: string) => setAttributes({ title: value })}
71 />
72 <SelectControl
73 label={__("Order", "yatra")}
74 value={attributes.order}
75 options={[
76 { label: __("Ascending", "yatra"), value: "asc" },
77 { label: __("Descending", "yatra"), value: "desc" },
78 ]}
79 onChange={(value: string) =>
80 setAttributes({ order: value as "asc" | "desc" })
81 }
82 />
83 <RangeControl
84 label={__("Number of Activities", "yatra")}
85 value={attributes.per_page}
86 onChange={(value: number | undefined) =>
87 setAttributes({
88 per_page: value !== undefined && value !== null ? value : 10,
89 })
90 }
91 min={1}
92 max={50}
93 />
94 <RangeControl
95 label={__("Columns", "yatra")}
96 value={attributes.columns}
97 onChange={(value: number | undefined) =>
98 setAttributes({
99 columns: value !== undefined && value !== null ? value : 3,
100 })
101 }
102 min={1}
103 max={6}
104 />
105 <ToggleControl
106 label={__("Show pagination", "yatra")}
107 checked={attributes.show_pagination}
108 onChange={(value: boolean) =>
109 setAttributes({ show_pagination: value })
110 }
111 />
112 <ClassificationMultiSelect
113 taxonomy="activity"
114 label={__("Activities to show", "yatra")}
115 help={__(
116 "Use \"All published\" or narrow with search and checkboxes.",
117 "yatra",
118 )}
119 value={activityIds}
120 onChange={(ids) => setAttributes({ activityIds: ids })}
121 />
122 </PanelBody>
123 </InspectorControls>
124
125 <div className="yatra-block-editor-preview">
126 <ServerSideRender
127 block="yatra/activity"
128 attributes={attributes}
129 key={JSON.stringify(attributes)}
130 />
131 </div>
132 </>
133 );
134 }
135