| @@ -6,9 +6,12 @@ | ||
| 6 | 6 | SelectControl, |
| 7 | 7 | ToggleControl, |
| 8 | 8 | } from "@wordpress/components"; |
| 9 | 9 | import { __ } from "@wordpress/i18n"; |
| 10 | +import { useEffect, useRef } from "@wordpress/element"; | |
| 10 | 11 | import ServerSideRender from "@wordpress/server-side-render"; |
| 12 | +import { ClassificationMultiSelect } from "../components/ClassificationMultiSelect"; | |
| 13 | +import { migrateNumericCsvPairToIds } from "../utils/migrateTaxonomyIds"; | |
| 11 | 14 | |
| 12 | 15 | interface ActivityBlockAttributes { |
| 13 | 16 | order: "asc" | "desc"; |
| 14 | 17 | columns: number; |
| @@ -14,8 +17,15 @@ | ||
| 14 | 17 | columns: number; |
| 15 | 18 | per_page: number; |
| 16 | 19 | title: string; |
| 17 | 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; | |
| 18 | 28 | } |
| 19 | 29 | |
| 20 | 30 | interface EditProps { |
| 21 | 31 | attributes: ActivityBlockAttributes; |
| @@ -21,9 +31,43 @@ | ||
| 21 | 31 | attributes: ActivityBlockAttributes; |
| 22 | 32 | setAttributes: (attrs: Partial<ActivityBlockAttributes>) => void; |
| 23 | 33 | } |
| 24 | 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 | + | |
| 25 | 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 | + | |
| 26 | 70 | return ( |
| 27 | 71 | <> |
| 28 | 72 | <InspectorControls> |
| 29 | 73 | <PanelBody title={__("Activity Settings", "yatra")} initialOpen={true}> |
| @@ -70,8 +114,49 @@ | ||
| 70 | 114 | checked={attributes.show_pagination} |
| 71 | 115 | onChange={(value: boolean) => |
| 72 | 116 | setAttributes({ show_pagination: value }) |
| 73 | 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 })} | |
| 74 | 159 | /> |
| 75 | 160 | </PanelBody> |
| 76 | 161 | </InspectorControls> |
| 77 | 162 | |