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-layers-combined.js
import { Fragment, createElement, Component } from '@wordpress/element'
import OptionsPanel from '../OptionsPanel'
import { DragDropContext, Droppable, Draggable } from 'react-beautiful-dnd'
import { reorder } from '../options/ct-layers'
import nanoid from 'nanoid'

import { getValueFromInput } from '../helpers/get-value-from-input'

const valueWithUniqueIds = (value) =>
	value.map((singleItem) => ({
		...singleItem,

		...(singleItem.__id
			? {}
			: {
					__id: nanoid(),
			  }),
	}))

const LayersCombined = ({ option, value, onChange }) => {
	let computedValue = Object.keys(value).reduce((acc, key) => {
		return {
			...acc,
			[key]: valueWithUniqueIds(value[key]),
		}
	}, {})

	const fullValue = Object.values(computedValue).reduce(
		(acc, value) => [...acc, ...value],
		[]
	)

	const defaultValue = Object.values(
		getValueFromInput(option['inner-options'], {})
	).reduce((acc, value) => [...acc, ...value])

	const missingItemsForRightColumn = valueWithUniqueIds([
		...defaultValue.filter(
			({ id }) => !fullValue.find(({ id: id2 }) => id === id2)
		),
	])

	computedValue.right = [
		...computedValue.right,
		...missingItemsForRightColumn,
	]

	return (
		<DragDropContext
			onDragEnd={(result) => {
				if (!result.destination) {
					return
				}

				if (
					result.destination.droppableId === result.source.droppableId
				) {
					onChange({
						...computedValue,

						[result.destination.droppableId]: reorder(
							computedValue[result.destination.droppableId],
							result.source.index,
							result.destination.index
						),
					})
					return
				}

				const current = computedValue[result.source.droppableId]
				const next = computedValue[result.destination.droppableId]
				const target = current[result.source.index]

				current.splice(result.source.index, 1)
				next.splice(result.destination.index, 0, target)

				onChange({
					...computedValue,
					[result.source.droppableId]: current,
					[result.destination.droppableId]: next,
				})
			}}>
			<OptionsPanel
				onChange={(key, optionValue) => {
					onChange({
						...computedValue,
						[key]: optionValue,
					})
				}}
				options={option['inner-options']}
				value={computedValue}
			/>
		</DragDropContext>
	)
}

export default LayersCombined