| 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 |
show_trip_count: boolean; |
| 25 |
show_description: boolean; |
| 26 |
show_image: boolean; |
| 27 |
hide_empty: boolean; |
| 28 |
} |
| 29 |
|
| 30 |
interface EditProps { |
| 31 |
attributes: ActivityBlockAttributes; |
| 32 |
setAttributes: (attrs: Partial<ActivityBlockAttributes>) => void; |
| 33 |
} |
| 34 |
|
| 35 |
function normalizeIds(arr: number[] | undefined): number[] { |
| 36 |
return Array.isArray(arr) |
| 37 |
? [...new Set(arr.map((n) => parseInt(String(n), 10)).filter((n) => n > 0))] |
| 38 |
: []; |
| 39 |
} |
| 40 |
|
| 41 |
export default function Edit({ attributes, setAttributes }: EditProps) { |
| 42 |
const migratedOnce = useRef(false); |
| 43 |
|
| 44 |
useEffect(() => { |
| 45 |
if (migratedOnce.current) { |
| 46 |
return; |
| 47 |
} |
| 48 |
migratedOnce.current = true; |
| 49 |
|
| 50 |
let ids = normalizeIds(attributes.activityIds); |
| 51 |
if (ids.length === 0) { |
| 52 |
ids = migrateNumericCsvPairToIds( |
| 53 |
attributes.activity_ids, |
| 54 |
attributes.activity, |
| 55 |
); |
| 56 |
} |
| 57 |
if (ids.length === 0) { |
| 58 |
return; |
| 59 |
} |
| 60 |
setAttributes({ |
| 61 |
activityIds: ids, |
| 62 |
activity: "", |
| 63 |
activity_ids: "", |
| 64 |
}); |
| 65 |
// eslint-disable-next-line react-hooks/exhaustive-deps |
| 66 |
}, [setAttributes]); |
| 67 |
|
| 68 |
const activityIds = normalizeIds(attributes.activityIds); |
| 69 |
|
| 70 |
return ( |
| 71 |
<> |
| 72 |
<InspectorControls> |
| 73 |
<PanelBody title={__("Activity Settings", "yatra")} initialOpen={true}> |
| 74 |
<TextControl |
| 75 |
label={__("Title", "yatra")} |
| 76 |
value={attributes.title} |
| 77 |
onChange={(value: string) => setAttributes({ title: value })} |
| 78 |
/> |
| 79 |
<SelectControl |
| 80 |
label={__("Order", "yatra")} |
| 81 |
value={attributes.order} |
| 82 |
options={[ |
| 83 |
{ label: __("Ascending", "yatra"), value: "asc" }, |
| 84 |
{ label: __("Descending", "yatra"), value: "desc" }, |
| 85 |
]} |
| 86 |
onChange={(value: string) => |
| 87 |
setAttributes({ order: value as "asc" | "desc" }) |
| 88 |
} |
| 89 |
/> |
| 90 |
<RangeControl |
| 91 |
label={__("Number of Activities", "yatra")} |
| 92 |
value={attributes.per_page} |
| 93 |
onChange={(value: number | undefined) => |
| 94 |
setAttributes({ |
| 95 |
per_page: value !== undefined && value !== null ? value : 10, |
| 96 |
}) |
| 97 |
} |
| 98 |
min={1} |
| 99 |
max={50} |
| 100 |
/> |
| 101 |
<RangeControl |
| 102 |
label={__("Columns", "yatra")} |
| 103 |
value={attributes.columns} |
| 104 |
onChange={(value: number | undefined) => |
| 105 |
setAttributes({ |
| 106 |
columns: value !== undefined && value !== null ? value : 3, |
| 107 |
}) |
| 108 |
} |
| 109 |
min={1} |
| 110 |
max={6} |
| 111 |
/> |
| 112 |
<ToggleControl |
| 113 |
label={__("Show pagination", "yatra")} |
| 114 |
checked={attributes.show_pagination} |
| 115 |
onChange={(value: boolean) => |
| 116 |
setAttributes({ show_pagination: value }) |
| 117 |
} |
| 118 |
/> |
| 119 |
<ClassificationMultiSelect |
| 120 |
taxonomy="activity" |
| 121 |
label={__("Activities to show", "yatra")} |
| 122 |
help={__( |
| 123 |
'Use "All published" or narrow with search and checkboxes.', |
| 124 |
"yatra", |
| 125 |
)} |
| 126 |
value={activityIds} |
| 127 |
onChange={(ids) => setAttributes({ activityIds: ids })} |
| 128 |
/> |
| 129 |
</PanelBody> |
| 130 |
|
| 131 |
<PanelBody title={__("Display options", "yatra")} initialOpen={false}> |
| 132 |
<ToggleControl |
| 133 |
label={__("Show trip count", "yatra")} |
| 134 |
checked={attributes.show_trip_count} |
| 135 |
onChange={(value: boolean) => |
| 136 |
setAttributes({ show_trip_count: value }) |
| 137 |
} |
| 138 |
/> |
| 139 |
<ToggleControl |
| 140 |
label={__("Show description", "yatra")} |
| 141 |
checked={attributes.show_description} |
| 142 |
onChange={(value: boolean) => |
| 143 |
setAttributes({ show_description: value }) |
| 144 |
} |
| 145 |
/> |
| 146 |
<ToggleControl |
| 147 |
label={__("Show image", "yatra")} |
| 148 |
checked={attributes.show_image} |
| 149 |
onChange={(value: boolean) => setAttributes({ show_image: value })} |
| 150 |
/> |
| 151 |
<ToggleControl |
| 152 |
label={__("Hide empty activities", "yatra")} |
| 153 |
help={__( |
| 154 |
"Skip activities that have no published trips assigned. Off by default — turn on for a tighter live catalog, leave off for editorial / onboarding views.", |
| 155 |
"yatra", |
| 156 |
)} |
| 157 |
checked={attributes.hide_empty} |
| 158 |
onChange={(value: boolean) => setAttributes({ hide_empty: value })} |
| 159 |
/> |
| 160 |
</PanelBody> |
| 161 |
</InspectorControls> |
| 162 |
|
| 163 |
<div className="yatra-block-editor-preview"> |
| 164 |
<ServerSideRender |
| 165 |
block="yatra/activity" |
| 166 |
attributes={attributes} |
| 167 |
key={JSON.stringify(attributes)} |
| 168 |
/> |
| 169 |
</div> |
| 170 |
</> |
| 171 |
); |
| 172 |
} |
| 173 |
|