HEX
Server: Apache/2.4.41 (Ubuntu)
System: Linux vm8 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64
User: afleverb (1000)
PHP: 7.4.33
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: //var/www/drakkar_site/wp-content/themes/blocksy/static/js/options/options/ct-entity-picker.js
import { createElement, useEffect, useState, useMemo } from '@wordpress/element'
import { __ } from 'ct-i18n'

import cachedFetch from 'ct-wordpress-helpers/cached-fetch'
import Select from './ct-select'

const withUniqueIDs = (data) =>
	data.filter(
		(value, index, self) =>
			self.findIndex((m) => m.id === value.id) === index
	)

const EntityIdPicker = ({
	value,
	option,
	option: {
		entity = 'posts',
		post_type = 'post',
		placeholder,
		additionOptions = [],
	},
	return_type = 'id',
	purpose,
	onChange,
}) => {
	const [allEntities, setAllEntities] = useState([])
	const requestBody = useMemo(() => {
		const requestBody = {}

		if (entity === 'posts') {
			requestBody.post_type = post_type
		}

		if (entity === 'taxonomies') {
			requestBody.post_type = post_type
		}

		return {
			...requestBody,
			...(value ? { alsoInclude: value } : {}),
		}
	}, [entity, post_type, value])

	const fetchPosts = (searchQuery = '') => {
		cachedFetch(
			`${wp.ajax.settings.url}?action=blocksy_get_all_entities`,
			{
				entity,

				...(searchQuery ? { search_query: searchQuery } : {}),
				...requestBody,
			},
			{
				// Abort intermediary requests.
				// TODO: maybe add a more specific name to the fetcherName to
				// avoid clashes with other instances of the same component.
				fetcherName: `entity-picker`,
			}
		)
			.then((r) => r.json())
			.then(({ data: { entities } }) => {
				setAllEntities(withUniqueIDs([...entities]))
			})
	}

	useEffect(() => {
		fetchPosts()
	}, [entity, post_type])

	return (
		<Select
			purpose={purpose}
			option={{
				appendToBody: true,
				defaultToFirstItem: false,
				searchPlaceholder: __(
					'Type to search by ID or title...',
					'blocksy-companion'
				),
				placeholder,
				choices: [
					...additionOptions,
					...allEntities.map((entity) => ({
						key: entity.id,
						value: entity.label,
						...(entity.group ? { group: entity.group } : {}),
					})),
				],
				search: true,
			}}
			value={value}
			onChange={(entity_id) => {
				if (return_type === 'entity') {
					onChange(
						allEntities.find(({ id }) => id === entity_id) ||
							entity_id
					)

					return
				}

				return onChange(entity_id)
			}}
			onInputValueChange={(value) => {
				if (
					allEntities.find(
						({ label, id }) => label === value || id === value
					)
				) {
					return
				}

				fetchPosts(value)
			}}
		/>
	)
}

export default EntityIdPicker