| 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 ServerSideRender from "@wordpress/server-side-render"; |
| 11 |
|
| 12 |
export interface TripCategoryBlockAttributes { |
| 13 |
order: "asc" | "desc"; |
| 14 |
columns: number; |
| 15 |
per_page: number; |
| 16 |
title: string; |
| 17 |
show_pagination: boolean; |
| 18 |
category: string; |
| 19 |
show_trip_count: boolean; |
| 20 |
show_description: boolean; |
| 21 |
show_image: boolean; |
| 22 |
hide_empty: boolean; |
| 23 |
featured_only: boolean; |
| 24 |
} |
| 25 |
|
| 26 |
interface EditProps { |
| 27 |
attributes: TripCategoryBlockAttributes; |
| 28 |
setAttributes: (attrs: Partial<TripCategoryBlockAttributes>) => void; |
| 29 |
} |
| 30 |
|
| 31 |
export default function Edit({ attributes, setAttributes }: EditProps) { |
| 32 |
const { |
| 33 |
order, |
| 34 |
columns, |
| 35 |
per_page, |
| 36 |
title, |
| 37 |
show_pagination, |
| 38 |
category, |
| 39 |
show_trip_count, |
| 40 |
show_description, |
| 41 |
show_image, |
| 42 |
hide_empty, |
| 43 |
featured_only, |
| 44 |
} = attributes; |
| 45 |
|
| 46 |
return ( |
| 47 |
<> |
| 48 |
<InspectorControls> |
| 49 |
<PanelBody |
| 50 |
title={__("Trip category settings", "yatra")} |
| 51 |
initialOpen={true} |
| 52 |
> |
| 53 |
<TextControl |
| 54 |
label={__("Title", "yatra")} |
| 55 |
value={title} |
| 56 |
onChange={(value: string) => setAttributes({ title: value })} |
| 57 |
/> |
| 58 |
<TextControl |
| 59 |
label={__("Categories (slugs)", "yatra")} |
| 60 |
help={__( |
| 61 |
"Optional. One slug or comma-separated slugs (same as the [yatra_trip_category] category attribute). Leave empty to list all categories.", |
| 62 |
"yatra", |
| 63 |
)} |
| 64 |
value={category} |
| 65 |
onChange={(value: string) => setAttributes({ category: value })} |
| 66 |
/> |
| 67 |
<SelectControl |
| 68 |
label={__("Order", "yatra")} |
| 69 |
value={order} |
| 70 |
options={[ |
| 71 |
{ label: __("Ascending", "yatra"), value: "asc" }, |
| 72 |
{ label: __("Descending", "yatra"), value: "desc" }, |
| 73 |
]} |
| 74 |
onChange={(value: string) => |
| 75 |
setAttributes({ order: value as "asc" | "desc" }) |
| 76 |
} |
| 77 |
/> |
| 78 |
<RangeControl |
| 79 |
label={__("Number of categories", "yatra")} |
| 80 |
value={per_page} |
| 81 |
onChange={(value: number | undefined) => |
| 82 |
setAttributes({ |
| 83 |
per_page: value !== undefined && value !== null ? value : 10, |
| 84 |
}) |
| 85 |
} |
| 86 |
min={1} |
| 87 |
max={50} |
| 88 |
/> |
| 89 |
<RangeControl |
| 90 |
label={__("Columns", "yatra")} |
| 91 |
value={columns} |
| 92 |
onChange={(value: number | undefined) => |
| 93 |
setAttributes({ |
| 94 |
columns: value !== undefined && value !== null ? value : 3, |
| 95 |
}) |
| 96 |
} |
| 97 |
min={1} |
| 98 |
max={6} |
| 99 |
/> |
| 100 |
<ToggleControl |
| 101 |
label={__("Show pagination", "yatra")} |
| 102 |
checked={show_pagination} |
| 103 |
onChange={(value: boolean) => |
| 104 |
setAttributes({ show_pagination: value }) |
| 105 |
} |
| 106 |
/> |
| 107 |
</PanelBody> |
| 108 |
|
| 109 |
<PanelBody |
| 110 |
title={__("Display options", "yatra")} |
| 111 |
initialOpen={false} |
| 112 |
> |
| 113 |
<ToggleControl |
| 114 |
label={__("Show trip count", "yatra")} |
| 115 |
checked={show_trip_count} |
| 116 |
onChange={(value: boolean) => |
| 117 |
setAttributes({ show_trip_count: value }) |
| 118 |
} |
| 119 |
/> |
| 120 |
<ToggleControl |
| 121 |
label={__("Show description", "yatra")} |
| 122 |
checked={show_description} |
| 123 |
onChange={(value: boolean) => |
| 124 |
setAttributes({ show_description: value }) |
| 125 |
} |
| 126 |
/> |
| 127 |
<ToggleControl |
| 128 |
label={__("Show image", "yatra")} |
| 129 |
checked={show_image} |
| 130 |
onChange={(value: boolean) => setAttributes({ show_image: value })} |
| 131 |
/> |
| 132 |
<ToggleControl |
| 133 |
label={__("Hide empty categories", "yatra")} |
| 134 |
checked={hide_empty} |
| 135 |
onChange={(value: boolean) => setAttributes({ hide_empty: value })} |
| 136 |
/> |
| 137 |
<ToggleControl |
| 138 |
label={__("Featured only (filter list)", "yatra")} |
| 139 |
help={__( |
| 140 |
"When on, only categories with “Featured category” enabled in Yatra → Trips → Categories are listed. The red “Featured” badge on each card is controlled per category there—not by this toggle.", |
| 141 |
"yatra", |
| 142 |
)} |
| 143 |
checked={featured_only} |
| 144 |
onChange={(value: boolean) => |
| 145 |
setAttributes({ featured_only: value }) |
| 146 |
} |
| 147 |
/> |
| 148 |
</PanelBody> |
| 149 |
</InspectorControls> |
| 150 |
|
| 151 |
<div className="yatra-block-editor-preview"> |
| 152 |
<ServerSideRender |
| 153 |
block="yatra/trip-category" |
| 154 |
attributes={attributes} |
| 155 |
key={JSON.stringify(attributes)} |
| 156 |
/> |
| 157 |
</div> |
| 158 |
</> |
| 159 |
); |
| 160 |
} |
| 161 |
|