import { InspectorControls } from "@wordpress/block-editor"; import { PanelBody, TextControl, RangeControl, SelectControl, ToggleControl, } from "@wordpress/components"; import { __ } from "@wordpress/i18n"; import { useEffect, useRef } from "@wordpress/element"; import ServerSideRender from "@wordpress/server-side-render"; import { ClassificationMultiSelect } from "../components/ClassificationMultiSelect"; import { migrateNumericCsvPairToIds } from "../utils/migrateTaxonomyIds"; export interface TripCategoryBlockAttributes { order: "asc" | "desc"; columns: number; per_page: number; title: string; show_pagination: boolean; categoryIds: number[]; category?: string; category_ids?: string; show_trip_count: boolean; show_description: boolean; show_image: boolean; hide_empty: boolean; featured_only: boolean; } interface EditProps { attributes: TripCategoryBlockAttributes; setAttributes: (attrs: Partial) => void; } function normalizeIds(arr: number[] | undefined): number[] { return Array.isArray(arr) ? [...new Set(arr.map((n) => parseInt(String(n), 10)).filter((n) => n > 0))] : []; } export default function Edit({ attributes, setAttributes }: EditProps) { const migratedOnce = useRef(false); useEffect(() => { if (migratedOnce.current) { return; } migratedOnce.current = true; let ids = normalizeIds(attributes.categoryIds); if (ids.length === 0) { ids = migrateNumericCsvPairToIds( attributes.category_ids, attributes.category, ); } if (ids.length === 0) { return; } setAttributes({ categoryIds: ids, category: "", category_ids: "", }); // eslint-disable-next-line react-hooks/exhaustive-deps }, [setAttributes]); const { order, columns, per_page, title, show_pagination, show_trip_count, show_description, show_image, hide_empty, featured_only, } = attributes; const categoryIds = normalizeIds(attributes.categoryIds); return ( <> setAttributes({ title: value })} /> setAttributes({ categoryIds: ids })} /> setAttributes({ order: value as "asc" | "desc" }) } /> setAttributes({ per_page: value !== undefined && value !== null ? value : 10, }) } min={1} max={50} /> setAttributes({ columns: value !== undefined && value !== null ? value : 3, }) } min={1} max={6} /> setAttributes({ show_pagination: value }) } /> setAttributes({ show_trip_count: value }) } /> setAttributes({ show_description: value }) } /> setAttributes({ show_image: value })} /> setAttributes({ hide_empty: value })} /> setAttributes({ featured_only: value }) } />
); }